use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class RepositoryManager method importExtension.
public DocumentReference importExtension(String extensionId, ExtensionRepository repository, Type type) throws QueryException, XWikiException, ResolveException {
TreeMap<Version, String> versions = new TreeMap<Version, String>();
Version lastVersion = getVersions(extensionId, repository, type, versions);
if (lastVersion == null) {
throw new ExtensionNotFoundException("Can't find any version for the extension [" + extensionId + "] on repository [" + repository + "]");
} else if (versions.isEmpty()) {
// If no valid version import the last version
versions.put(lastVersion, extensionId);
} else {
// Select the last valid version
lastVersion = versions.lastKey();
}
Extension extension = repository.resolve(new ExtensionId(extensionId, lastVersion));
// Get former ids versions
Collection<ExtensionId> features = extension.getExtensionFeatures();
for (ExtensionId feature : features) {
try {
getVersions(feature.getId(), repository, type, versions);
} catch (ResolveException e) {
// Ignore
}
}
XWikiContext xcontext = this.xcontextProvider.get();
boolean needSave = false;
XWikiDocument document = getExistingExtensionDocumentById(extensionId);
if (document == null) {
// Create document
document = xcontext.getWiki().getDocument(new DocumentReference(xcontext.getWikiId(), Arrays.asList("Extension", extension.getName()), "WebHome"), xcontext);
for (int i = 1; !document.isNew(); ++i) {
document = xcontext.getWiki().getDocument(new DocumentReference(xcontext.getWikiId(), Arrays.asList("Extension", extension.getName() + ' ' + i), "WebHome"), xcontext);
}
document.readFromTemplate(this.currentResolver.resolve(XWikiRepositoryModel.EXTENSION_TEMPLATEREFERENCE), xcontext);
needSave = true;
}
// Update document
BaseObject extensionObject = document.getXObject(XWikiRepositoryModel.EXTENSION_CLASSREFERENCE);
if (extensionObject == null) {
extensionObject = document.newXObject(XWikiRepositoryModel.EXTENSION_CLASSREFERENCE, xcontext);
needSave = true;
}
if (!StringUtils.equals(extensionId, getValue(extensionObject, XWikiRepositoryModel.PROP_EXTENSION_ID, (String) null))) {
extensionObject.set(XWikiRepositoryModel.PROP_EXTENSION_ID, extensionId, xcontext);
needSave = true;
}
// Update extension informations
needSave |= updateExtension(extension, extensionObject, xcontext);
// Proxy marker
BaseObject extensionProxyObject = document.getXObject(XWikiRepositoryModel.EXTENSIONPROXY_CLASSREFERENCE);
if (extensionProxyObject == null) {
extensionProxyObject = document.newXObject(XWikiRepositoryModel.EXTENSIONPROXY_CLASSREFERENCE, xcontext);
extensionProxyObject.setIntValue(XWikiRepositoryModel.PROP_PROXY_AUTOUPDATE, 1);
needSave = true;
}
needSave |= update(extensionProxyObject, XWikiRepositoryModel.PROP_PROXY_REPOSITORYID, repository.getDescriptor().getId());
needSave |= update(extensionProxyObject, XWikiRepositoryModel.PROP_PROXY_REPOSITORYTYPE, repository.getDescriptor().getType());
needSave |= update(extensionProxyObject, XWikiRepositoryModel.PROP_PROXY_REPOSITORYURI, repository.getDescriptor().getURI().toString());
// Remove unexisting versions
Set<String> validVersions = new HashSet<String>();
List<BaseObject> versionObjects = document.getXObjects(XWikiRepositoryModel.EXTENSIONVERSION_CLASSREFERENCE);
if (versionObjects != null) {
for (BaseObject versionObject : versionObjects) {
if (versionObject != null) {
String version = getValue(versionObject, XWikiRepositoryModel.PROP_VERSION_VERSION);
if (StringUtils.isBlank(version) || (isVersionProxyingEnabled(document) && !new DefaultVersion(version).equals(extension.getId().getVersion()))) {
// Empty version OR old versions should be proxied
document.removeXObject(versionObject);
needSave = true;
} else {
if (!versions.containsKey(new DefaultVersion(version))) {
// The version does not exist on remote repository
if (!isVersionValid(document, versionObject, xcontext)) {
// The version is invalid, removing it to not make the whole extension invalid
document.removeXObject(versionObject);
needSave = true;
} else {
// The version is valid, lets keep it
validVersions.add(version);
}
} else {
// This version exist on remote repository
validVersions.add(version);
}
}
}
}
}
List<BaseObject> dependencyObjects = document.getXObjects(XWikiRepositoryModel.EXTENSIONDEPENDENCY_CLASSREFERENCE);
if (dependencyObjects != null) {
for (BaseObject dependencyObject : dependencyObjects) {
if (dependencyObject != null) {
String version = getValue(dependencyObject, XWikiRepositoryModel.PROP_DEPENDENCY_EXTENSIONVERSION);
if (!validVersions.contains(version)) {
// The version is invalid, removing it to not make the whole extension invalid
document.removeXObject(dependencyObject);
needSave = true;
}
}
}
}
for (Map.Entry<Version, String> entry : versions.entrySet()) {
Version version = entry.getKey();
String id = entry.getValue();
try {
Extension versionExtension;
if (version.equals(extension.getId().getVersion())) {
versionExtension = extension;
} else if (isVersionProxyingEnabled(document)) {
continue;
} else {
versionExtension = repository.resolve(new ExtensionId(id, version));
}
// Update version related informations
needSave |= updateExtensionVersion(document, versionExtension);
} catch (Exception e) {
this.logger.error("Failed to resolve extension with id [" + id + "] and version [" + version + "] on repository [" + repository + "]", e);
}
}
if (needSave) {
document.setAuthorReference(xcontext.getUserReference());
if (document.isNew()) {
document.setContentAuthorReference(xcontext.getUserReference());
document.setCreatorReference(xcontext.getUserReference());
}
xcontext.getWiki().saveDocument(document, "Imported extension [" + extensionId + "] from repository [" + repository.getDescriptor() + "]", true, xcontext);
}
return document.getDocumentReference();
}
use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class RepositoryManager method getExtensionVersionObject.
/**
* @since 9.5RC1
*/
public BaseObject getExtensionVersionObject(XWikiDocument extensionDocument, String version, boolean allowProxying) {
if (version == null) {
List<BaseObject> objects = extensionDocument.getXObjects(XWikiRepositoryModel.EXTENSIONVERSION_CLASSREFERENCE);
if (objects == null || objects.isEmpty()) {
return null;
} else {
return objects.get(objects.size() - 1);
}
}
BaseObject extensionVersionObject = extensionDocument.getObject(XWikiRepositoryModel.EXTENSIONVERSION_CLASSNAME, "version", version, false);
if (extensionVersionObject == null && allowProxying && isVersionProxyingEnabled(extensionDocument)) {
// no ExtensionVersionClass object so we need to create such object temporarily and delete it
try {
// FIXME - see XWIKI-14138 - this is nasty hack for obtaining ExtensionVersion XObject, when its
// FIXME information should be proxied and not stored permanently behind extension document
// FIXME To be substitude by some better solution in the future
XWikiDocument extensionDocumentClone = extensionDocument.clone();
addExtensionVersionObjectToDocument(extensionDocumentClone, version);
extensionVersionObject = extensionDocumentClone.getObject(XWikiRepositoryModel.EXTENSIONVERSION_CLASSNAME, "version", version, false);
} catch (XWikiException | ResolveException e) {
throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
}
}
return extensionVersionObject;
}
use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class ModelFactory method toRestPage.
public Page toRestPage(URI baseUri, URI self, Document doc, boolean useVersion, Boolean withPrettyNames, Boolean withObjects, Boolean withXClass, Boolean withAttachments) throws XWikiException {
Page page = this.objectFactory.createPage();
toRestPageSummary(page, baseUri, doc, useVersion, withPrettyNames);
XWikiContext xwikiContext = this.xcontextProvider.get();
page.setMajorVersion(doc.getRCSVersion().at(0));
page.setMinorVersion(doc.getRCSVersion().at(1));
page.setHidden(doc.isHidden());
page.setLanguage(doc.getLocale().toString());
page.setCreator(doc.getCreator());
if (withPrettyNames) {
page.setCreatorName(xwikiContext.getWiki().getUserName(doc.getCreator(), null, false, xwikiContext));
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(doc.getCreationDate());
page.setCreated(calendar);
page.setModifier(doc.getContentAuthor());
if (withPrettyNames) {
page.setModifierName(xwikiContext.getWiki().getUserName(doc.getContentAuthor(), null, false, xwikiContext));
}
calendar = Calendar.getInstance();
calendar.setTime(doc.getContentUpdateDate());
page.setModified(calendar);
page.setComment(doc.getComment());
page.setContent(doc.getContent());
if (self != null) {
Link pageLink = this.objectFactory.createLink();
pageLink.setHref(self.toString());
pageLink.setRel(Relations.SELF);
page.getLinks().add(pageLink);
}
com.xpn.xwiki.api.Class xwikiClass = doc.getxWikiClass();
if (xwikiClass != null) {
String classUri = Utils.createURI(baseUri, ClassResource.class, doc.getWiki(), xwikiClass.getName()).toString();
Link classLink = this.objectFactory.createLink();
classLink.setHref(classUri);
classLink.setRel(Relations.CLASS);
page.getLinks().add(classLink);
}
XWikiContext xcontext = xcontextProvider.get();
// Add attachments
if (withAttachments) {
page.setAttachments(objectFactory.createAttachments());
for (com.xpn.xwiki.api.Attachment attachment : doc.getAttachmentList()) {
URL url = xcontext.getURLFactory().createAttachmentURL(attachment.getFilename(), doc.getSpace(), doc.getName(), "download", null, doc.getWiki(), xcontext);
String attachmentXWikiAbsoluteUrl = url.toString();
String attachmentXWikiRelativeUrl = xcontext.getURLFactory().getURL(url, xcontext);
page.getAttachments().getAttachments().add(toRestAttachment(baseUri, attachment, attachmentXWikiRelativeUrl, attachmentXWikiAbsoluteUrl, withPrettyNames, false));
}
}
// Add objects
if (withObjects) {
page.setObjects(objectFactory.createObjects());
XWikiDocument xwikiDocument = xcontext.getWiki().getDocument(doc.getDocumentReference(), xcontext);
for (List<BaseObject> objects : xwikiDocument.getXObjects().values()) {
for (BaseObject object : objects) {
// Deleting an object leads to a null entry in the list of objects.
if (object != null) {
page.getObjects().getObjectSummaries().add(toRestObject(baseUri, doc, object, false, withPrettyNames));
}
}
}
}
// Add xclass
if (withXClass) {
page.setClazz(toRestClass(baseUri, doc.getxWikiClass()));
}
return page;
}
use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class Utils method getPageFullName.
/**
* Get the page full name given its components.
*
* @param wikiName
* @param spaces
* @param pageName
* @return The page full name.
*/
public static String getPageFullName(String wikiName, List<String> spaces, String pageName) {
XWikiDocument xwikiDocument = new XWikiDocument(new DocumentReference(wikiName, spaces, pageName));
Document document = new Document(xwikiDocument, null);
return document.getFullName();
}
use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class Utils method getObjectId.
/**
* Get the object id given its components.
*
* @param wikiName
* @param spaces
* @param pageName
* @param className
* @param objectNumber
* @return The object id.
*/
public static String getObjectId(String wikiName, List<String> spaces, String pageName, String className, int objectNumber) {
XWikiDocument xwikiDocument = new XWikiDocument(new DocumentReference(wikiName, spaces, pageName));
Document document = new Document(xwikiDocument, null);
return String.format("%s:%s[%d]", document.getPrefixedFullName(), className, objectNumber);
}
Aggregations