use of org.xwiki.extension.version.internal.DefaultVersion 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 org.xwiki.extension.version.internal.DefaultVersion in project xwiki-platform by xwiki.
the class RepositoryManager method findLastVersion.
/**
* Compare all version located in a document to find the last one.
*
* @param document the extension document
* @return the last version
*/
private String findLastVersion(XWikiDocument document) {
DocumentReference versionClassReference = getClassReference(document, XWikiRepositoryModel.EXTENSIONVERSION_CLASSREFERENCE);
List<BaseObject> versionObjects = document.getXObjects(versionClassReference);
DefaultVersion lastVersion = null;
if (versionObjects != null) {
for (BaseObject versionObject : versionObjects) {
if (versionObject != null) {
String versionString = getValue(versionObject, XWikiRepositoryModel.PROP_VERSION_VERSION);
if (versionString != null) {
DefaultVersion version = new DefaultVersion(versionString);
if (lastVersion == null || version.compareTo(lastVersion) > 0) {
lastVersion = version;
}
}
}
}
}
return lastVersion != null ? lastVersion.getValue() : null;
}
use of org.xwiki.extension.version.internal.DefaultVersion 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 org.xwiki.extension.version.internal.DefaultVersion in project xwiki-platform by xwiki.
the class DefaultDistributionManager method initialize.
@Override
public void initialize() throws InitializationException {
// Get default UI from the configuration.
this.mainUIExtensionId = this.configuration.getProperty("distribution.defaultUI", ExtensionId.class);
this.wikiUIExtensionId = this.configuration.getProperty("distribution.defaultWikiUI", ExtensionId.class);
// Get the current distribution
this.distributionExtension = this.coreExtensionRepository.getEnvironmentExtension();
// Extract various configuration from the distribution extension
if (this.distributionExtension != null) {
// Main wiki default UI
if (this.mainUIExtensionId == null) {
String mainUIId = this.distributionExtension.getProperty("xwiki.extension.distribution.ui");
if (mainUIId != null) {
String mainUIVersion = this.distributionExtension.getProperty("xwiki.extension.distribution.ui.version");
this.mainUIExtensionId = new ExtensionId(mainUIId, mainUIVersion != null ? new DefaultVersion(mainUIVersion) : this.distributionExtension.getId().getVersion());
}
} else if (this.mainUIExtensionId.getVersion() == null) {
this.mainUIExtensionId = new ExtensionId(this.mainUIExtensionId.getId(), this.distributionExtension.getId().getVersion());
}
// Subwikis defualt UI
if (this.wikiUIExtensionId == null) {
String wikiUIId = this.distributionExtension.getProperty("xwiki.extension.distribution.wikiui");
if (wikiUIId != null) {
String wikiUIVersion = this.distributionExtension.getProperty("xwiki.extension.distribution.wikiui.version");
this.wikiUIExtensionId = new ExtensionId(wikiUIId, wikiUIVersion != null ? new DefaultVersion(wikiUIVersion) : this.distributionExtension.getId().getVersion());
}
} else if (this.wikiUIExtensionId.getVersion() == null) {
this.wikiUIExtensionId = new ExtensionId(this.wikiUIExtensionId.getId(), this.distributionExtension.getId().getVersion());
}
}
}
use of org.xwiki.extension.version.internal.DefaultVersion in project xwiki-platform by xwiki.
the class ExtensionVersionsRESTResource method getExtensionVersions.
@GET
public ExtensionVersions getExtensionVersions(@PathParam("extensionId") String extensionId, @QueryParam(Resources.QPARAM_LIST_START) @DefaultValue("0") int offset, @QueryParam(Resources.QPARAM_LIST_NUMBER) @DefaultValue("-1") int number, @QueryParam(Resources.QPARAM_VERSIONS_RANGES) String ranges) throws QueryException, InvalidVersionRangeException {
Query query = createExtensionsSummariesQuery(null, "extension.id = :extensionId", 0, -1, true);
query.bindValue("extensionId", extensionId);
ExtensionVersions extensions = this.extensionObjectFactory.createExtensionVersions();
getExtensionSummaries(extensions.getExtensionVersionSummaries(), query);
// Filter by ranges
if (StringUtils.isNotBlank(ranges)) {
VersionConstraint constraint = this.extensionFactory.getVersionConstraint(ranges);
if (constraint.getVersion() != null) {
throw new InvalidVersionRangeException("Invalid ranges syntax [" + ranges + "]");
}
for (Iterator<ExtensionVersionSummary> it = extensions.getExtensionVersionSummaries().iterator(); it.hasNext(); ) {
if (!constraint.containsVersion(new DefaultVersion(it.next().getVersion()))) {
it.remove();
}
}
}
// Order by version
final Map<String, Version> versionCache = new HashMap<>();
Collections.sort(extensions.getExtensionVersionSummaries(), new Comparator<ExtensionVersionSummary>() {
@Override
public int compare(ExtensionVersionSummary o1, ExtensionVersionSummary o2) {
return toVersion(o1.getVersion()).compareTo(toVersion(o2.getVersion()));
}
private Version toVersion(String versionString) {
Version version = versionCache.get(versionString);
if (version == null) {
version = extensionFactory.getVersion(versionString);
versionCache.put(versionString, version);
}
return version;
}
});
extensions.setTotalHits(extensions.getExtensionVersionSummaries().size());
extensions.setOffset(offset);
if (offset > 0 || (number > -1 && offset + number < extensions.getExtensionVersionSummaries().size())) {
if (offset >= extensions.getExtensionVersionSummaries().size() || number == 0) {
extensions.getExtensionVersionSummaries().clear();
} else {
List<ExtensionVersionSummary> limited = new ArrayList<ExtensionVersionSummary>(extensions.getExtensionVersionSummaries());
extensions.getExtensionVersionSummaries().clear();
extensions.withExtensionVersionSummaries(limited.subList(offset < 0 ? 0 : offset, number < 0 ? extensions.getExtensionVersionSummaries().size() : offset + number));
}
}
return extensions;
}
Aggregations