Search in sources :

Example 11 with CatalogCategory

use of org.eclipse.equinox.internal.p2.discovery.model.CatalogCategory in project epp.mpc by eclipse.

the class NewsUrlHandler method handleSearch.

@Override
protected boolean handleSearch(CatalogDescriptor descriptor, String url, String searchString, Map<String, String> params) {
    MarketplaceWizard marketplaceWizard = viewer.getWizard();
    // $NON-NLS-1$
    String filterParam = params.get("filter");
    // $NON-NLS-1$
    String[] filters = filterParam.split(" ");
    ICategory searchCategory = null;
    IMarket searchMarket = null;
    for (String filter : filters) {
        if (filter.startsWith("tid:")) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            String id = filter.substring("tid:".length());
            List<CatalogCategory> catalogCategories = marketplaceWizard.getCatalog().getCategories();
            for (CatalogCategory catalogCategory : catalogCategories) {
                if (catalogCategory instanceof MarketplaceCategory) {
                    MarketplaceCategory marketplaceCategory = (MarketplaceCategory) catalogCategory;
                    List<? extends IMarket> markets = marketplaceCategory.getMarkets();
                    for (IMarket market : markets) {
                        if (id.equals(market.getId())) {
                            searchMarket = market;
                        } else {
                            final List<? extends ICategory> categories = market.getCategory();
                            for (ICategory category : categories) {
                                if (id.equals(category.getId())) {
                                    searchCategory = category;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    marketplaceWizard.getCatalogPage().search(descriptor, searchMarket, searchCategory, searchString);
    return true;
}
Also used : ICategory(org.eclipse.epp.mpc.core.model.ICategory) CatalogCategory(org.eclipse.equinox.internal.p2.discovery.model.CatalogCategory) MarketplaceCategory(org.eclipse.epp.internal.mpc.ui.catalog.MarketplaceCategory) IMarket(org.eclipse.epp.mpc.core.model.IMarket)

Example 12 with CatalogCategory

use of org.eclipse.equinox.internal.p2.discovery.model.CatalogCategory in project epp.mpc by eclipse.

the class MarketplaceDiscoveryStrategy method handleSearchResult.

protected void handleSearchResult(MarketplaceCategory catalogCategory, ISearchResult result, final IProgressMonitor monitor) {
    List<CatalogItem> items = getItems();
    if (items != null && !result.getNodes().isEmpty()) {
        int nodeWork = 1000;
        int favoritesWork = catalogCategory.getContents() == Contents.USER_FAVORITES ? 0 : 1000;
        SubMonitor progress = SubMonitor.convert(monitor, Messages.MarketplaceDiscoveryStrategy_loadingResources, result.getNodes().size() * nodeWork + favoritesWork);
        try {
            boolean userFavoritesSupported = false;
            if (catalogCategory.getContents() == Contents.USER_FAVORITES) {
                userFavoritesSupported = true;
            } else if (hasUserFavoritesService()) {
                try {
                    applyShellProvider();
                    marketplaceService.userFavorites(result.getNodes(), progress.newChild(favoritesWork));
                    userFavoritesSupported = true;
                } catch (NotAuthorizedException e1) {
                // user is not logged in. we just ignore this.
                } catch (UnsupportedOperationException e1) {
                // ignore
                } catch (Exception e1) {
                    // something went wrong. log and proceed.
                    MarketplaceClientCore.error(Messages.MarketplaceDiscoveryStrategy_FavoritesRetrieveError, e1);
                }
            }
            for (final INode node : result.getNodes()) {
                String id = node.getId();
                try {
                    final MarketplaceNodeCatalogItem catalogItem = new MarketplaceNodeCatalogItem();
                    catalogItem.setMarketplaceUrl(catalogDescriptor.getUrl());
                    catalogItem.setId(id);
                    catalogItem.setName(getCatalogItemName(node));
                    catalogItem.setCategoryId(catalogCategory.getId());
                    ICategories categories = node.getCategories();
                    if (categories != null) {
                        for (ICategory category : categories.getCategory()) {
                            catalogItem.addTag(new Tag(ICategory.class, category.getId(), category.getName()));
                        }
                    }
                    catalogItem.setData(node);
                    catalogItem.setSource(source);
                    catalogItem.setLicense(node.getLicense());
                    catalogItem.setUserFavorite(userFavoritesSupported ? node.getUserFavorite() : null);
                    IIus ius = node.getIus();
                    if (ius != null) {
                        List<MarketplaceNodeInstallableUnitItem> installableUnitItems = new ArrayList<MarketplaceNodeInstallableUnitItem>();
                        for (IIu iu : ius.getIuElements()) {
                            MarketplaceNodeInstallableUnitItem iuItem = new MarketplaceNodeInstallableUnitItem();
                            iuItem.init(iu);
                            installableUnitItems.add(iuItem);
                        }
                        catalogItem.setInstallableUnitItems(installableUnitItems);
                    }
                    if (node.getShortdescription() == null && node.getBody() != null) {
                        // bug 306653 <!--break--> marks the end of the short description.
                        String descriptionText = node.getBody();
                        Matcher matcher = BREAK_PATTERN.matcher(node.getBody());
                        if (matcher.find()) {
                            int start = matcher.start();
                            if (start > 0) {
                                String shortDescriptionText = descriptionText.substring(0, start).trim();
                                if (shortDescriptionText.length() > 0) {
                                    descriptionText = shortDescriptionText;
                                }
                            }
                        }
                        catalogItem.setDescription(descriptionText);
                    } else {
                        catalogItem.setDescription(node.getShortdescription());
                    }
                    catalogItem.setProvider(node.getCompanyname());
                    String updateurl = node.getUpdateurl();
                    if (updateurl != null) {
                        try {
                            // trim is important!
                            updateurl = updateurl.trim();
                            URLUtil.toURL(updateurl);
                            catalogItem.setSiteUrl(updateurl);
                        } catch (MalformedURLException e) {
                        // don't use malformed URLs
                        }
                    }
                    if (catalogItem.getInstallableUnits() == null || catalogItem.getInstallableUnits().isEmpty() || catalogItem.getSiteUrl() == null) {
                        catalogItem.setAvailable(false);
                    }
                    if (node.getImage() != null) {
                        if (!source.getResourceProvider().containsResource(node.getImage())) {
                            cacheResource(source.getResourceProvider(), catalogItem, node.getImage());
                        }
                        createIcon(catalogItem, node);
                    }
                    if (node.getBody() != null || node.getScreenshot() != null) {
                        final Overview overview = new Overview();
                        overview.setItem(catalogItem);
                        overview.setSummary(node.getBody());
                        overview.setUrl(node.getUrl());
                        catalogItem.setOverview(overview);
                        if (node.getScreenshot() != null) {
                            if (!source.getResourceProvider().containsResource(node.getScreenshot())) {
                                cacheResource(source.getResourceProvider(), catalogItem, node.getScreenshot());
                            }
                            overview.setScreenshot(node.getScreenshot());
                        }
                    }
                    items.add(catalogItem);
                    marketplaceInfo.map(catalogItem.getMarketplaceUrl(), node);
                    marketplaceInfo.computeInstalled(computeInstalledFeatures(progress.newChild(nodeWork)), catalogItem);
                } catch (RuntimeException ex) {
                    MarketplaceClientUi.error(NLS.bind(Messages.MarketplaceDiscoveryStrategy_ParseError, // $NON-NLS-1$
                    node == null ? "null" : id), ex);
                }
            }
        } finally {
            progress.done();
        }
        if (result.getMatchCount() != null) {
            catalogCategory.setMatchCount(result.getMatchCount());
            if (result.getMatchCount() > result.getNodes().size()) {
                // add an item here to indicate that the search matched more items than were returned by the server
                addCatalogItem(catalogCategory);
            }
        }
    }
}
Also used : INode(org.eclipse.epp.mpc.core.model.INode) MalformedURLException(java.net.MalformedURLException) Matcher(java.util.regex.Matcher) ICategory(org.eclipse.epp.mpc.core.model.ICategory) SubMonitor(org.eclipse.core.runtime.SubMonitor) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Overview(org.eclipse.equinox.internal.p2.discovery.model.Overview) NotAuthorizedException(org.eclipse.epp.internal.mpc.core.service.AbstractDataStorageService.NotAuthorizedException) URISyntaxException(java.net.URISyntaxException) CoreException(org.eclipse.core.runtime.CoreException) NotAuthorizedException(org.eclipse.epp.internal.mpc.core.service.AbstractDataStorageService.NotAuthorizedException) NoSuchElementException(java.util.NoSuchElementException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) CatalogItem(org.eclipse.equinox.internal.p2.discovery.model.CatalogItem) IIu(org.eclipse.epp.mpc.core.model.IIu) ICategories(org.eclipse.epp.mpc.core.model.ICategories) Tag(org.eclipse.equinox.internal.p2.discovery.model.Tag) IIus(org.eclipse.epp.mpc.core.model.IIus)

Example 13 with CatalogCategory

use of org.eclipse.equinox.internal.p2.discovery.model.CatalogCategory in project epp.mpc by eclipse.

the class MarketplaceCatalog method performDiscovery.

protected IStatus performDiscovery(DiscoveryOperation operation, boolean refresh, IProgressMonitor monitor) {
    MultiStatus status = new MultiStatus(MarketplaceClientUi.BUNDLE_ID, 0, Messages.MarketplaceCatalog_queryFailed, null);
    if (getDiscoveryStrategies().isEmpty()) {
        throw new IllegalStateException();
    }
    // reset, keeping no items but the same tags, categories and certifications
    List<CatalogItem> items = new ArrayList<CatalogItem>();
    List<CatalogCategory> categories = new ArrayList<CatalogCategory>(getCategories());
    List<Certification> certifications = new ArrayList<Certification>(getCertifications());
    List<Tag> tags = new ArrayList<Tag>(getTags());
    if (!refresh) {
        for (CatalogCategory catalogCategory : categories) {
            catalogCategory.getItems().clear();
        }
    }
    final int totalTicks = 100000;
    final int discoveryTicks = totalTicks - (totalTicks / 10);
    monitor.beginTask(Messages.MarketplaceCatalog_queryingMarketplace, totalTicks);
    try {
        int strategyTicks = discoveryTicks / getDiscoveryStrategies().size();
        for (AbstractDiscoveryStrategy discoveryStrategy : getDiscoveryStrategies()) {
            if (monitor.isCanceled()) {
                status.add(Status.CANCEL_STATUS);
                break;
            }
            if (discoveryStrategy instanceof MarketplaceDiscoveryStrategy) {
                List<CatalogCategory> oldCategories = discoveryStrategy.getCategories();
                List<CatalogItem> oldItems = discoveryStrategy.getItems();
                List<Certification> oldCertifications = discoveryStrategy.getCertifications();
                List<Tag> oldTags = discoveryStrategy.getTags();
                discoveryStrategy.setCategories(categories);
                discoveryStrategy.setItems(items);
                discoveryStrategy.setCertifications(certifications);
                discoveryStrategy.setTags(tags);
                try {
                    MarketplaceDiscoveryStrategy marketplaceStrategy = (MarketplaceDiscoveryStrategy) discoveryStrategy;
                    operation.run(marketplaceStrategy, new SubProgressMonitor(monitor, strategyTicks));
                } catch (CoreException e) {
                    IStatus error = MarketplaceClientCore.computeWellknownProblemStatus(e);
                    if (error == null) {
                        error = new Status(e.getStatus().getSeverity(), DiscoveryCore.ID_PLUGIN, NLS.bind(Messages.MarketplaceCatalog_failedWithError, discoveryStrategy.getClass().getSimpleName()), e);
                    }
                    status.add(error);
                } finally {
                    // remove everything from strategy again, so it can't accidentally mess with the results later
                    discoveryStrategy.setCategories(oldCategories);
                    discoveryStrategy.setItems(oldItems);
                    discoveryStrategy.setCertifications(oldCertifications);
                    discoveryStrategy.setTags(oldTags);
                    // make sure strategy didn't misbehave
                    if (items.contains(null)) {
                        while (items.remove(null)) {
                        }
                        IStatus error = new Status(IStatus.WARNING, DiscoveryCore.ID_PLUGIN, NLS.bind(Messages.MarketplaceCatalog_addedNullEntry, discoveryStrategy.getClass().getSimpleName()));
                        status.add(error);
                    }
                }
            }
        }
        update(categories, items, certifications, tags);
    } finally {
        monitor.done();
    }
    return computeStatus(status);
}
Also used : MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) ArrayList(java.util.ArrayList) MultiStatus(org.eclipse.core.runtime.MultiStatus) CatalogCategory(org.eclipse.equinox.internal.p2.discovery.model.CatalogCategory) Certification(org.eclipse.equinox.internal.p2.discovery.model.Certification) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) CatalogItem(org.eclipse.equinox.internal.p2.discovery.model.CatalogItem) CoreException(org.eclipse.core.runtime.CoreException) AbstractDiscoveryStrategy(org.eclipse.equinox.internal.p2.discovery.AbstractDiscoveryStrategy) Tag(org.eclipse.equinox.internal.p2.discovery.model.Tag)

Example 14 with CatalogCategory

use of org.eclipse.equinox.internal.p2.discovery.model.CatalogCategory in project epp.mpc by eclipse.

the class MarketplaceDiscoveryStrategy method addUserActionItem.

public UserActionCatalogItem addUserActionItem(MarketplaceCategory catalogCategory, UserAction userAction, Object data) {
    for (ListIterator<CatalogItem> i = items.listIterator(items.size()); i.hasPrevious(); ) {
        CatalogItem item = i.previous();
        if (item.getSource() == source && (item.getCategory() == catalogCategory || catalogCategory.getId().equals(item.getCategoryId())) && item instanceof UserActionCatalogItem) {
            UserActionCatalogItem actionItem = (UserActionCatalogItem) item;
            if (actionItem.getUserAction() == userAction) {
                return actionItem;
            }
        }
    }
    UserActionCatalogItem catalogItem = new UserActionCatalogItem();
    catalogItem.setUserAction(userAction);
    catalogItem.setSource(source);
    catalogItem.setData(data);
    // $NON-NLS-1$
    catalogItem.setId(catalogDescriptor.getUrl().toString() + "#" + userAction.name());
    catalogItem.setCategoryId(catalogCategory.getId());
    items.add(0, catalogItem);
    return catalogItem;
}
Also used : CatalogItem(org.eclipse.equinox.internal.p2.discovery.model.CatalogItem)

Aggregations

CatalogCategory (org.eclipse.equinox.internal.p2.discovery.model.CatalogCategory)8 CatalogItem (org.eclipse.equinox.internal.p2.discovery.model.CatalogItem)8 CoreException (org.eclipse.core.runtime.CoreException)3 SubMonitor (org.eclipse.core.runtime.SubMonitor)3 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 NoSuchElementException (java.util.NoSuchElementException)2 NotAuthorizedException (org.eclipse.epp.internal.mpc.core.service.AbstractDataStorageService.NotAuthorizedException)2 MarketplaceCategory (org.eclipse.epp.internal.mpc.ui.catalog.MarketplaceCategory)2 ICategory (org.eclipse.epp.mpc.core.model.ICategory)2 INode (org.eclipse.epp.mpc.core.model.INode)2 AbstractDiscoveryStrategy (org.eclipse.equinox.internal.p2.discovery.AbstractDiscoveryStrategy)2 Tag (org.eclipse.equinox.internal.p2.discovery.model.Tag)2 HashMap (java.util.HashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Matcher (java.util.regex.Matcher)1 IStatus (org.eclipse.core.runtime.IStatus)1 MultiStatus (org.eclipse.core.runtime.MultiStatus)1