Tech ValidationSpecificationClass
package aaaia.xwiki;
import com.xpn.xwiki.validation.*;
import com.xpn.xwiki.*;
import com.xpn.xwiki.doc.*;
import com.xpn.xwiki.objects.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.lang.StringEscapeUtils;
public class SpecificationValidation implements XWikiValidationInterface {
private static final Logger log = LoggerFactory.getLogger(SpecificationValidation.class);
private static String SPACE_NAME = "Species"
private static String CLAZZ_NAME = "${SPACE_NAME}.SpecificationClass";
private static String HTML_TITLE_DISPLAY_CLASS_NAME = "Species.HtmlTitleDisplayClass"
private static String HTML_TITLE_FIELD_NAME = "pHtmlTitle"
private static String DEFAULT_PARENT = "Species.Incertae sedis"
private static def PARENT_ALLOWED_CLASSES = [CLAZZ_NAME]
private Object aiautil = null;
public boolean validateDocument(XWikiDocument doc, XWikiContext context) {
def res = true;
try {
aiautil = context.getWiki().parseGroovyFromPage("Species.Tech IkonotekaScripts",context);
def obj = doc.getObject(CLAZZ_NAME);
def esc = {input ->
return StringEscapeUtils.escapeHtml(""+input)
}
get properties from object
def pSpecTypeNonEsc = obj.getStringValue("pSpecType")
def pSpecValueNonEsc = obj.getStringValue("pSpecValue")
def pAutorOpisuNonEsc = obj.getStringValue("pAutorOpisu")
def pRokOpisuNonEsc = obj.getIntValue("pRokOpisu")
def pSpecType = esc(pSpecTypeNonEsc)
def pSpecValue = esc(pSpecValueNonEsc)
def pAutorOpisu = esc(pAutorOpisuNonEsc)
no need of escaping as it is int
def pRokOpisu = pRokOpisuNonEsc
title interception
def properTitle
properTitle = "$pSpecTypeNonEsc $pSpecValueNonEsc" $pAutorOpisuNonEsc, $pRokOpisuNonEsc"
if (doc.getTitle() != properTitle && doc.getTitle()){
doc.setTitle(properTitle)
}
html title interception
it is mandatory to escape html in html title
def htmlTitleObj = doc.getObject(HTML_TITLE_DISPLAY_CLASS_NAME,true, context)
def pHtmlTitle = htmlTitleObj.getStringValue(HTML_TITLE_FIELD_NAME)
def properHtmlTitle
jeśli brak roku bądź autora to odpowiednia sekcja nie powinna się wyświetlać
def tmpPRokOpisu = (pRokOpisu==0 || !pRokOpisu)?null:pRokOpisu;
def pAutorPRokOpisuPart = [pAutorOpisu, tmpPRokOpisu].grep{it}.join(", ")
if (pAutorPRokOpisuPart) {
pAutorPRokOpisuPart = " " + pAutorPRokOpisuPart
}
if (aiautil.taksonyPisaneKursywa.contains(pSpecType)) {
properHtmlTitle = "$pSpecType <i>$pSpecValue</i>$pAutorPRokOpisuPart"
} else {
properHtmlTitle = "$pSpecType $pSpecValue$pAutorPRokOpisuPart"
}
if (properHtmlTitle != pHtmlTitle) {
htmlTitleObj.setStringValue(HTML_TITLE_FIELD_NAME, properHtmlTitle)
}
parent interception
def interceptParentName = context.getRequest().getParameter("myinput")
interceptParentName = org.apache.commons.lang.StringEscapeUtils.unescapeHtml(interceptParentName )
log.debug("Setting parent/interceptParentName=$interceptParentName;")
if (doc.getParent() != interceptParentName && interceptParentName){
def parentDoc = context.getWiki().getDocument(interceptParentName, context);
log.debug("Setting parent/parentDoc != null=${parentDoc != null};")
if (!aiautil.hasOneOfClasses(parentDoc, context, PARENT_ALLOWED_CLASSES)) {
log.debug("!aiautil.hasOneOfClasses(parentDoc, context, PARENT_ALLOWED_CLASSES)=TRUE")
com.xpn.xwiki.XWiki xwiki = context.getWiki();
def hasAdmin = xwiki.getRightService().hasAdminRights(context);
if (!hasAdmin) {
interceptParentName = DEFAULT_PARENT
}
}
log.debug("Setting parent[$interceptParentName] for page[${doc.title}]")
doc.setParent(interceptParentName)
}
aiautil.storeParentFullNameToCache(doc, CLAZZ_NAME, context.getRequest())
aiautil.setLastToCache(context.getRequest(), CLAZZ_NAME, "pSpecType", pSpecType)
} catch (e) {
log.error(e.message,e)
e.printStackTrace()
res = false;
}
return res;
}
public boolean validateObject(BaseObject object, XWikiContext context) {
return true;
}
}