On Wed, Mar 26, 2014 at 11:00 PM, DeHaynes <ew1560(a)wayne.edu> wrote:
I feel like I am so close, but it just is not working.
I have the following
code but it is throwing an error. I will attach a screenshot of the error.
(My company is blocking SSH all of a sudden. So I cannot copy the file.) It
is saying there is no such property. I assume it means the "xredirect"?
{{groovy}}
import org.xwiki.observation.*
import org.xwiki.observation.event.*
import org.xwiki.bridge.event.*
import org.xwiki.context.*
import com.xpn.xwiki.web.*
import com.xpn.xwiki.*
import org.apache.velocity.VelocityContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class RequestWrapper extends
HttpServletRequestWrapper
public class RequestWrapper extends HttpServletRequestWrapper
implements XWikiRequest
(you need to implement XWikiRequest to be able to call
XWikiContext#setRequest(XWikiRequest) below)
{
private HashMap params = new HashMap();
public RequestWrapper(HttpServletRequest request)
{
super(request);
}
public String getParameter(String name)
{
// if we added one, return that one
if ( params.get( name ) != null )
{
return params.get( name );
}
// otherwise return what's in the original request
HttpServletRequest req = (HttpServletRequest) super.getRequest();
return req.getParameter( name );
}
public void addParameter( String name, String value )
{
params.put( name, value );
}
}
class ProcessDocumentRenamer implements EventListener
{
def xwiki
def context
def response
ProcessDocumentRenamer(xwiki, context)
{
this.xwiki = xwiki
this.context = context
You don't need this and it's not even recommended to keep a reference
to the xwiki and context object from the moment the event was created.
The event listener should use (as you did below) the context from when
the event is fired.
}
String getName()
{
// The unique name of this event listener
return "ProcessDocumentRenamer"
}
List<Event> getEvents()
{
// The list of events this listener listens to
return Arrays.asList(new DocumentCreatedEvent(), new
DocumentUpdatedEvent())
}
// Called by the Observation Manager when an event matches the list of
events returned
// by getEvents()
void onEvent(Event event, Object source, Object data)
{
// Current context
def crtContext =
Utils.getComponent(Execution.class).getContext().getProperty('xwikicontext')
def response =
Utils.getComponent(Execution.class).getContext().getProperty('response')
// Defines the type of object that should be looked for
def myObject =
source.getObject("DocumentationTemplates.ProcessClass");
// If the page has an object of the specified class, we get the
value of the relevant property and assign it to our title variable
if (myObject != null) {
// Defines the variable that we will later use to update the title
of the document
def documentName = myObject.get("processName").value
if (source.getName() != documentName) {
// Build the new name for the document.
def FullDocumentName =
source.getSpaceName() + "." +
documentName;
It's recommended to use DocumentReference instead:
def newDocRef = new DocumentReference(documentName,
source.getDocumentReference().getLastSpaceReference())
// Set the document title to the new name.
source.setTitle(documentName)
// Force the storage to keep the same version number, so that
this looks like a single save event
source.setMetaDataDirty(false);
source.setContentDirty(false);
// Rename the document
source.rename(FullDocumentName, crtContext);
def RedirectRequest = new RequestWrapper(request);
RedirectRequest.addParameter('xredirect',xwiki.getURL(FullDocumentName));
RedirectRequest.addParameter('xredirect', source.getURL('view',
crtContext));
crtContext.setRequest(RedirectRequest);
Hope this helps,
Marius
}
}
}
}
// Register against the Observation Manager
def observation = Utils.getComponent(ObservationManager.class)
observation.removeListener("ProcessDocumentRenamer")
def listener = new ProcessDocumentRenamer(xwiki, xcontext)
observation.addListener(listener)
{{/groovy}}
<http://xwiki.475771.n2.nabble.com/file/n7589834/Error.png>
--
View this message in context:
http://xwiki.475771.n2.nabble.com/Looking-for-help-with-groovy-tp7589782p75…
Sent from the XWiki- Dev mailing list archive at
Nabble.com.
_______________________________________________
devs mailing list
devs(a)xwiki.org
http://lists.xwiki.org/mailman/listinfo/devs