View Javadoc

1   /*
2   Karma CLI - Command Line Interface for 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.cli.cmd;
20  
21  import nl.toolforge.karma.core.cmd.CommandDescriptor;
22  import nl.toolforge.karma.core.cmd.CommandException;
23  import nl.toolforge.karma.core.cmd.CommandResponse;
24  import nl.toolforge.karma.core.cmd.event.MessageEvent;
25  import nl.toolforge.karma.core.cmd.event.SimpleMessage;
26  import nl.toolforge.karma.core.cmd.impl.ListManifests;
27  import nl.toolforge.karma.core.manifest.Manifest;
28  import nl.toolforge.karma.core.manifest.ManifestException;
29  import nl.toolforge.karma.core.manifest.ManifestHeader;
30  import org.apache.commons.lang.StringUtils;
31  
32  import java.util.ArrayList;
33  import java.util.Collections;
34  import java.util.Iterator;
35  import java.util.List;
36  import java.util.Set;
37  
38  /***
39   * Command line interface implementation of the {@link ListManifests} command.
40   *
41   * @author D.A. Smedes
42   * @author W.H. Schraal
43   * @version $Id: ListManifestsImpl.java,v 1.24 2004/11/12 14:16:13 asmedes Exp $
44   */
45  public class ListManifestsImpl extends ListManifests {
46  
47    private CommandResponse response = new CommandResponse();
48  
49    public ListManifestsImpl(CommandDescriptor descriptor) throws ManifestException {
50      super(descriptor);
51    }
52  
53    public void execute() throws CommandException {
54  
55      super.execute();
56  
57      Set headers = getHeaders();
58  
59      if (headers.size() == 0) {
60        response.addEvent(new MessageEvent(this, new SimpleMessage("No manifests found for this working context.")));
61        response.addEvent(new MessageEvent(this, new SimpleMessage("TIP : Try updating the manifest store with the `update-admin`-command.")));
62      } else {
63  
64        Iterator manifestsIterator = headers.iterator();
65        ManifestHeader header;
66  
67        Manifest currentManifest = getContext().getCurrentManifest();
68  
69        StringBuffer buffer = new StringBuffer();
70  
71        buffer.append("\nManifests for working context `"+ getWorkingContext().getName() +"`:\n\n");
72  
73        String h1 = "Type";
74        int MAX_TYPE_LENGTH = 13;
75  
76        String h2 = "Name";
77        int MAX_NAME_LENGTH = 30;
78  
79        buffer.append(h1 + StringUtils.repeat(" ", 13 - h1.length()) + " | ");
80        buffer.append(h2 + StringUtils.repeat(" ", 32 - h1.length()) + "\n");
81        buffer.append(StringUtils.repeat("_", 71) + "\n");
82  
83  
84        List manifestList = new ArrayList();
85  
86        while (manifestsIterator.hasNext()) {
87  
88          header = (ManifestHeader) manifestsIterator.next();
89  
90          String typeSpaces = StringUtils.repeat(" ", MAX_TYPE_LENGTH - header.getType().length());
91          String nameSpaces = StringUtils.repeat(" ", MAX_NAME_LENGTH - header.getName().length());
92  
93          if ((currentManifest != null) && header.getName().equals(currentManifest.getName())) {
94            manifestList.add(header.getType() + typeSpaces + " | " + header.getName() + nameSpaces + " ** current manifest **\n");
95          } else {
96            manifestList.add(header.getType() + typeSpaces + " | " + header.getName() + nameSpaces + "\n");
97          }
98        }
99  
100       Collections.sort(manifestList);
101 
102       manifestsIterator = manifestList.iterator();
103       while (manifestsIterator.hasNext()) {
104         buffer.append((String) manifestsIterator.next());
105       }
106       response.addEvent(new MessageEvent(this, new SimpleMessage(buffer.toString())));
107     }
108 
109   }
110 
111   public CommandResponse getCommandResponse() {
112     return this.response;
113   }
114 
115 }