View Javadoc

1   /*
2   Karma core - Core of the Karma application
3   Copyright (C) 2004  Toolforge <www.toolforge.nl>
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19  package nl.toolforge.karma.core.cmd.impl;
20  
21  import nl.toolforge.karma.core.Version;
22  import nl.toolforge.karma.core.cmd.CommandDescriptor;
23  import nl.toolforge.karma.core.cmd.CommandException;
24  import nl.toolforge.karma.core.cmd.CommandResponse;
25  import nl.toolforge.karma.core.cmd.DefaultCommand;
26  import nl.toolforge.karma.core.manifest.Manifest;
27  import nl.toolforge.karma.core.manifest.ManifestException;
28  import nl.toolforge.karma.core.module.Module;
29  import nl.toolforge.karma.core.module.ModuleComparator;
30  import nl.toolforge.karma.core.module.ModuleTypeException;
31  import nl.toolforge.karma.core.vc.ModuleStatus;
32  import nl.toolforge.karma.core.vc.VersionControlException;
33  import nl.toolforge.karma.core.vc.cvsimpl.threads.CVSLogThread;
34  import nl.toolforge.karma.core.vc.threads.ParallelRunner;
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  import java.util.ArrayList;
39  import java.util.Collection;
40  import java.util.Collections;
41  import java.util.Iterator;
42  import java.util.List;
43  import java.util.Map;
44  
45  /***
46   * This command gets the active manifest and presents it in the UI. UI implementations are responsible for the rendering
47   * part.
48   */
49  public class ViewManifest extends DefaultCommand {
50  
51    protected Log logger = LogFactory.getLog(ViewManifest.class);
52  
53    private List renderedList = null;
54    private CommandResponse commandResponse = new CommandResponse();
55  
56    public ViewManifest(CommandDescriptor descriptor) {
57      super(descriptor);
58  
59      renderedList = new ArrayList();
60    }
61  
62    public void execute() throws CommandException {
63  
64      if (!getContext().isManifestLoaded()) {
65        throw new CommandException(ManifestException.NO_ACTIVE_MANIFEST);
66      }
67      Manifest manifest = getContext().getCurrentManifest();
68  
69      List sourceModules = new ArrayList();
70  
71      // Transform and sort
72      //
73  
74      // todo hmm kan dit niet simpeler ?
75      //
76      Collection c = manifest.getAllModules().values();
77  
78      for (Iterator i = c.iterator(); i.hasNext();) {
79        sourceModules.add(i.next());
80      }
81  
82      Collections.sort(sourceModules, new ModuleComparator());
83  
84      ParallelRunner runner = new ParallelRunner(manifest, CVSLogThread.class);
85      runner.execute(0); // Blocks ...
86  
87      Map statusOverview = runner.retrieveResults();
88  
89      for (Iterator i = sourceModules.iterator(); i.hasNext();) {
90  
91        Module module = (Module) i.next();
92  
93        ModuleStatus moduleStatus = (ModuleStatus) statusOverview.get(module);
94  
95        String[] moduleData = new String[8];
96        moduleData[0] = module.getName();
97  
98        boolean existsInRepository = moduleStatus.existsInRepository();
99  
100       try {
101 
102         if (!existsInRepository) {
103           moduleData[1] = Module.UNKNOWN.getType();
104         } else {
105           try {
106             moduleData[1] = module.getType().getShortType();
107           } catch (ModuleTypeException e) {
108             //this exception occurs when a module has not yet been checked out.
109             //therefore, only a warning.
110             logger.warn(e);
111             moduleData[1] = Module.UNKNOWN.getType();
112           }
113         }
114 
115         if (!manifest.isLocal(module)) {
116           moduleData[2] = "N/A";
117         } else {
118           if (manifest.getState(module).equals(Module.WORKING)) {
119             moduleData[2] = "HEAD";
120           } else {
121             Version localVersion = moduleStatus.getLocalVersion();
122             moduleData[2] = (localVersion == null ? "" : localVersion.getVersionNumber());
123           }
124         }
125 
126         if (existsInRepository) {
127           Version remoteVersion = moduleStatus.getLastVersion();
128           moduleData[3] = (remoteVersion == null ? "" : "(" + remoteVersion.getVersionNumber() + ")");
129         } else {
130           moduleData[3] = "";
131         }
132 
133       } catch (VersionControlException v) {
134         // Version for the module is non-existing in the repository.
135         //
136         throw new CommandException(v.getErrorCode(), v.getMessageArguments());
137       }
138 
139       moduleData[4] = "(" + module.getVersionAsString() + ")";
140       if ( moduleData[4].equals("(N/A)") ) {
141         moduleData[4] = "";
142       }
143       moduleData[5] = (module.hasPatchLine() ? "available" : "not available");
144 
145       if (existsInRepository) {
146         moduleData[6] = manifest.getState(module).toString();
147         moduleData[7] = module.getLocation().getId();
148       } else {
149         moduleData[6] = "";
150         if (moduleStatus.connectionFailure()) {
151           moduleData[7] = module.getLocation().getId() + " : <Connection failed>";
152         } else if (moduleStatus.authenticationFailure()) {
153           moduleData[7] = module.getLocation().getId() + "<Authentication failed>";
154         } else if (moduleStatus.internalError()) {
155           moduleData[7] = module.getLocation().getId() + "<Internal Error>";
156         } else {
157           moduleData[7] = module.getLocation().getId() + "<Not in repository>";
158         }
159       }
160       renderedList.add(moduleData);
161     }
162   }
163 
164   /***
165    * Gets the commands' response object.
166    *
167    * @return The commands' response object.
168    */
169   public CommandResponse getCommandResponse() {
170     return this.commandResponse;
171   }
172 
173   /***
174    * <p>Returns the contents of the manifest in a two-dimensional <code>String[]</code> data-structure, for easy
175    * reference. The contents of this structure can be queried through <code>renderedList.get(i)</code>. This call
176    * retrieves a <code>String[]</code> with the most important data items for each module.
177    *
178    * @return A <code>List</code> containing <code>String[]</code> instances.
179    */
180   protected List getData() {
181     return renderedList;
182   }
183 }