org.efaps.maven.plugin.install.GenerateJmsClassMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of efaps-maven-plugin Show documentation
Show all versions of efaps-maven-plugin Show documentation
eFaps Maven Plug-In to install / deploy eFaps applications and create CI/Jms Classes.
/*
z * Copyright 2003 - 2012 The eFaps Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Revision: $Rev: 7685 $
* Last Changed: $Date: 2012-06-18 10:34:35 -0500 (Mon, 18 Jun 2012) $
* Last Changed By: $Author: [email protected] $
*/
package org.efaps.maven.plugin.install;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.apache.commons.digester3.Digester;
import org.apache.commons.digester3.annotations.FromAnnotationsRuleModule;
import org.apache.commons.digester3.binder.DigesterLoader;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.efaps.maven.plugin.install.digester.AttributeCI;
import org.efaps.maven.plugin.install.digester.ITypeDefintion;
import org.efaps.maven.plugin.install.digester.StatusCI;
import org.efaps.maven.plugin.install.digester.TypeCI;
import org.efaps.update.FileType;
import org.efaps.update.Install.InstallFile;
import org.efaps.update.version.Application;
import org.efaps.update.version.Dependency;
import org.jfrog.maven.annomojo.annotations.MojoGoal;
import org.jfrog.maven.annomojo.annotations.MojoParameter;
import org.jfrog.maven.annomojo.annotations.MojoPhase;
import org.jfrog.maven.annomojo.annotations.MojoRequiresDependencyResolution;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
* TODO comment!
*
* @author The eFaps Team
* @version $Id: GenerateJmsClassMojo.java 7685 2012-06-18 15:34:35Z [email protected] $
*/
@MojoGoal(value = "generate-jmsclass")
@MojoRequiresDependencyResolution(value = "compile")
@MojoPhase(value = "generate-sources")
public class GenerateJmsClassMojo
extends AbstractEFapsInstallMojo
{
/**
* The base package name.
*/
@MojoParameter(required = true, defaultValue = "org.efaps.esjp.jms")
private String jmsPackage;
/**
* This Regex will be used to replace the application name
* with {@link #jmsPackageReplacement}.
*/
@MojoParameter(required = true, defaultValue = "eFaps-|eFapsApp-",
description = "This Regex will be used to replace the application name with jmsPackageReplacement")
private String jmsPackageRegex;
/**
* The replacement String used in conjunction with {@link #jmsPackageRegex}.
*/
@MojoParameter(defaultValue = "",
description = "The replacement String used in conjunction with jmsPackageRegex")
private final String jmsPackageReplacement;
/**
* This Regex will be used to replace the Classname result
* with {@link #jmsClassNameReplacement}.
*/
@MojoParameter(required = true, defaultValue = "^[A-Za-z]+_")
private String jmsClassNameRegex;
/**
* The replacement String used in conjunction with {@link #jmsClassNameRegex}.
*/
@MojoParameter(defaultValue = "",
description = "The replacement String used in conjunction with jmsClassNameRegex")
private final String jmsClassNameReplacement;
/**
* The current Maven project.
*/
@MojoParameter(defaultValue = "${project}", required = true, readonly = true)
private MavenProject project;
/**
* Mapping between the Types of eFaps and the package the responding class is in.
*/
private final Map type2package = new HashMap();
/**
* Mapping between the Types of eFaps and the responding class.
*/
private final Map type2ClassName = new TreeMap();
/**
* Constructor setting empty string defautl values.
*/
public GenerateJmsClassMojo()
{
this.jmsClassNameReplacement = "";
this.jmsPackageReplacement = "";
}
/**
* Executes the install goal.
*
* @throws MojoExecutionException if installation failed
*/
public void execute()
throws MojoExecutionException
{
try {
init(false);
getOutputDirectory().mkdir();
final String folders = this.jmsPackage.replace(".", File.separator);
final File srcFolder = new File(getOutputDirectory(), folders);
srcFolder.mkdirs();
final Application appl = Application.getApplicationFromSource(
getVersionFile(),
getClasspathElements(),
getEFapsDir(),
getOutputDirectory(),
getIncludes(),
getExcludes(),
getTypeMapping());
for (final Dependency dependency : appl.getDependencies()) {
dependency.resolve();
final Application dependApp = Application.getApplicationFromJarFile(
dependency.getJarFile(), getClasspathElements());
final List files = dependApp.getInstall().getFiles();
final String applicationName = dependApp.getApplication().replaceAll(this.jmsPackageRegex, "")
.toLowerCase();
for (final InstallFile file : files) {
if (file.getType().equals(FileType.XML)) {
readFile(applicationName, srcFolder, file);
}
}
}
final List files = appl.getInstall().getFiles();
final String applicationName = appl.getApplication().replaceAll(this.jmsPackageRegex, "").toLowerCase();
for (final InstallFile file : files) {
if (file.getType().equals(FileType.XML)) {
readFile(applicationName, srcFolder, file);
}
}
this.project.addCompileSourceRoot(getOutputDirectory().getAbsolutePath());
} catch (final Exception e) {
throw new MojoExecutionException("Could not execute SourceInstall script", e);
}
}
private void readFile(final String _applicationName,
final File _srcFolder,
final InstallFile _file)
throws MojoExecutionException
{
try {
final DigesterLoader loader = DigesterLoader.newLoader(new FromAnnotationsRuleModule()
{
@Override
protected void configureRules()
{
bindRulesFrom(TypeCI.class);
bindRulesFrom(StatusCI.class);
}
});
final Digester digester = loader.newDigester();
final URLConnection connection = _file.getUrl().openConnection();
connection.setUseCaches(false);
final InputStream stream = connection.getInputStream();
final InputSource source = new InputSource(stream);
final Object item = digester.parse(source);
stream.close();
if (item != null) {
if (item instanceof TypeCI) {
final TypeCI typeItem = ((TypeCI) item);
final String packageName = typeItem.getPackageName(this.jmsPackageRegex, this.jmsPackageReplacement);
final String className = typeItem.getClassName(this.jmsClassNameRegex, this.jmsClassNameReplacement);
this.type2package.put(typeItem.getName(), packageName);
this.type2ClassName.put(typeItem.getName(), "org.efaps.esjp.jms."
+ packageName + "." + className);
final File folder = new File(_srcFolder, packageName);
folder.mkdirs();
final File javaFile = new File(folder, className + ".java");
FileUtils.writeStringToFile(javaFile, getJava(typeItem));
}
}
} catch (final SAXException e) {
throw new MojoExecutionException("Could not execute SourceInstall script", e);
} catch (final IOException e) {
throw new MojoExecutionException("Could not execute SourceInstall script", e);
}
}
private String getJava(final TypeCI _typeCI)
{
final StringBuilder ret = new StringBuilder();
final String packageName = _typeCI.getPackageName(this.jmsPackageRegex, this.jmsPackageReplacement);
final String className = _typeCI.getClassName(this.jmsClassNameRegex, this.jmsClassNameReplacement);
ret.append("package org.efaps.esjp.jms.").append(packageName).append(";\n\n")
.append("import javax.xml.bind.annotation.XmlAccessType;\n")
.append("import javax.xml.bind.annotation.XmlAccessorType;\n")
.append("import javax.xml.bind.annotation.XmlElement;\n")
.append("import javax.xml.bind.annotation.XmlElementWrapper;\n")
.append("import javax.xml.bind.annotation.XmlElements;\n")
.append("import javax.xml.bind.annotation.XmlRootElement;\n")
.append("import javax.xml.bind.annotation.XmlType;\n")
.append("import org.efaps.esjp.jms.AbstractObject;\n")
.append("import org.efaps.esjp.jms.AbstractClassificationObject;\n")
.append("import org.efaps.esjp.jms.annotation.*;\n")
.append("import org.efaps.esjp.jms.attributes.*;\n\n")
.append("@XmlAccessorType(XmlAccessType.NONE)\n")
.append("@XmlRootElement(name = \"").append(_typeCI.getName()).append("\")\n")
.append("@XmlType(name = \"").append(packageName).append(".")
.append(_typeCI.getName()).append("\")\n")
.append("@Type(uuid = \"").append(_typeCI.getUuid()).append("\")\n")
.append("public ").append(_typeCI.isAbstract() ? "abstract " : "").append("class ")
.append(className).append("\n");
final String extendStr;
if (_typeCI.getParent() == null || _typeCI.getParent().equals("Admin_Abstract")) {
if (_typeCI.isClassification()) {
extendStr = "AbstractClassificationObject";
} else {
extendStr = "AbstractObject";
}
} else if (GenerateJmsClassMojo.this.type2package.containsKey(_typeCI.getParent())
&& !packageName.equals(GenerateJmsClassMojo.this.type2package.get(_typeCI.getParent()))) {
extendStr = GenerateJmsClassMojo.this.jmsPackage + "."
+ GenerateJmsClassMojo.this.type2package.get(_typeCI.getParent()) + "."
+ _typeCI.getParent().replaceAll(GenerateJmsClassMojo.this.jmsClassNameRegex, "");
} else {
extendStr = _typeCI.getParent().replaceAll(GenerateJmsClassMojo.this.jmsClassNameRegex, "");
}
ret.append(" extends ").append(extendStr).append("\n")
.append("{\n");
final StringBuilder getter = new StringBuilder();
final Map> attributes = new TreeMap>();
for (final ITypeDefintion typeDef : _typeCI.getDefinitions()) {
for (final AttributeCI attribute : typeDef.getAttributes()) {
List profiles;
if (attributes.containsKey(attribute)) {
profiles = attributes.get(attribute);
} else {
profiles = new ArrayList();
}
profiles.addAll(typeDef.getProfiles());
attributes.put(attribute, profiles);
}
}
for (final Entry> entry : attributes.entrySet()) {
if (!"Type".equals(entry.getKey().getType())
&& !"OID".equals(entry.getKey().getName()) && !"ID".equals(entry.getKey().getName())) {
final StringBuilder profiles = new StringBuilder();
if (!entry.getValue().isEmpty()) {
profiles.append(", profiles = {");
boolean first = true;
for (final String profile : entry.getValue()) {
if (first) {
first = false;
} else {
profiles.append(", ");
}
profiles.append("\"").append(profile).append("\"");
}
profiles.append("} ");
}
final StringBuilder setter = new StringBuilder();
ret
.append(" @XmlElement(name = \"").append(entry.getKey().getName().toLowerCase()).append("\")\n")
.append(" private ");
getter
.append(" @Attribute(name = \"").append(entry.getKey().getName())
.append("\", method = MethodType.GETTER").append(profiles.length() > 0 ? profiles : "" )
.append(")\n")
.append(" public ");
setter
.append(" @Attribute(name = \"").append(entry.getKey().getName())
.append("\", method = MethodType.SETTER").append(profiles.length() > 0 ? profiles : "" )
.append(")\n")
.append(" public void ");
final String attrTypeTmp;
if ("String".equals(entry.getKey().getType())) {
attrTypeTmp = "StringAttribute ";
} else if ("Long".equals(entry.getKey().getType())){
attrTypeTmp = "LongAttribute ";
} else if ("Integer".equals(entry.getKey().getType())){
attrTypeTmp = "IntegerAttribute ";
} else if ("Link".equals(entry.getKey().getType())){
attrTypeTmp = "LinkAttribute ";
} else if ("LinkWithRanges".equals(entry.getKey().getType())){
attrTypeTmp = "LinkAttribute ";
} else if ("Decimal".equals(entry.getKey().getType())){
attrTypeTmp = "DecimalAttribute ";
} else if ("Date".equals(entry.getKey().getType())){
attrTypeTmp = "DateAttribute ";
} else if ("DateTime".equals(entry.getKey().getType())){
attrTypeTmp = "DateTimeAttribute ";
} else if ("Status".equals(entry.getKey().getType())){
attrTypeTmp = "StatusAttribute ";
} else if ("Rate".equals(entry.getKey().getType())){
attrTypeTmp = "RateAttribute ";
} else {
attrTypeTmp = "StringAttribute ";
}
String instanceVariable = entry.getKey().getName();
if ("Class".equalsIgnoreCase(instanceVariable)) {
instanceVariable = "Clazz";
} else if ("Abstract".equalsIgnoreCase(instanceVariable)) {
instanceVariable = "AbstractV";
} else if ("Default".equalsIgnoreCase(instanceVariable)) {
instanceVariable = "DefaultV";
}
ret
.append(attrTypeTmp).append(instanceVariable.toLowerCase()).append(";\n\n");
getter
.append(attrTypeTmp).append("get").append(instanceVariable).append("()\n")
.append(" {\n")
.append(" return this.").append(instanceVariable.toLowerCase()).append(";\n")
.append(" }\n\n");
setter
.append("set").append(instanceVariable).append("(final ").append(attrTypeTmp).append("_")
.append(instanceVariable.toLowerCase()).append(")\n")
.append(" {\n")
.append(" this.").append(instanceVariable.toLowerCase()).append(" = _")
.append(instanceVariable.toLowerCase()).append(";\n")
.append(" }\n\n");
getter.append(setter);
}
}
ret.append(getter).append("}\n");
return ret.toString();
}
}