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));
}
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();
}
}
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();
}
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;
}
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);
}
Aggregations