001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019: package de.schlund.pfixcore.util.basicapp.basics;
020:
021: import java.io.BufferedReader;
022: import java.io.IOException;
023: import java.io.InputStreamReader;
024: import java.util.ArrayList;
025:
026: import org.apache.log4j.Logger;
027:
028: import de.schlund.pfixcore.util.basicapp.helper.AppValues;
029: import de.schlund.pfixcore.util.basicapp.helper.StringUtils;
030: import de.schlund.pfixcore.util.basicapp.objects.Project;
031: import de.schlund.pfixcore.util.basicapp.objects.ServletObject;
032:
033: /**
034: * The main settings for a new Project will be set here
035: *
036: * @author <a href="mailto:rapude@schlund.de">Ralf Rapude</a>
037: * @version $Id: CreateProjectSettings.java 3416 2008-03-07 10:56:29Z smarsching $
038: */
039: public final class CreateProjectSettings {
040:
041: private static final Logger LOG = Logger
042: .getLogger(CreateProjectSettings.class);
043: /** A Project defines all informations for building a new application */
044: private Project project = null;
045: /** Informations given by the user */
046: public BufferedReader projectIn = new BufferedReader(
047: new InputStreamReader(System.in));
048: private ArrayList<ServletObject> servletList = null;
049: /** A counter for the servlet objects id */
050: private int servletCounter = 0;
051:
052: /** Constructor just prepares a new project */
053: public CreateProjectSettings() {
054: project = new Project();
055: servletList = project.getServletList();
056: }
057:
058: /**
059: * A getter for the project.
060: * @return a Project object. It consists of all
061: * necessary informations.
062: */
063: public Project getCurrentProject() {
064: return project;
065: }
066:
067: /** init method for this class */
068: public void runGetSettings() {
069: LOG.debug("Getting project settings starts now");
070: servletCounter = 0;
071: System.out.println("\n\n\n");
072: System.out
073: .println("**************************************************");
074: System.out
075: .println("* *");
076: System.out
077: .println("* Pustefix ProjectGenerator 1.0 *");
078: System.out
079: .println("* *");
080: System.out
081: .println("**************************************************");
082: System.out
083: .println("\nPlease follow the instructions to create a new "
084: + "project.");
085: System.out
086: .println("You can abort the process by pressing Ctrl + C.");
087:
088: try {
089: // setting the basic items
090: setProjectName();
091: setProjectLanguage();
092: setProjectComment();
093: // setting servlets names
094: setServletName();
095: project.setServletList(servletList);
096: } catch (IOException e) {
097: LOG.debug(e.getMessage(), e);
098: }
099: }
100:
101: /**
102: * Sets the project name
103: * @throws IOException
104: */
105: private void setProjectName() throws IOException {
106: int counter = 0;
107: String input = null;
108: boolean goOn = true;
109:
110: // a loop will be done if the project name is required
111: do {
112: System.out
113: .println("\nPlease type in the projects name e.g. "
114: + "\"myproject\"");
115: input = projectIn.readLine();
116: // checking for the right project setter
117: if (!StringUtils.checkString(input).equals("")) {
118:
119: // check whether the project already exists
120: if (!StringUtils.checkExistingProject(input)) {
121: project.setProjectName(StringUtils
122: .giveCorrectString(input));
123: } else {
124: checkOverwriteProject(input);
125: }
126:
127: goOn = false;
128: } else {
129: System.out
130: .println("The projects name is mandatory. Please type in\n"
131: + "a valid String");
132: counter += 1;
133: // nonsens has been typed in for three times. Check whether
134: // the user wants to abort
135: if (counter == 3) {
136: checkExit(0);
137: goOn = false;
138: }
139: }
140:
141: } while (goOn);
142: }
143:
144: /**
145: * Setting the default language for the new project.
146: * English is set by default.
147: * @throws IOException
148: */
149: private void setProjectLanguage() throws IOException {
150: String input = null;
151:
152: System.out
153: .println("\nPlease type in the projects default language "
154: + "(it's english if you leave the field blank).");
155: input = projectIn.readLine();
156:
157: if (StringUtils.checkString(input).equals("")) {
158: project.setLanguage(AppValues.DEFAULTLNG);
159: } else {
160: project.setLanguage(input);
161: }
162: }
163:
164: /**
165: * Setting a comment for the Project
166: */
167: private void setProjectComment() throws IOException {
168: String input = null;
169:
170: System.out
171: .println("\nPlease type in a comment for the Project");
172: System.out
173: .println("It will be \"projectname + comment\" if you leave it blank.");
174: input = projectIn.readLine();
175:
176: if (StringUtils.checkString(input).equals("")) {
177: project.setComment(project.getProjectName()
178: + AppValues.PRJCOMMENTSUFF);
179: LOG.debug("Defaultcomment has been set");
180: } else {
181: project.setComment(input);
182: LOG.debug("Projectcomment has been set by user: " + input);
183: }
184: }
185:
186: /**
187: * Method for setting the defaults servlet name
188: * @throws IOException
189: */
190: private void setServletName() throws IOException {
191: String input = null;
192: ServletObject myServletObject = new ServletObject(
193: servletCounter);
194: boolean goOn = true;
195: int counter = 0;
196: int myServCounter = servletCounter + 1;
197:
198: do {
199: System.out
200: .println("\nPlease type in a name for the servlet "
201: + myServCounter);
202: input = projectIn.readLine();
203: // checking for the right project setter
204: if (!StringUtils.checkString(input).equals("")) {
205: myServletObject.setServletName(StringUtils
206: .giveCorrectString(input));
207: servletList.add(myServletObject);
208: // increasing the counter
209: servletCounter++;
210: System.out.println("Servlet " + myServCounter
211: + " has been added!");
212: goOn = false;
213: storeMoreServlets();
214: } else {
215: counter += 1;
216: // nonsens has been typed in for three times. Check whether
217: // the user wants to abort
218: if (counter == 3) {
219: checkExit(1);
220: goOn = false;
221: }
222: }
223: } while (goOn);
224:
225: }
226:
227: /**
228: * Ask the user if he wants to create some more
229: * servlets
230: * @throws IOException
231: */
232: private void storeMoreServlets() throws IOException {
233: String input = null;
234: boolean goOn = true;
235:
236: do {
237: System.out
238: .println("\nWould you like to create another servlet? [yes] [no]");
239: input = projectIn.readLine().toLowerCase();
240:
241: if (input.equals("yes") || input.equals("y")) {
242: setServletName();
243: goOn = false;
244: } else if (input.equals("no") || input.equals("n")) {
245: goOn = false;
246: }
247:
248: } while (goOn);
249: }
250:
251: /**
252: * A method in order to check whether the user wants
253: * to abort
254: * @param method: An integer for the method to go on
255: * @throws IOException
256: */
257: private void checkExit(int method) throws IOException {
258: String exit = null;
259: boolean goOn = true;
260:
261: do {
262: System.out
263: .println("\nYou havn't typed in a valid value yet!\n"
264: + "Do you want to abort? [Yes] [No]");
265: exit = projectIn.readLine().toLowerCase();
266:
267: if (exit.equals("yes") || exit.equals("y")) {
268: System.exit(0);
269: } else if (exit.equals("n") || exit.equals("no")) {
270: goOn = false;
271: switch (method) {
272: case 0:
273: setProjectName();
274: break;
275: case 1:
276: setServletName();
277: break;
278: }
279: }
280:
281: } while (goOn);
282: }
283:
284: /**
285: * If the project already exists this method checks whether the user
286: * really wants to overwrite it.
287: * @param input The new project name typed in by the current user.
288: */
289: private void checkOverwriteProject(String input) throws IOException {
290: LOG
291: .debug("Checkout whether the user wants to overwrite existing project");
292: String overwrite = null;
293: boolean goOn = true;
294: System.out.println("\nThe project already exists!");
295:
296: do {
297: System.out
298: .println("Do you really want to overwrite an existing "
299: + "application? [Yes] [No]");
300: overwrite = projectIn.readLine().toLowerCase();
301:
302: if (overwrite.equals("yes") || overwrite.equals("y")) {
303: project.setProjectName(input);
304: goOn = false;
305: ;
306: } else if (overwrite.equals("no") || overwrite.equals("n")) {
307: setProjectName();
308: goOn = false;
309: }
310:
311: } while (goOn);
312: }
313:
314: /**
315: * Just a getter for the servlet counter
316: * @return the counter for the amount of servlets
317: */
318: public int getServletCounter() {
319: return servletCounter;
320: }
321: }
|