use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.
the class RepositoryManager method getExistingExtensionDocumentById.
public XWikiDocument getExistingExtensionDocumentById(String extensionId) throws QueryException, XWikiException {
XWikiContext xcontext = this.xcontextProvider.get();
DocumentReference[] cachedDocumentReference = this.documentReferenceCache.get(extensionId);
if (cachedDocumentReference == null) {
Query query = this.queryManager.createQuery("select doc.fullName from Document doc, doc.object(" + XWikiRepositoryModel.EXTENSION_CLASSNAME + ") as extension where extension." + XWikiRepositoryModel.PROP_EXTENSION_ID + " = :extensionId", Query.XWQL);
query.bindValue("extensionId", extensionId);
List<String> documentNames = query.execute();
if (!documentNames.isEmpty()) {
cachedDocumentReference = new DocumentReference[] { this.currentStringResolver.resolve(documentNames.get(0)) };
} else {
cachedDocumentReference = new DocumentReference[1];
}
this.documentReferenceCache.set(extensionId, cachedDocumentReference);
}
return cachedDocumentReference[0] != null ? xcontext.getWiki().getDocument(cachedDocumentReference[0], xcontext) : null;
}
use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.
the class RepositoryManager method validateExtension.
public void validateExtension(XWikiDocument document, boolean save) throws XWikiException {
BaseObject extensionObject = document.getXObject(XWikiRepositoryModel.EXTENSION_CLASSREFERENCE);
if (extensionObject == null) {
// Not an extension
return;
}
boolean needSave = false;
XWikiContext xcontext = this.xcontextProvider.get();
// Update last version field
String lastVersion = findLastVersion(document);
if (lastVersion != null && !StringUtils.equals(lastVersion, getValue(extensionObject, XWikiRepositoryModel.PROP_EXTENSION_LASTVERSION, (String) null))) {
BaseObject extensionObjectToSave = document.getXObject(extensionObject.getReference());
extensionObjectToSave.set(XWikiRepositoryModel.PROP_EXTENSION_LASTVERSION, lastVersion, xcontext);
needSave = true;
}
// Update valid extension field
boolean valid = isValid(document, extensionObject, xcontext);
if (valid) {
this.logger.debug("The extension in the document [{}] is not valid", document.getDocumentReference());
} else {
this.logger.debug("The extension in the document [{}] is valid", document.getDocumentReference());
}
int currentValue = getValue(extensionObject, XWikiRepositoryModel.PROP_EXTENSION_VALIDEXTENSION, 0);
if ((currentValue == 1) != valid) {
BaseObject extensionObjectToSave = document.getXObject(XWikiRepositoryModel.EXTENSION_CLASSREFERENCE);
extensionObjectToSave.set(XWikiRepositoryModel.PROP_EXTENSION_VALIDEXTENSION, valid ? "1" : "0", xcontext);
needSave = true;
}
if (save && needSave) {
xcontext.getWiki().saveDocument(document, "Validated extension", true, xcontext);
}
}
use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.
the class RepositoryManager method updateExtensionVersionDependencies.
private boolean updateExtensionVersionDependencies(XWikiDocument document, Extension extension) throws XWikiException {
boolean needSave = false;
List<ExtensionDependency> dependencies = new ArrayList<>(extension.getDependencies());
int dependencyIndex = 0;
// Clean misplaced or bad existing dependencies associated to this extension version
List<BaseObject> xobjects = document.getXObjects(XWikiRepositoryModel.EXTENSIONDEPENDENCY_CLASSREFERENCE);
if (xobjects != null) {
boolean deleteExistingObjects = false;
// Clone since we are going to modify and parse it at the same time
xobjects = new ArrayList<>(document.getXObjects(XWikiRepositoryModel.EXTENSIONDEPENDENCY_CLASSREFERENCE));
for (int i = 0; i < xobjects.size(); ++i) {
BaseObject dependencyObject = xobjects.get(i);
if (dependencyObject != null) {
String extensionVersion = getValue(dependencyObject, XWikiRepositoryModel.PROP_DEPENDENCY_EXTENSIONVERSION, (String) null);
if (StringUtils.isNotEmpty(extensionVersion) && extension.getId().getVersion().equals(new DefaultVersion(extensionVersion))) {
if (deleteExistingObjects) {
document.removeXObject(dependencyObject);
needSave = true;
} else {
String xobjectId = getValue(dependencyObject, XWikiRepositoryModel.PROP_DEPENDENCY_ID);
String xobjectConstraint = getValue(dependencyObject, XWikiRepositoryModel.PROP_DEPENDENCY_CONSTRAINT);
List<String> xobjectRepositories = (List<String>) getValue(dependencyObject, XWikiRepositoryModel.PROP_DEPENDENCY_REPOSITORIES);
boolean xobjectOptional = getValue(dependencyObject, XWikiRepositoryModel.PROP_DEPENDENCY_OPTIONAL, 0) == 1;
if (dependencies.size() > dependencyIndex) {
ExtensionDependency dependency = dependencies.get(dependencyIndex);
DefaultExtensionDependency xobjectDependency = new DefaultExtensionDependency(xobjectId, new DefaultVersionConstraint(xobjectConstraint), xobjectOptional, dependency.getProperties());
xobjectDependency.setRepositories(XWikiRepositoryModel.toRepositoryDescriptors(xobjectRepositories, this.extensionFactory));
if (dependency.equals(xobjectDependency)) {
++dependencyIndex;
continue;
}
}
deleteExistingObjects = true;
document.removeXObject(dependencyObject);
needSave = true;
}
}
}
}
}
// Add missing dependencies
if (dependencyIndex < dependencies.size()) {
XWikiContext xcontext = this.xcontextProvider.get();
for (; dependencyIndex < dependencies.size(); ++dependencyIndex) {
ExtensionDependency dependency = dependencies.get(dependencyIndex);
BaseObject dependencyObject = document.newXObject(XWikiRepositoryModel.EXTENSIONDEPENDENCY_CLASSREFERENCE, xcontext);
dependencyObject.set(XWikiRepositoryModel.PROP_DEPENDENCY_EXTENSIONVERSION, extension.getId().getVersion().getValue(), xcontext);
dependencyObject.set(XWikiRepositoryModel.PROP_DEPENDENCY_ID, dependency.getId(), xcontext);
dependencyObject.set(XWikiRepositoryModel.PROP_DEPENDENCY_CONSTRAINT, dependency.getVersionConstraint().getValue(), xcontext);
dependencyObject.set(XWikiRepositoryModel.PROP_DEPENDENCY_OPTIONAL, dependency.isOptional() ? 1 : 0, xcontext);
dependencyObject.set(XWikiRepositoryModel.PROP_DEPENDENCY_REPOSITORIES, XWikiRepositoryModel.toStringList(dependency.getRepositories()), xcontext);
needSave = true;
}
}
return needSave;
}
use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.
the class ModelFactory method toRestObject.
public Object toRestObject(URI baseUri, Document doc, BaseObject xwikiObject, boolean useVersion, Boolean withPrettyNames) {
Object object = this.objectFactory.createObject();
fillObjectSummary(object, doc, xwikiObject, withPrettyNames);
XWikiContext xwikiContext = this.xcontextProvider.get();
BaseClass xwikiClass = xwikiObject.getXClass(xwikiContext);
for (java.lang.Object propertyClassObject : xwikiClass.getProperties()) {
com.xpn.xwiki.objects.classes.PropertyClass propertyClass = (com.xpn.xwiki.objects.classes.PropertyClass) propertyClassObject;
Property property = this.objectFactory.createProperty();
for (java.lang.Object o : propertyClass.getProperties()) {
BaseProperty baseProperty = (BaseProperty) o;
Attribute attribute = this.objectFactory.createAttribute();
attribute.setName(baseProperty.getName());
/* Check for null values in order to prevent NPEs */
if (baseProperty.getValue() != null) {
attribute.setValue(baseProperty.getValue().toString());
} else {
attribute.setValue("");
}
property.getAttributes().add(attribute);
}
if (propertyClass instanceof ListClass) {
ListClass listClass = (ListClass) propertyClass;
List allowedValueList = listClass.getList(xwikiContext);
if (!allowedValueList.isEmpty()) {
Formatter f = new Formatter();
for (int i = 0; i < allowedValueList.size(); i++) {
if (i != allowedValueList.size() - 1) {
f.format("%s,", allowedValueList.get(i).toString());
} else {
f.format("%s", allowedValueList.get(i).toString());
}
}
Attribute attribute = this.objectFactory.createAttribute();
attribute.setName(Constants.ALLOWED_VALUES_ATTRIBUTE_NAME);
attribute.setValue(f.toString());
property.getAttributes().add(attribute);
}
}
property.setName(propertyClass.getName());
property.setType(propertyClass.getClassType());
try {
property.setValue(serializePropertyValue(xwikiObject.get(propertyClass.getName())));
} catch (XWikiException e) {
// Should never happen
}
String propertyUri;
if (useVersion) {
propertyUri = Utils.createURI(baseUri, ObjectPropertyAtPageVersionResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), doc.getVersion(), xwikiObject.getClassName(), xwikiObject.getNumber(), propertyClass.getName()).toString();
} else {
propertyUri = Utils.createURI(baseUri, ObjectPropertyResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), xwikiObject.getClassName(), xwikiObject.getNumber(), propertyClass.getName()).toString();
}
Link propertyLink = this.objectFactory.createLink();
propertyLink.setHref(propertyUri);
propertyLink.setRel(Relations.SELF);
property.getLinks().add(propertyLink);
object.getProperties().add(property);
}
Link objectLink = getObjectLink(this.objectFactory, baseUri, doc, xwikiObject, useVersion, Relations.SELF);
object.getLinks().add(objectLink);
return object;
}
use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.
the class ModelFactory method toRestAttachment.
public Attachment toRestAttachment(URI baseUri, com.xpn.xwiki.api.Attachment xwikiAttachment, String xwikiRelativeUrl, String xwikiAbsoluteUrl, Boolean withPrettyNames, boolean versionURL) {
Attachment attachment = this.objectFactory.createAttachment();
Document doc = xwikiAttachment.getDocument();
attachment.setId(String.format("%s@%s", doc.getPrefixedFullName(), xwikiAttachment.getFilename()));
attachment.setName(xwikiAttachment.getFilename());
attachment.setSize(xwikiAttachment.getFilesize());
attachment.setVersion(xwikiAttachment.getVersion());
attachment.setPageId(doc.getPrefixedFullName());
attachment.setPageVersion(doc.getVersion());
attachment.setMimeType(xwikiAttachment.getMimeType());
attachment.setAuthor(xwikiAttachment.getAuthor());
if (withPrettyNames) {
XWikiContext xcontext = xcontextProvider.get();
attachment.setAuthorName(xcontext.getWiki().getUserName(xwikiAttachment.getAuthor(), null, false, xcontext));
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(xwikiAttachment.getDate());
attachment.setDate(calendar);
attachment.setXwikiRelativeUrl(xwikiRelativeUrl);
attachment.setXwikiAbsoluteUrl(xwikiAbsoluteUrl);
String pageUri = Utils.createURI(baseUri, PageResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName()).toString();
Link pageLink = objectFactory.createLink();
pageLink.setHref(pageUri);
pageLink.setRel(Relations.PAGE);
attachment.getLinks().add(pageLink);
String attachmentUri;
if (versionURL) {
attachmentUri = Utils.createURI(baseUri, AttachmentVersionResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), xwikiAttachment.getFilename(), xwikiAttachment.getVersion()).toString();
} else {
attachmentUri = Utils.createURI(baseUri, AttachmentResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), xwikiAttachment.getFilename()).toString();
}
Link attachmentLink = objectFactory.createLink();
attachmentLink.setHref(attachmentUri);
attachmentLink.setRel(Relations.ATTACHMENT_DATA);
attachment.getLinks().add(attachmentLink);
return attachment;
}
Aggregations