01: /*
02: * Copyright 2006-2007 Luca Garulli (luca.garulli@assetdata.it)
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.romaframework.wizard.command.project;
17:
18: import java.util.ArrayList;
19: import java.util.List;
20:
21: import org.romaframework.wizard.ProjectManager;
22: import org.romaframework.wizard.WizardException;
23: import org.romaframework.wizard.command.BaseWizardCommand;
24:
25: /**
26: * Base abstract Wizard Command class with project management.
27: *
28: * @author Luca Garulli (luca.garulli@assetdata.it)
29: */
30: public abstract class OptionalProjectBaseWizardCommand extends
31: BaseWizardCommand {
32:
33: private static final String PROJECT_PARAM = "-p";
34: protected String projectName;
35:
36: public String getParameters() {
37: return "[-p<project-name>]";
38: }
39:
40: /**
41: * Parse all optional parameters and return a String array purified of its.
42: *
43: * @param iParameters
44: * @return
45: * @throws WizardException
46: */
47: protected String[] parseOptionalParameters(String[] iParameters)
48: throws WizardException {
49: List<String> purifiedParams = new ArrayList<String>(5);
50: for (String par : iParameters) {
51: if (par != null)
52: if (par.startsWith(PROJECT_PARAM)) {
53: projectName = par.substring(PROJECT_PARAM.length());
54: break;
55: } else
56: purifiedParams.add(par);
57: }
58: String[] params = new String[purifiedParams.size()];
59: purifiedParams.toArray(params);
60:
61: if (projectName == null)
62: projectName = ProjectManager.getInstance()
63: .getCurrentProjectName();
64:
65: if (projectName == null) {
66: getIO()
67: .getError()
68: .println(
69: "\nNo current project setted. Specify a project for this wizard using -p option or use the 'switch' wizard to set it globally as current.");
70: throw new WizardException(
71: "No current project setted. Specify a project for this wizard using -p option or use the 'switch' wizard to set it globally as current.");
72: }
73: return params;
74: }
75:
76: public void help() {
77: getIO().getError().println(getCommandHelp());
78: getIO()
79: .getError()
80: .println(
81: " where: <project-name> is the name of project you want to use. If not defined, the current will be taken");
82: }
83:
84: public String getProjectName() {
85: return projectName;
86: }
87:
88: public void setProjectName(String projectName) {
89: this.projectName = projectName;
90: }
91: }
|