Package org.wso2.carbon.component.mgt.core.query

Source Code of org.wso2.carbon.component.mgt.core.query.AvailableIUQuery

/*
* Copyright 2009-2010 WSO2, Inc. (http://wso2.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wso2.carbon.component.mgt.core.query;

import org.eclipse.equinox.internal.provisional.p2.engine.IProfile;
import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.internal.provisional.p2.metadata.query.IUPropertyQuery;
import org.eclipse.equinox.internal.provisional.p2.metadata.query.InstallableUnitQuery;
import org.eclipse.equinox.internal.provisional.p2.metadata.query.LatestIUVersionQuery;
import org.eclipse.equinox.internal.provisional.p2.query.Collector;
import org.eclipse.equinox.internal.provisional.p2.query.CompositeQuery;
import org.eclipse.equinox.internal.provisional.p2.query.IQueryable;
import org.eclipse.equinox.internal.provisional.p2.query.Query;
import org.wso2.carbon.component.mgt.core.util.ProvisioningUtils;

import java.util.Iterator;

public class AvailableIUQuery extends IUQuery {
    public AvailableIUQuery(QueryContext queryContext) {
        super(queryContext);
    }

    public Collector perform() {
        Collector collector = new Collector();
        IQueryable queryable = queryContext.getQueryable();
        IProfile profile = ProvisioningUtils.getProfile();
        if (queryable == null || profile == null) {
            return collector;
        }

        Query query = new IUPropertyQuery(IInstallableUnit.PROP_TYPE_GROUP, Boolean.TRUE.toString());

        if (queryContext.isShowLatestVersionsOnly()) {
            query = new CompositeQuery(new Query[]{query, new LatestIUVersionQuery()});
        }

        collector = queryable.query(query, collector, null);

        if (queryContext.isHideAlreadyInstalled()) {
            Collector filteredCollection = new Collector();
            for (Iterator itr = collector.iterator(); itr.hasNext();) {
                IInstallableUnit iu = (IInstallableUnit) itr.next();

                Collector tmpCollector = profile.query(new InstallableUnitQuery(iu.getId()), new Collector(), null);
                if (tmpCollector.size() == 0) {
                    filteredCollection.accept(iu);
                    continue;
                }
                Iterator iter = tmpCollector.iterator();

                while (iter.hasNext()) {
                    IInstallableUnit installed = (IInstallableUnit) iter.next();
                    if (installed.getVersion().compareTo(iu.getVersion()) < 0) {
                        filteredCollection.accept(iu);
                    }
                }
            }
            return filteredCollection;
        }
        return collector;
    }
}
TOP

Related Classes of org.wso2.carbon.component.mgt.core.query.AvailableIUQuery

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.