Search in sources :

Example 6 with Version

use of org.xwiki.extension.version.Version in project xwiki-platform by xwiki.

the class FlavorSearchJob method findValidVersion.

private Extension findValidVersion(String flavorId, String namespace) {
    IterableResult<Version> versions;
    try {
        versions = this.repositoryManager.resolveVersions(flavorId, 0, -1);
        if (versions.getSize() == 0) {
            this.logger.debug("Could not find any version for the flavor extension [{}]", flavorId);
            return null;
        }
        List<Version> versionList = new ArrayList<Version>(versions.getSize());
        for (Version version : versions) {
            versionList.add(version);
        }
        return findValidVersion(flavorId, namespace, versionList);
    } catch (ResolveException e) {
        this.logger.debug("Failed to resolve versions for extension id [{}]", flavorId, e);
    }
    return null;
}
Also used : ResolveException(org.xwiki.extension.ResolveException) Version(org.xwiki.extension.version.Version) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 7 with Version

use of org.xwiki.extension.version.Version 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;
}
Also used : VersionConstraint(org.xwiki.extension.version.VersionConstraint) Query(org.xwiki.query.Query) HashMap(java.util.HashMap) DefaultVersion(org.xwiki.extension.version.internal.DefaultVersion) ArrayList(java.util.ArrayList) InvalidVersionRangeException(org.xwiki.extension.version.InvalidVersionRangeException) ExtensionVersionSummary(org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionVersionSummary) Version(org.xwiki.extension.version.Version) DefaultVersion(org.xwiki.extension.version.internal.DefaultVersion) ExtensionVersions(org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionVersions) GET(javax.ws.rs.GET)

Aggregations

Version (org.xwiki.extension.version.Version)7 ExtensionId (org.xwiki.extension.ExtensionId)3 DefaultVersion (org.xwiki.extension.version.internal.DefaultVersion)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Extension (org.xwiki.extension.Extension)2 ResolveException (org.xwiki.extension.ResolveException)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 GET (javax.ws.rs.GET)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 CacheException (org.xwiki.cache.CacheException)1 ComponentLifecycleException (org.xwiki.component.manager.ComponentLifecycleException)1