Search in sources :

Example 81 with EntityReference

use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.

the class XWiki method getPreferencesDocumentReference.

/**
 * Return the document reference to the wiki preferences.
 *
 * @param context see {@link XWikiContext}
 * @since 4.3M2
 */
private DocumentReference getPreferencesDocumentReference(XWikiContext context) {
    String database = context.getWikiId();
    EntityReference spaceReference;
    if (database != null) {
        spaceReference = new EntityReference(SYSTEM_SPACE, EntityType.SPACE, new WikiReference(database));
    } else {
        spaceReference = getCurrentMixedEntityReferenceResolver().resolve(SYSTEM_SPACE, EntityType.SPACE);
    }
    return new DocumentReference("XWikiPreferences", new SpaceReference(spaceReference));
}
Also used : SpaceReference(org.xwiki.model.reference.SpaceReference) EntityReference(org.xwiki.model.reference.EntityReference) RegexEntityReference(org.xwiki.model.reference.RegexEntityReference) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference)

Example 82 with EntityReference

use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.

the class XWikiHibernateStore method loadXWikiDoc.

@Override
public XWikiDocument loadXWikiDoc(XWikiDocument doc, XWikiContext inputxcontext) throws XWikiException {
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    try {
        // To change body of implemented methods use Options | File Templates.
        boolean bTransaction = true;
        MonitorPlugin monitor = Util.getMonitorPlugin(context);
        try {
            // Start monitoring timer
            if (monitor != null) {
                monitor.startTimer(HINT);
            }
            doc.setStore(this);
            checkHibernate(context);
            SessionFactory sfactory = injectCustomMappingsInSessionFactory(doc, context);
            bTransaction = bTransaction && beginTransaction(sfactory, context);
            Session session = getSession(context);
            session.setFlushMode(FlushMode.MANUAL);
            try {
                session.load(doc, Long.valueOf(doc.getId()));
                doc.setNew(false);
                doc.setMostRecent(true);
                // Fix for XWIKI-1651
                doc.setDate(new Date(doc.getDate().getTime()));
                doc.setCreationDate(new Date(doc.getCreationDate().getTime()));
                doc.setContentUpdateDate(new Date(doc.getContentUpdateDate().getTime()));
            } catch (ObjectNotFoundException e) {
                // No document
                doc.setNew(true);
                // Make sure to always return a document with an original version, even for one that does not exist.
                // Allow writing more generic code.
                doc.setOriginalDocument(new XWikiDocument(doc.getDocumentReference(), doc.getLocale()));
                return doc;
            }
            // Loading the attachment list
            if (doc.hasElement(XWikiDocument.HAS_ATTACHMENTS)) {
                loadAttachmentList(doc, context, false);
            }
            // TODO: handle the case where there are no xWikiClass and xWikiObject in the Database
            BaseClass bclass = new BaseClass();
            String cxml = doc.getXClassXML();
            if (cxml != null) {
                bclass.fromXML(cxml);
                doc.setXClass(bclass);
                bclass.setDirty(false);
            }
            // Store this XWikiClass in the context so that we can use it in case of recursive usage
            // of classes
            context.addBaseClass(bclass);
            if (doc.hasElement(XWikiDocument.HAS_OBJECTS)) {
                Query query = session.createQuery("from BaseObject as bobject where bobject.name = :name order by bobject.number");
                query.setText("name", doc.getFullName());
                @SuppressWarnings("unchecked") Iterator<BaseObject> it = query.list().iterator();
                EntityReference localGroupEntityReference = new EntityReference("XWikiGroups", EntityType.DOCUMENT, new EntityReference("XWiki", EntityType.SPACE));
                DocumentReference groupsDocumentReference = new DocumentReference(context.getWikiId(), localGroupEntityReference.getParent().getName(), localGroupEntityReference.getName());
                boolean hasGroups = false;
                while (it.hasNext()) {
                    BaseObject object = it.next();
                    DocumentReference classReference = object.getXClassReference();
                    if (classReference == null) {
                        continue;
                    }
                    // object which doesn't really belong to this document
                    if (!object.getDocumentReference().equals(doc.getDocumentReference())) {
                        continue;
                    }
                    BaseObject newobject;
                    if (classReference.equals(doc.getDocumentReference())) {
                        newobject = bclass.newCustomClassInstance(context);
                    } else {
                        newobject = BaseClass.newCustomClassInstance(classReference, context);
                    }
                    if (newobject != null) {
                        newobject.setId(object.getId());
                        newobject.setXClassReference(object.getRelativeXClassReference());
                        newobject.setDocumentReference(object.getDocumentReference());
                        newobject.setNumber(object.getNumber());
                        newobject.setGuid(object.getGuid());
                        object = newobject;
                    }
                    if (classReference.equals(groupsDocumentReference)) {
                        // Groups objects are handled differently.
                        hasGroups = true;
                    } else {
                        loadXWikiCollectionInternal(object, doc, context, false, true);
                    }
                    doc.setXObject(object.getNumber(), object);
                }
                // This will do every group member in a single query.
                if (hasGroups) {
                    Query query2 = session.createQuery("select bobject.number, prop.value from StringProperty as prop," + "BaseObject as bobject where bobject.name = :name and bobject.className='XWiki.XWikiGroups' " + "and bobject.id=prop.id.id and prop.id.name='member' order by bobject.number");
                    query2.setText("name", doc.getFullName());
                    @SuppressWarnings("unchecked") Iterator<Object[]> it2 = query2.list().iterator();
                    while (it2.hasNext()) {
                        Object[] result = it2.next();
                        Integer number = (Integer) result[0];
                        String member = (String) result[1];
                        BaseObject obj = BaseClass.newCustomClassInstance(groupsDocumentReference, context);
                        obj.setDocumentReference(doc.getDocumentReference());
                        obj.setXClassReference(localGroupEntityReference);
                        obj.setNumber(number.intValue());
                        obj.setStringValue("member", member);
                        doc.setXObject(obj.getNumber(), obj);
                    }
                }
            }
            doc.setContentDirty(false);
            doc.setMetaDataDirty(false);
            // We need to ensure that the loaded document becomes the original document
            doc.setOriginalDocument(doc.clone());
            if (bTransaction) {
                endTransaction(context, false);
            }
        } catch (Exception e) {
            Object[] args = { doc.getDocumentReference() };
            throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_READING_DOC, "Exception while reading document [{0}]", e, args);
        } finally {
            try {
                if (bTransaction) {
                    endTransaction(context, false);
                }
            } catch (Exception e) {
            }
            // End monitoring timer
            if (monitor != null) {
                monitor.endTimer(HINT);
            }
        }
        this.logger.debug("Loaded XWikiDocument: [{}]", doc.getDocumentReference());
        return doc;
    } finally {
        restoreExecutionXContext();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Query(org.hibernate.Query) MonitorPlugin(com.xpn.xwiki.monitor.api.MonitorPlugin) XWikiContext(com.xpn.xwiki.XWikiContext) Date(java.util.Date) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) MigrationRequiredException(com.xpn.xwiki.store.migration.MigrationRequiredException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) QueryException(org.xwiki.query.QueryException) UnexpectedException(org.xwiki.store.UnexpectedException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) SQLException(java.sql.SQLException) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) EntityReference(org.xwiki.model.reference.EntityReference) BaseObject(com.xpn.xwiki.objects.BaseObject) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Example 83 with EntityReference

use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.

the class XWikiStats method getLocalKey.

@Override
protected String getLocalKey() {
    StringBuilder sb = new StringBuilder(64);
    // The R40000XWIKI6990DataMigration use a stubbed class to provide the above two values. If the ids depends
    // on other non-static requirements, it should be adapted accordingly.
    String name = getName();
    int nb = getNumber();
    if (!StringUtils.isEmpty(name)) {
        // TODO: Refactor to get the original reference and fix the confusion when a space contains escaped chars
        EntityReference ref = this.resolver.resolve(name, EntityType.DOCUMENT);
        if (ref.getName().equals(name)) {
            ref = new EntityReference(name, EntityType.SPACE);
        }
        sb.append(getLocalUidStringEntityReferenceSerializer().serialize(ref));
    }
    // and it will not hurt anyway.
    if (nb != 0) {
        // TODO: Avoid the hashed number, and use the original info (referer, period, etc...)
        String str = Integer.toString(nb);
        sb.append(str.length()).append(':').append(str);
    }
    return sb.toString();
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference)

Example 84 with EntityReference

use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.

the class RightsManager method removeUserOrGroupFromRights.

/**
 * Remove all references to provided user or group from provided rights document.
 *
 * @param rightsDocument the document containing the rights preferences.
 * @param userOrGroupWiki the name of the wiki of the use or group.
 * @param userOrGroupSpace the name of the space of the use or group.
 * @param userOrGroupName the name of the use or group.
 * @param user indicate if it is a user or a group.
 * @param global indicate if user or group is removed from global or document rights.
 * @param context the XWiki context.
 * @return true if user or group has been found and removed.
 */
public boolean removeUserOrGroupFromRights(XWikiDocument rightsDocument, String userOrGroupWiki, String userOrGroupSpace, String userOrGroupName, boolean user, boolean global, XWikiContext context) {
    boolean needUpdate = false;
    EntityReference rightClassReference = global ? XWikiRightServiceImpl.GLOBALRIGHTCLASS_REFERENCE : XWikiRightServiceImpl.RIGHTCLASS_REFERENCE;
    List<BaseObject> rightObjects = rightsDocument.getXObjects(rightClassReference);
    if (rightObjects != null) {
        for (BaseObject bobj : rightObjects) {
            if (bobj == null) {
                continue;
            }
            needUpdate |= removeUserOrGroupFromRight(bobj, userOrGroupWiki, userOrGroupSpace, userOrGroupName, user, context);
            if (needUpdate && bobj.getStringValue(RIGHTSFIELD_USERS).trim().length() == 0 && bobj.getStringValue(RIGHTSFIELD_GROUPS).trim().length() == 0) {
                rightsDocument.removeXObject(bobj);
            }
        }
    }
    return needUpdate;
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 85 with EntityReference

use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.

the class RightsManager method fillLevelTreeMap.

/**
 * Fill the {@link LevelTree} {@link Map}.
 *
 * @param rightsMap the {@link LevelTree} {@link Map} to fill.
 * @param preferences the document containing rights preferences.
 * @param levelsToMatch the levels names to check ("view", "edit", etc.).
 * @param global indicate it is global rights (wiki or space) or document rights.
 * @param direct if true fill the {@link LevelTree#direct} field, otherwise fill the {@link LevelTree#inherited}.
 * @param context the XWiki context.
 * @throws XWikiException error when browsing rights preferences.
 */
private void fillLevelTreeMap(Map<String, LevelTree> rightsMap, XWikiDocument preferences, List<String> levelsToMatch, boolean global, boolean direct, XWikiContext context) throws XWikiException {
    List<String> levelInherited = null;
    if (levelsToMatch == null) {
        levelInherited = new ArrayList<String>();
    }
    if (!preferences.isNew()) {
        EntityReference rightClassReference = global ? XWikiRightServiceImpl.GLOBALRIGHTCLASS_REFERENCE : XWikiRightServiceImpl.RIGHTCLASS_REFERENCE;
        List<BaseObject> rightObjects = preferences.getXObjects(rightClassReference);
        if (rightObjects != null) {
            for (BaseObject bobj : rightObjects) {
                fillLevelTreeMap(rightsMap, levelInherited, bobj, levelsToMatch, direct, context);
            }
        }
    }
    fillLevelTreeMapInherited(rightsMap, levelInherited, preferences, levelsToMatch, global, context);
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Aggregations

EntityReference (org.xwiki.model.reference.EntityReference)338 Test (org.junit.Test)157 DocumentReference (org.xwiki.model.reference.DocumentReference)107 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)39 BaseObject (com.xpn.xwiki.objects.BaseObject)38 ArrayList (java.util.ArrayList)27 XWikiContext (com.xpn.xwiki.XWikiContext)24 WikiReference (org.xwiki.model.reference.WikiReference)24 SpaceReference (org.xwiki.model.reference.SpaceReference)23 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)18 XWikiException (com.xpn.xwiki.XWikiException)17 EntityType (org.xwiki.model.EntityType)11 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)10 EntityReferenceProvider (org.xwiki.model.reference.EntityReferenceProvider)9 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)8 XDOM (org.xwiki.rendering.block.XDOM)8 URL (java.net.URL)7 AttachmentReference (org.xwiki.model.reference.AttachmentReference)7 HashMap (java.util.HashMap)6 Before (org.junit.Before)6