001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.romaframework.wizard.command.project.impl;
017:
018: import org.romaframework.core.Utility;
019: import org.romaframework.core.io.virtualfile.VirtualFile;
020: import org.romaframework.wizard.MainWizard;
021: import org.romaframework.wizard.ProjectManager;
022: import org.romaframework.wizard.WizardConstants;
023: import org.romaframework.wizard.command.project.OptionalProjectBaseWizardCommand;
024: import org.romaframework.wizard.helper.FileManagementHelper;
025: import org.romaframework.wizard.properties.ProjectProperties;
026:
027: /**
028: * Generates CRUD classes starting from a domain class.
029: *
030: * @author Luca Garulli (luca.garulli@assetdata.it)
031: */
032: public class ProjectCrudWizard extends OptionalProjectBaseWizardCommand {
033:
034: public String getName() {
035: return "crud";
036: }
037:
038: public String getParameters() {
039: return super .getParameters()
040: + " <domain-class> [<output-class>]";
041: }
042:
043: public void execute(String[] iParameters) {
044: if (iParameters.length < 2)
045: syntaxError();
046:
047: iParameters = parseOptionalParameters(iParameters);
048:
049: // GET THE CLASS NAME AND PACKAGE
050: String[] classNames = extractPackageClassNames(iParameters[1]);
051:
052: String projectPath = ProjectManager.getInstance()
053: .getProjectPath(projectName);
054: ProjectProperties projProp = ProjectManager.getInstance()
055: .getProjectInfo(projectName);
056:
057: String crudClassName = classNames[1];
058: String classPackage;
059: if (iParameters.length > 2) {
060: classPackage = iParameters[2];
061: } else {
062: // GET PROJECT PACKAGE
063: classPackage = projProp
064: .getProperty(ProjectProperties.PACKAGE_VAR);
065: classPackage += ".view.domain.";
066: classPackage += crudClassName.toLowerCase();
067: }
068:
069: System.setProperty(WizardConstants.VAR_CRUD_CLASS,
070: crudClassName);
071: System.setProperty(WizardConstants.VAR_DOMAIN_PACKAGE,
072: classNames[0]);
073: System.setProperty(WizardConstants.VAR_DOMAIN_CLASS,
074: classNames[1]);
075: System.setProperty(WizardConstants.VAR_CLASS_PACKAGE,
076: classPackage);
077:
078: String destPath = projectPath + "/src/"
079: + Utility.getResourcePath(classPackage) + "/";
080:
081: String sourcePath = FileManagementHelper
082: .getVirtualPath(MainWizard.getRomaHome()
083: + "wizards/wizard-crud/template/");
084:
085: VirtualFile sourcePathFile = FileManagementHelper
086: .getVirtualFile(sourcePath);
087:
088: FileManagementHelper.executeCopy(getIO(), sourcePathFile,
089: destPath, false);
090: }
091:
092: private String[] extractPackageClassNames(String fullClassName) {
093: String[] classNames = new String[2];
094: int pos = fullClassName.lastIndexOf('.');
095: if (pos == -1) {
096: classNames[0] = "";
097: classNames[1] = fullClassName;
098: } else {
099: classNames[0] = fullClassName.substring(0, pos + 1);
100: classNames[1] = fullClassName.substring(pos + 1);
101: }
102: return classNames;
103: }
104:
105: public void help() {
106: super .help();
107:
108: StringBuffer buffer = new StringBuffer();
109:
110: buffer
111: .append(" <domain-class> is the domain class name (with full package) against to");
112: buffer.append("\n generate CRUD");
113: buffer
114: .append("\n <output-class> is the destination class name (without package)");
115: buffer.append("\n");
116: buffer.append("\nExamples:");
117: buffer.append("\n");
118: buffer.append("\nroma " + getName()
119: + " org.romaframework.demo.domain.Account");
120: buffer.append("\nroma " + getName()
121: + " org.romaframework.blog.domain.Blog BlogView");
122:
123: getIO().getError().println(buffer);
124: }
125: }
|