1 package nl.toolforge.karma.cli.cmd;
2
3 import nl.toolforge.karma.core.cmd.CommandDescriptor;
4 import nl.toolforge.karma.core.cmd.CommandException;
5 import nl.toolforge.karma.core.cmd.CommandResponse;
6 import nl.toolforge.karma.core.cmd.DefaultCommand;
7 import nl.toolforge.karma.core.cmd.event.MessageEvent;
8 import nl.toolforge.karma.core.cmd.event.SimpleMessage;
9 import nl.toolforge.karma.core.location.Location;
10 import nl.toolforge.karma.core.location.LocationException;
11 import org.apache.commons.lang.StringUtils;
12
13 import java.util.Iterator;
14 import java.util.Map;
15
16 /***
17 * Lists all manifests in the working contexts' location store.
18 *
19 * @author D.A. Smedes
20 * @version $Id: ListLocations.java,v 1.1 2004/10/21 06:45:41 asmedes Exp $
21 */
22 public class ListLocations extends DefaultCommand {
23
24 private CommandResponse response = new CommandResponse();
25
26 public ListLocations(CommandDescriptor descriptor) {
27 super(descriptor);
28 }
29
30 public void execute() throws CommandException {
31
32 Map locations = null;
33 try {
34 locations = getWorkingContext().getLocationLoader().getLocations();
35 } catch (LocationException e) {
36 throw new CommandException(e.getErrorCode(), e.getMessageArguments());
37 }
38
39 StringBuffer buffer = new StringBuffer();
40
41 buffer.append("| Id" + StringUtils.repeat(" ", 11) + " |");
42 buffer.append(" Type" + StringUtils.repeat(" ", 5) + " |");
43 buffer.append(" Connection String" + StringUtils.repeat(" ", 51) + " |\n");
44
45 buffer.append("|" + StringUtils.repeat("_", 98) + "|\n");
46
47 for (Iterator i = locations.values().iterator(); i.hasNext();) {
48
49 Location location = (Location) i.next();
50
51 buffer.append("| " + location.getId() + StringUtils.repeat(" ", 14 - location.getId().length()));
52 buffer.append("| " + location.getType() + StringUtils.repeat(" ", 10 - location.getType().toString().length()));
53 buffer.append("| " + location.toString() + StringUtils.repeat(" ", 69 - location.toString().length()) + "|\n");
54 }
55
56 response.addEvent(new MessageEvent(new SimpleMessage(buffer.toString())));
57
58 }
59
60 public CommandResponse getCommandResponse() {
61 return response;
62 }
63
64 }