use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.
the class DefaultIOTargetService method getSourceSyntax.
@Override
public String getSourceSyntax(String reference) throws IOServiceException {
try {
EntityReference ref = referenceResolver.resolve(reference, EntityType.DOCUMENT);
EntityReference docRef = ref.extractReference(EntityType.DOCUMENT);
if (docRef != null) {
// doc
return dab.getTranslatedDocumentInstance(new DocumentReference(docRef)).getSyntax().toIdString();
} else {
return dab.getDocumentSyntaxId(reference);
}
} catch (Exception e) {
throw new IOServiceException("An exception has occurred while getting the syntax of the source for " + reference, e);
}
}
use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.
the class AnnotationEventGeneratorEventListener method getBaseObjectReference.
/**
* @param objectEvent the event involving an object
* @return the {@link BaseObjectReference} of the object corresponding to the object event
*/
private BaseObjectReference getBaseObjectReference(XObjectEvent objectEvent) {
EntityReference objectReference = objectEvent.getReference();
BaseObjectReference baseObjectReference = null;
if (objectReference instanceof BaseObjectReference) {
baseObjectReference = (BaseObjectReference) objectEvent.getReference();
} else {
baseObjectReference = new BaseObjectReference(objectEvent.getReference());
}
return baseObjectReference;
}
use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.
the class DefaultIOService method getAnnotation.
@Override
public Annotation getAnnotation(String target, String annotationID) throws IOServiceException {
try {
if (annotationID == null || target == null) {
return null;
}
// parse the target and extract the local reference serialized from it, by the same rules
EntityReference targetReference = referenceResolver.resolve(target, EntityType.DOCUMENT);
// build the target identifier for the annotation
String localTargetId = target;
// and the name of the document where it should be stored
String docName = target;
if (targetReference.getType() == EntityType.DOCUMENT || targetReference.getType() == EntityType.OBJECT_PROPERTY) {
localTargetId = localSerializer.serialize(targetReference);
docName = serializer.serialize(targetReference.extractReference(EntityType.DOCUMENT));
}
// get the document
XWikiContext deprecatedContext = getXWikiContext();
XWikiDocument document = deprecatedContext.getWiki().getDocument(docName, deprecatedContext);
// and the annotation class objects in it
// parse the annotation id as object index
BaseObject object = document.getXObject(configuration.getAnnotationClassReference(), Integer.valueOf(annotationID.toString()));
if (object == null || !localTargetId.equals(object.getStringValue(Annotation.TARGET_FIELD))) {
return null;
}
// use the object number as annotation id
return loadAnnotationFromObject(object, deprecatedContext);
} catch (NumberFormatException e) {
throw new IOServiceException("Could not parse annotation id " + annotationID, e);
} catch (XWikiException e) {
throw new IOServiceException("An exception has occurred while loading the annotation with id " + annotationID, e);
}
}
use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.
the class DefaultIOService method updateAnnotations.
/**
* {@inheritDoc}
* <p>
* Implementation which gets all the annotation class objects in the document pointed by the target, and matches
* their ids against the ids in the passed collection of annotations. If they match, they are updated with the new
* data in the annotations in annotation.
* </p>
*
* @see org.xwiki.annotation.io.IOService#updateAnnotations(String, java.util.Collection)
*/
@Override
public void updateAnnotations(String target, Collection<Annotation> annotations) throws IOServiceException {
try {
EntityReference targetReference = referenceResolver.resolve(target, EntityType.DOCUMENT);
// get the document name from the parsed reference
String docName = target;
if (targetReference.getType() == EntityType.DOCUMENT || targetReference.getType() == EntityType.OBJECT_PROPERTY) {
docName = serializer.serialize(targetReference.extractReference(EntityType.DOCUMENT));
}
// get the document pointed to by the target
XWikiContext deprecatedContext = getXWikiContext();
XWikiDocument document = deprecatedContext.getWiki().getDocument(docName, deprecatedContext);
List<String> updateNotifs = new ArrayList<String>();
boolean updated = false;
for (Annotation annotation : annotations) {
// parse annotation id as string. If cannot parse, then ignore annotation, is not valid
int annId = 0;
try {
annId = Integer.parseInt(annotation.getId());
} catch (NumberFormatException e) {
continue;
}
BaseObject object = document.getXObject(configuration.getAnnotationClassReference(), annId);
if (object == null) {
continue;
}
updated = updateObject(object, annotation, deprecatedContext) || updated;
updateNotifs.add(annotation.getId());
}
if (updated) {
// set the author of the document to the current user
document.setAuthor(deprecatedContext.getUser());
deprecatedContext.getWiki().saveDocument(document, "Updated annotations", deprecatedContext);
}
} catch (XWikiException e) {
throw new IOServiceException("An exception has occurred while updating the annotation", e);
}
}
use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.
the class IndexedObjectReferenceTest method testObjectNumberWhenIndexNotNumber.
@Test
public void testObjectNumberWhenIndexNotNumber() {
IndexedObjectReference reference = new IndexedObjectReference(new EntityReference("XWiki.Class[number]", EntityType.OBJECT, new EntityReference("Page", EntityType.DOCUMENT, new EntityReference("Space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)))));
assertEquals("XWiki.Class[number]", reference.getClassName());
assertNull(reference.getObjectNumber());
}
Aggregations