This code will take the text from a field on the form and set the form title
to the text. It will also rename the document to the specified text.
Finally it will redirect the XWiki to the newly named document. This all
happens when the user saves the document.
I have commented a large part of the code for myself and anyone else trying
to understand.
{{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;
// The main purpose of this class is to allow us to change/add the
'xredirect' parameter on the request. Because we are renaming the document
// in the ProcessDocumentRenamer, if we don't redirect the browser to the
new document name, they will get a message saying the document they
// just saved doesn't exist or has been deleted.
// This class wraps the HttpServletRequest class from the context. The
reason we do this is so we can access the contents of the request.
// There is a property('xredirect') that we want to change to point at our
renamed document.
// This also implements XWikiRequest interface. The reason we do this is
because this interface is how we access the 'xredirect' parameter.
public class RequestWrapper extends HttpServletRequestWrapper implements
XWikiRequest
{
// Used to hold the parameters.
private HashMap params = new HashMap();
// The class constructor. It takes the HttpServletRequest, object that
it is going to wrap, as an input parameter.
public RequestWrapper(HttpServletRequest request)
{
// I do not know what super does? Do anybody else know?
super(request);
}
// Gets parameters from the class.
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 );
}
// Method to add parameters to a class. We will use this when we add
'xredirect'.
public void addParameter( String name, String value )
{
params.put( name, value );
}
// This is needed to meet the XWikiRequest interface requirements.
Since the request is already an XWikiRequest, I just
// box it and pass the parameter through as the return value.
public String get(String name)
{
return ((XWikiRequest)this).get(name);
}
// This is needed to meet the XWikiRequest interface requirements.
Since the request is already an XWikiRequest, I just
// box it and pass the parameter through as the return value.
public javax.servlet.http.Cookie getCookie(String cookieName)
{
return ((XWikiRequest)this).getCookie(cookieName);
}
// This is needed to meet the XWikiRequest interface requirements.
Since the request is already an XWikiRequest, I just
// box it and pass it as the return value.
public javax.servlet.http.HttpServletRequest getHttpServletRequest()
{
return ((javax.servlet.http.HttpServletRequest)this);
}
}
// The main purpose of this class is to rename and re-title a document of
type "DocumentationTemplates.ProcessClass" to the title
// specified by the user in the "processName" field within the document.
This class is a listener that waits for the
// "DocumentCreateEvent" or "DocumentUpdateEvents" for the
"DocumentationTemplates.ProcessClass". It then checks to see if the
// current document name matches the "processName" field. If it doesn't,
it will rename and re-title the document to what is
// specified in the "processName" field. It will then use the
"RequestWrapper" to direct the user's browser to the new document URL.
class ProcessDocumentRenamer implements EventListener
{
// The "ProcessDocumentRenamer" constructor. The "xwiki" and
"context"
input parameters are required, but we do not need them, so
// nothing is done with them.
ProcessDocumentRenamer(xwiki, context)
{
}
// Required to implement the "EventListener" interface. It provides the
name of the listener to the system.
String getName()
{
// The unique name of this event listener
return "ProcessDocumentRenamer"
}
// Required to implement the "EventListener" interface. It provides the
events that the listener will respond to.
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)
{
// Defines the type of object we should be looked for.
def myObject =
source.getObject("DocumentationTemplates.ProcessClass");
// If the page has an object of the specified class, we get the
object.
if (myObject != null)
{
// Get the title that the user specified for the document and
store it in the variable "NewDocumentName".
// We will later use to update the title and name of the
document.
def NewDocumentName = myObject.get("processName").value
// Check if the document name is different than the one the user
specified.
if (source.getName() != NewDocumentName)
{
// Build the new name for the document in the format of
"SpaceName.PageName".
def FullDocumentName = source.getSpaceName() + "." +
NewDocumentName;
// Set the document title to the new name.
source.setTitle(NewDocumentName)
// Force the storage to keep the same version number, so
that this looks like a single save event
source.setMetaDataDirty(false);
source.setContentDirty(false);
// Get the current context. It is needed to rename the
document.
def crtContext =
Utils.getComponent(Execution.class).getContext().getProperty('xwikicontext')
// Rename the document
source.rename(FullDocumentName, crtContext);
// Create an instance of the RequestWrapper class and give
it the request from the current context.
def RedirectRequest = new
RequestWrapper(crtContext.getRequest());
// Get the URL for the document (which has already been
renamed, so it points to the new location) and
// store it in the RedirectRequest.
RedirectRequest.addParameter('xredirect',
source.getURL('view', crtContext));
// Store the modified request back in the context.
crtContext.setRequest(RedirectRequest);
}
}
}
}
// Register against the Observation Manager
def observation = Utils.getComponent(ObservationManager.class)
observation.removeListener("ProcessDocumentRenamer")
def listener = new ProcessDocumentRenamer(xwiki, xcontext)
observation.addListener(listener)
{{/groovy}}
--
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.