001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    
021    package org.apache.geronimo.console.car;
022    
023    import java.util.List;
024    import java.util.ArrayList;
025    import java.util.Collections;
026    import java.util.Comparator;
027    
028    import javax.portlet.RenderRequest;
029    
030    import org.apache.geronimo.system.plugin.PluginInstaller;
031    import org.apache.geronimo.system.plugin.PluginInstallerGBean;
032    import org.apache.geronimo.system.plugin.model.PluginListType;
033    import org.apache.geronimo.system.plugin.model.PluginType;
034    import org.apache.geronimo.system.plugin.model.PluginArtifactType;
035    import org.apache.geronimo.kernel.repository.Dependency;
036    
037    /**
038     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
039     */
040    public abstract class AbstractListHandler extends BaseImportExportHandler {
041        public AbstractListHandler(String mode, String viewName) {
042            super(mode, viewName);
043        }
044    
045        protected void listPlugins(RenderRequest request, PluginInstaller pluginInstaller, PluginListType data, boolean validate) {
046            List<PluginInfoBean> plugins = new ArrayList<PluginInfoBean>();
047    
048            for (PluginType metadata: data.getPlugin()) {
049    
050                // ignore plugins which have no artifacts defined
051                if (metadata.getPluginArtifact().isEmpty()) {
052                    continue;
053                }
054    
055                if (metadata.getCategory() == null) {
056                    metadata.setCategory("Unspecified");
057                }
058    
059                for (PluginArtifactType artifact : metadata.getPluginArtifact()) {
060                    PluginInfoBean plugin = new PluginInfoBean();
061                    plugin.setPlugin(metadata);
062                    plugin.setPluginArtifact(artifact);
063    
064                    if (validate) {
065                        // determine if the plugin is installable
066                        PluginType holder = PluginInstallerGBean.copy(metadata, artifact);
067                        try {
068                            pluginInstaller.validatePlugin(holder);
069                        } catch (Exception e) {
070                            plugin.setInstallable(false);
071                        }
072                        Dependency[] missingPrereqs = pluginInstaller.checkPrerequisites(holder);
073                        if (missingPrereqs.length > 0) {
074                            plugin.setInstallable(false);
075                        }
076                    }
077                    plugins.add(plugin);
078                }
079            }
080    
081            // sort the plugin list based on the selected table column
082            final String column = request.getParameter("column");
083            Collections.sort(plugins, new Comparator<PluginInfoBean>() {
084                public int compare(PluginInfoBean o1, PluginInfoBean o2) {
085                    if ("Category".equals(column)) {
086                        String category1 = o1.getCategory();
087                        String category2 = o2.getCategory();
088                        if (category1.equals(category2)) {
089                            return o1.getName().compareTo(o2.getName());
090                        }
091                        return category1.compareTo(category2);
092                    }
093                    else if ("Version".equals(column)) {
094                        String version1 = o1.getPluginArtifact().getModuleId().getVersion();
095                        String version2 = o2.getPluginArtifact().getModuleId().getVersion();
096                        if (version1.equals(version2)) {
097                            return o1.getName().compareTo(o2.getName());
098                        }
099                        return version1.compareTo(version2);
100                    }
101                    else if ("Installable".equals(column)) {
102                        if (o1.isInstallable() == o2.isInstallable()) {
103                            return o1.getName().compareTo(o2.getName());
104                        }
105                        return o1.isInstallable() ? -1 : 1 ;
106                    }
107                    else { // default sort column is Name
108                        return o1.getName().compareTo(o2.getName());
109                    }
110                }
111            });
112    
113            // save everything in the request
114            request.setAttribute("plugins", plugins);
115        }
116    }