1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package nl.toolforge.karma.cli.cmd;
20
21 import java.text.SimpleDateFormat;
22
23 import java.util.Iterator;
24 import java.util.List;
25
26 import org.apache.commons.lang.StringUtils;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import nl.toolforge.karma.core.cmd.CommandDescriptor;
32 import nl.toolforge.karma.core.cmd.CommandException;
33 import nl.toolforge.karma.core.cmd.CommandResponse;
34 import nl.toolforge.karma.core.cmd.DefaultCommand;
35 import nl.toolforge.karma.core.cmd.event.MessageEvent;
36 import nl.toolforge.karma.core.cmd.event.SimpleMessage;
37 import nl.toolforge.karma.core.history.ModuleHistory;
38 import nl.toolforge.karma.core.history.ModuleHistoryEvent;
39 import nl.toolforge.karma.core.history.ModuleHistoryException;
40 import nl.toolforge.karma.core.history.ModuleHistoryFactory;
41 import nl.toolforge.karma.core.manifest.ManifestException;
42 import nl.toolforge.karma.core.module.Module;
43
44 /***
45 * Renders the contents of the <code>history.xml</code> to the console.
46 *
47 * @author D.A. Smedes
48 *
49 * @version $Id: ViewModuleHistory.java,v 1.7 2004/11/16 14:15:40 amooy Exp $
50 */
51 public class ViewModuleHistory extends DefaultCommand {
52
53 private static final Log logger = LogFactory.getLog(ViewModuleHistory.class);
54
55 private CommandResponse commandResponse = new CommandResponse();
56
57 public ViewModuleHistory(CommandDescriptor descriptor) {
58 super(descriptor);
59 }
60
61 public void execute() throws CommandException {
62
63
64 if (!getContext().isManifestLoaded()) {
65 throw new CommandException(ManifestException.NO_ACTIVE_MANIFEST);
66 }
67
68 String moduleName = getCommandLine().getOptionValue("m");
69 Module module = null;
70 try {
71 module = getContext().getCurrentManifest().getModule(moduleName);
72 } catch (ManifestException e) {
73 throw new CommandException(e.getErrorCode(),e.getMessageArguments());
74 }
75
76 if (!getContext().getCurrentManifest().isLocal(module)) {
77 throw new CommandException(CommandException.MODULE_NOT_LOCAL, new Object[]{module.getName()});
78 }
79
80 ModuleHistoryFactory factory =
81 ModuleHistoryFactory.getInstance(getContext().getCurrentManifest().getBaseDirectory());
82
83 ModuleHistory history = null;
84 try {
85 history = factory.getModuleHistory(module);
86 } catch (ModuleHistoryException e) {
87 logger.error(e.getMessage());
88 throw new CommandException(e.getErrorCode(), e.getMessageArguments());
89 }
90
91 StringBuffer buffer = new StringBuffer();
92 buffer.append("\n");
93
94 String header = "Module history for module : " + module.getName();
95 buffer.append(header + "\n\n\n");
96
97 String h1 = "Type";
98 String h2 = "Author";
99 String h3 = "Version";
100 String h4 = "Timestamp";
101 String h5 = "Comment";
102
103 final int MAX_DATETIME = 20;
104 final int MAX_COMMENT = 60;
105
106 buffer.append(h1 + StringUtils.repeat(" ", 18 - h1.length()) + "| ");
107 buffer.append(h2 + StringUtils.repeat(" ", 15 - h2.length()) + "| ");
108 buffer.append(h3 + StringUtils.repeat(" ", 8 - h3.length()) + "| ");
109 buffer.append(h4 + StringUtils.repeat(" ", MAX_DATETIME - h4.length()) + "| ");
110 buffer.append(h5);
111 buffer.append("\n");
112 buffer.append(StringUtils.repeat("_", 100));
113 buffer.append("\n\n");
114
115 List events = history.getEvents();
116
117 for (Iterator i = events.iterator(); i.hasNext();) {
118
119 ModuleHistoryEvent event = (ModuleHistoryEvent) i.next();
120
121 buffer.append(event.getType() + StringUtils.repeat(" ", 18 - event.getType().length()) + "| ");
122 buffer.append(event.getAuthor() + StringUtils.repeat(" ", 15 - event.getAuthor().length()) + "| ");
123 buffer.append(event.getVersion().getVersionNumber() + StringUtils.repeat(" ", 8 - event.getVersion().getVersionNumber().length()) + "| ");
124
125 String date = new SimpleDateFormat("dd-MMM-yyyy HH:mm").format(event.getDatetime());
126
127 buffer.append(date + StringUtils.repeat(" ", MAX_DATETIME - date.length()) + "| ");
128
129 String comment = null;
130 if (event.getComment().length() > MAX_COMMENT) {
131 comment = event.getComment().substring(0, MAX_COMMENT);
132 } else {
133 comment = event.getComment();
134 }
135 buffer.append(comment+"\n");
136 }
137
138 commandResponse.addEvent(new MessageEvent(new SimpleMessage(buffer.toString())));
139 }
140
141 public CommandResponse getCommandResponse() {
142 return this.commandResponse;
143 }
144
145 }