use of com.xpn.xwiki.objects.classes.PropertyClass in project xwiki-platform by xwiki.
the class XWikiDocument method display.
/**
* @param fieldname the name of the field to display
* @param type the type of the field to display
* @param pref the prefix to add in the field identifier in edit display for example
* @param obj the object containing the field to display
* @param wrappingSyntaxId the syntax of the content in which the result will be included. This to take care of some
* escaping depending of the syntax.
* @param context the XWiki context
* @return the rendered field
*/
public String display(String fieldname, String type, String pref, BaseObject obj, String wrappingSyntaxId, XWikiContext context) {
if (obj == null) {
return "";
}
boolean isInRenderingEngine = BooleanUtils.toBoolean((Boolean) context.get("isInRenderingEngine"));
HashMap<String, Object> backup = new HashMap<String, Object>();
try {
backupContext(backup, context);
setAsContextDoc(context);
// Make sure to execute with the right of the document author instead of the content author
// (because modifying object property does not modify content author)
XWikiDocument sdoc = context.getDoc();
if (sdoc != null && !Objects.equals(sdoc.getContentAuthorReference(), sdoc.getAuthorReference())) {
// Hack the sdoc to make test module believe the content author is the author
sdoc = sdoc.clone();
sdoc.setContentAuthorReference(sdoc.getAuthorReference());
context.put(CKEY_SDOC, sdoc);
}
type = type.toLowerCase();
StringBuffer result = new StringBuffer();
PropertyClass pclass = (PropertyClass) obj.getXClass(context).get(fieldname);
String prefix = pref + LOCAL_REFERENCE_SERIALIZER.serialize(obj.getXClass(context).getDocumentReference()) + "_" + obj.getNumber() + "_";
if (pclass == null) {
return "";
} else if (pclass.isCustomDisplayed(context)) {
pclass.displayCustom(result, fieldname, prefix, type, obj, context);
} else if (type.equals("view")) {
pclass.displayView(result, fieldname, prefix, obj, context);
} else if (type.equals("rendered")) {
String fcontent = pclass.displayView(fieldname, prefix, obj, context);
// Thus for the new rendering we simply make this mode work like the "view" mode.
if (is10Syntax(wrappingSyntaxId)) {
result.append(getRenderedContent(fcontent, getSyntaxId(), context));
} else {
result.append(fcontent);
}
} else if (type.equals("edit")) {
context.addDisplayedField(fieldname);
// escaping. For example for a textarea check the TextAreaClass class.
if (is10Syntax(wrappingSyntaxId)) {
// Don't use pre when not in the rendernig engine since for template we don't evaluate wiki syntax.
if (isInRenderingEngine) {
result.append("{pre}");
}
}
pclass.displayEdit(result, fieldname, prefix, obj, context);
if (is10Syntax(wrappingSyntaxId)) {
if (isInRenderingEngine) {
result.append("{/pre}");
}
}
} else if (type.equals("hidden")) {
// escaping. For example for a textarea check the TextAreaClass class.
if (is10Syntax(wrappingSyntaxId) && isInRenderingEngine) {
result.append("{pre}");
}
pclass.displayHidden(result, fieldname, prefix, obj, context);
if (is10Syntax(wrappingSyntaxId) && isInRenderingEngine) {
result.append("{/pre}");
}
} else if (type.equals("search")) {
// Backward compatibility
// Check if the method has been injected using aspects
Method searchMethod = null;
for (Method method : pclass.getClass().getMethods()) {
if (method.getName().equals("displaySearch") && method.getParameterTypes().length == 5) {
searchMethod = method;
break;
}
}
if (searchMethod != null) {
// escaping. For example for a textarea check the TextAreaClass class.
if (is10Syntax(wrappingSyntaxId) && isInRenderingEngine) {
result.append("{pre}");
}
prefix = LOCAL_REFERENCE_SERIALIZER.serialize(obj.getXClass(context).getDocumentReference()) + "_";
searchMethod.invoke(pclass, result, fieldname, prefix, context.get("query"), context);
if (is10Syntax(wrappingSyntaxId) && isInRenderingEngine) {
result.append("{/pre}");
}
} else {
pclass.displayView(result, fieldname, prefix, obj, context);
}
} else {
pclass.displayView(result, fieldname, prefix, obj, context);
}
// Add the {{html}}{{/html}} only when result really contains html since it's not needed for pure text
if (isInRenderingEngine && !is10Syntax(wrappingSyntaxId) && (result.indexOf("<") != -1 || result.indexOf(">") != -1)) {
result.insert(0, "{{html clean=\"false\" wiki=\"false\"}}");
result.append("{{/html}}");
}
return result.toString();
} catch (Exception ex) {
// TODO: It would better to check if the field exists rather than catching an exception
// raised by a NPE as this is currently the case here...
LOGGER.warn("Failed to display field [" + fieldname + "] in [" + type + "] mode for Object of Class [" + getDefaultEntityReferenceSerializer().serialize(obj.getDocumentReference()) + "]", ex);
return "";
} finally {
restoreContext(backup, context);
}
}
use of com.xpn.xwiki.objects.classes.PropertyClass in project xwiki-platform by xwiki.
the class DefaultDocumentAccessBridge method getPropertyType.
@Override
public String getPropertyType(String className, String propertyName) throws Exception {
XWikiContext xcontext = getContext();
PropertyClass pc = null;
try {
pc = (PropertyClass) xcontext.getWiki().getDocument(className, xcontext).getXClass().get(propertyName);
} catch (XWikiException e) {
this.logger.warn("Failed to get document [{}]. Root cause: [{}]", className, ExceptionUtils.getRootCauseMessage(e));
}
return pc == null ? null : pc.newProperty().getClass().getName();
}
use of com.xpn.xwiki.objects.classes.PropertyClass in project xwiki-platform by xwiki.
the class PropAddAction method action.
@Override
public boolean action(XWikiContext context) throws XWikiException {
// CSRF prevention
if (!csrfTokenCheck(context)) {
return false;
}
XWiki xwiki = context.getWiki();
XWikiResponse response = context.getResponse();
XWikiDocument doc = context.getDoc();
XWikiForm form = context.getForm();
String propName = ((PropAddForm) form).getPropName();
if (!Util.isValidXMLElementName(propName)) {
context.put("message", "action.addClassProperty.error.invalidName");
response.setStatus(HttpServletResponse.SC_BAD_REQUEST, localizePlainOrKey("action.addClassProperty.error.invalidName"));
return true;
}
String propType = ((PropAddForm) form).getPropType();
BaseClass bclass = doc.getXClass();
bclass.setName(doc.getFullName());
if (bclass.get(propName) != null) {
context.put("message", "action.addClassProperty.error.alreadyExists");
List<String> parameters = new ArrayList<String>();
parameters.add(propName);
context.put("messageParameters", parameters);
response.setStatus(HttpServletResponse.SC_BAD_REQUEST, localizePlainOrKey("action.addClassProperty.error.alreadyExists", parameters.toArray()));
return true;
} else {
MetaClass mclass = xwiki.getMetaclass();
PropertyMetaClass pmclass = (PropertyMetaClass) mclass.get(propType);
if (pmclass != null) {
PropertyClass pclass = (PropertyClass) pmclass.newObject(context);
pclass.setObject(bclass);
pclass.setName(propName);
pclass.setPrettyName(propName);
bclass.put(propName, pclass);
doc.setAuthorReference(context.getUserReference());
if (doc.isNew()) {
doc.setCreatorReference(context.getUserReference());
}
doc.setMetaDataDirty(true);
xwiki.saveDocument(doc, localizePlainOrKey("core.comment.addClassProperty"), true, context);
}
}
// forward to edit
String redirect = Utils.getRedirect("edit", "editor=class", context);
sendRedirect(response, redirect);
return false;
}
use of com.xpn.xwiki.objects.classes.PropertyClass in project xwiki-platform by xwiki.
the class PropUpdateAction method propUpdate.
public boolean propUpdate(XWikiContext context) throws XWikiException {
XWiki xwiki = context.getWiki();
XWikiDocument doc = context.getDoc();
XWikiForm form = context.getForm();
// Prepare new class
BaseClass bclass = doc.getXClass();
BaseClass bclass2 = bclass.clone();
bclass2.setFields(new HashMap());
// Prepare a Map for field renames
Map<String, String> fieldsToRename = new HashMap<String, String>();
for (PropertyClass originalProperty : (Collection<PropertyClass>) bclass.getFieldList()) {
PropertyClass newProperty = originalProperty.clone();
String name = newProperty.getName();
Map<String, ?> map = ((EditForm) form).getObject(name);
newProperty.getXClass(context).fromMap(map, newProperty);
String newName = newProperty.getName();
if (!Util.isValidXMLElementName(newName)) {
context.put("message", "propertynamenotcorrect");
return true;
}
if (newName.indexOf(" ") != -1) {
newName = newName.replaceAll(" ", "");
newProperty.setName(newName);
}
bclass2.addField(newName, newProperty);
if (!newName.equals(name)) {
fieldsToRename.put(name, newName);
bclass2.addPropertyForRemoval(originalProperty);
}
}
doc.setXClass(bclass2);
doc.renameProperties(bclass.getName(), fieldsToRename);
doc.setMetaDataDirty(true);
if (doc.isNew()) {
doc.setCreator(context.getUser());
}
doc.setAuthor(context.getUser());
xwiki.saveDocument(doc, localizePlainOrKey("core.comment.updateClassProperty"), true, context);
// We need to load all documents that use this property and rename it
if (fieldsToRename.size() > 0) {
List<String> list = xwiki.getStore().searchDocumentsNames(", BaseObject as obj where obj.name=doc.fullName and obj.className='" + Utils.SQLFilter(bclass.getName()) + "' and doc.fullName <> '" + Utils.SQLFilter(bclass.getName()) + "'", context);
for (String docName : list) {
XWikiDocument doc2 = xwiki.getDocument(docName, context);
doc2.renameProperties(bclass.getName(), fieldsToRename);
xwiki.saveDocument(doc2, localizePlainOrKey("core.comment.updateClassPropertyName"), true, context);
}
}
return false;
}
use of com.xpn.xwiki.objects.classes.PropertyClass in project xwiki-platform by xwiki.
the class AbstractSolrMetadataExtractor method setObjectContent.
/**
* Adds the properties of a given object to a Solr document.
*
* @param solrDocument the document where to add the properties
* @param object the object whose properties to add
* @param locale the locale of the indexed document; in case of translations, this will obviously be different than
* the original document's locale
*/
protected void setObjectContent(SolrInputDocument solrDocument, BaseObject object, Locale locale) {
if (object == null) {
// Yes, the platform can return null objects.
return;
}
BaseClass xClass = object.getXClass(this.xcontextProvider.get());
for (Object field : object.getFieldList()) {
@SuppressWarnings("unchecked") BaseProperty<EntityReference> property = (BaseProperty<EntityReference>) field;
// Avoid indexing empty properties.
if (property.getValue() != null) {
PropertyClass propertyClass = (PropertyClass) xClass.get(property.getName());
setPropertyValue(solrDocument, property, propertyClass, locale);
}
}
}
Aggregations