Search in sources :

Example 11 with PropertyClass

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);
    }
}
Also used : HashMap(java.util.HashMap) BaseObject(com.xpn.xwiki.objects.BaseObject) ToString(org.suigeneris.jrcs.util.ToString) Method(java.lang.reflect.Method) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass) XWikiException(com.xpn.xwiki.XWikiException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DifferentiationFailedException(org.suigeneris.jrcs.diff.DifferentiationFailedException) ExecutionContextException(org.xwiki.context.ExecutionContextException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) MissingParserException(org.xwiki.rendering.parser.MissingParserException) IOException(java.io.IOException) ParseException(org.xwiki.rendering.parser.ParseException) TransformationException(org.xwiki.rendering.transformation.TransformationException) QueryException(org.xwiki.query.QueryException)

Example 12 with PropertyClass

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();
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass) XWikiException(com.xpn.xwiki.XWikiException)

Example 13 with PropertyClass

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;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) PropertyMetaClass(com.xpn.xwiki.objects.meta.PropertyMetaClass) MetaClass(com.xpn.xwiki.objects.meta.MetaClass) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) ArrayList(java.util.ArrayList) XWiki(com.xpn.xwiki.XWiki) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass) PropertyMetaClass(com.xpn.xwiki.objects.meta.PropertyMetaClass)

Example 14 with PropertyClass

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;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) HashMap(java.util.HashMap) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWiki(com.xpn.xwiki.XWiki) Collection(java.util.Collection) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass)

Example 15 with PropertyClass

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);
        }
    }
}
Also used : BaseClass(com.xpn.xwiki.objects.classes.BaseClass) EntityReference(org.xwiki.model.reference.EntityReference) BaseObject(com.xpn.xwiki.objects.BaseObject) BaseProperty(com.xpn.xwiki.objects.BaseProperty) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass)

Aggregations

PropertyClass (com.xpn.xwiki.objects.classes.PropertyClass)28 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)16 XWikiException (com.xpn.xwiki.XWikiException)8 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)7 BaseObject (com.xpn.xwiki.objects.BaseObject)5 BaseProperty (com.xpn.xwiki.objects.BaseProperty)5 ToString (org.suigeneris.jrcs.util.ToString)5 XWiki (com.xpn.xwiki.XWiki)4 ArrayList (java.util.ArrayList)4 QueryException (org.xwiki.query.QueryException)4 XWikiContext (com.xpn.xwiki.XWikiContext)3 IOException (java.io.IOException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 DifferentiationFailedException (org.suigeneris.jrcs.diff.DifferentiationFailedException)3 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)3 ExecutionContextException (org.xwiki.context.ExecutionContextException)3 MissingParserException (org.xwiki.rendering.parser.MissingParserException)3 ParseException (org.xwiki.rendering.parser.ParseException)3 TransformationException (org.xwiki.rendering.transformation.TransformationException)3 HashMap (java.util.HashMap)2