[OLINGO-261] Simplifying enum handling

This commit is contained in:
Francesco Chicchiriccò 2014-05-14 13:43:06 +02:00
parent 77e9e86d59
commit 99cd23fbcb
3 changed files with 15 additions and 42 deletions

View File

@ -34,8 +34,6 @@ import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.olingo.client.api.CommonEdmEnabledODataClient;
import org.apache.olingo.client.api.v3.UnsupportedInV3Exception;
import org.apache.olingo.commons.api.domain.CommonODataEntity;
@ -240,6 +238,17 @@ public final class CoreUtils {
}
}
@SuppressWarnings({"unchecked", "rawtypes"})
private static Enum<?> enumValueToObject(final ODataEnumValue value, final Class<?> reference) {
final Namespace namespace = reference.getAnnotation(Namespace.class);
final EnumType enumType = reference.getAnnotation(EnumType.class);
if (value.getTypeName().equals(namespace.value() + "." + enumType.name())) {
return Enum.valueOf((Class<Enum>) reference, value.getValue());
}
return null;
}
private static Object primitiveValueToObject(final ODataPrimitiveValue value, final Class<?> reference) {
Object obj;
@ -396,29 +405,6 @@ public final class CoreUtils {
}
}
@SuppressWarnings({"unchecked", "rawtypes"})
private static Enum<?> buildEnumInstance(final ODataEnumValue value) {
try {
for (String enumTypeName
: StringUtils.split(IOUtils.toString(CoreUtils.class.getResourceAsStream("/META-INF/enumTypes")), '\n')) {
final Class<Enum> enumClass =
(Class<Enum>) Thread.currentThread().getContextClassLoader().loadClass(enumTypeName);
if (enumClass != null) {
final Namespace namespace = enumClass.getAnnotation(Namespace.class);
final EnumType enumType = enumClass.getAnnotation(EnumType.class);
if (value.getTypeName().equals(namespace.value() + "." + enumType.name())) {
return Enum.valueOf(enumClass, value.getValue());
}
}
}
} catch (Exception e) {
LOG.error("While trying to load enum for {}", value, e);
}
return null;
}
@SuppressWarnings("unchecked")
public static Object getValueFromProperty(
final CommonEdmEnabledODataClient<?> client,
@ -469,7 +455,7 @@ public final class CoreUtils {
res = collection;
} else if (property instanceof ODataProperty && ((ODataProperty) property).hasEnumValue()) {
res = buildEnumInstance(((ODataProperty) property).getEnumValue());
res = enumValueToObject(((ODataProperty) property).getEnumValue(), internalRef);
} else {
res = primitiveValueToObject(property.getPrimitiveValue(), internalRef);
}

View File

@ -84,8 +84,9 @@ public abstract class AbstractPOJOGenMojo extends AbstractMojo {
protected abstract String getVersion();
protected File mkdir(final String path) {
final File dir = new File(outputDirectory + File.separator + TOOL_DIR + File.separator + path);
protected File mkPkgDir(final String path) {
final File dir = new File(outputDirectory + File.separator + TOOL_DIR + File.separator
+ basePackage.replace('.', File.separatorChar) + File.separator + path);
if (dir.exists()) {
if (!dir.isDirectory()) {
@ -98,10 +99,6 @@ public abstract class AbstractPOJOGenMojo extends AbstractMojo {
return dir;
}
protected File mkPkgDir(final String path) {
return mkdir(basePackage.replace('.', File.separatorChar) + File.separator + path);
}
protected void writeFile(final String name, final File path, final VelocityContext ctx, final Template template,
final boolean append) throws MojoExecutionException {
@ -227,8 +224,6 @@ public abstract class AbstractPOJOGenMojo extends AbstractMojo {
namespaces.add(schema.getNamespace().toLowerCase());
}
final StringBuilder enumTypeNames = new StringBuilder();
for (EdmSchema schema : edm.getSchemas()) {
createUtility(edm, schema, basePackage);
@ -248,7 +243,6 @@ public abstract class AbstractPOJOGenMojo extends AbstractMojo {
// write types into types package
for (EdmEnumType enumType : schema.getEnumTypes()) {
final String className = utility.capitalize(enumType.getName());
enumTypeNames.append(typesPkg).append('.').append(className).append('\n');
objs.clear();
objs.put("enumType", enumType);
parseObj(typesBaseDir, typesPkg, "enumType", className + ".java", objs);
@ -324,9 +318,6 @@ public abstract class AbstractPOJOGenMojo extends AbstractMojo {
}
}
}
final File metaInf = mkdir("META-INF");
FileUtils.fileWrite(metaInf.getPath() + File.separator + "enumTypes", enumTypeNames.toString());
} catch (Exception t) {
getLog().error(t);
@ -335,5 +326,4 @@ public abstract class AbstractPOJOGenMojo extends AbstractMojo {
: new MojoExecutionException("While executin mojo", t);
}
}
}

View File

@ -1,3 +0,0 @@
org.apache.olingo.fit.proxy.v4.staticservice.microsoft.test.odata.services.odatawcfservice.types.AccessLevel
org.apache.olingo.fit.proxy.v4.staticservice.microsoft.test.odata.services.odatawcfservice.types.Color
org.apache.olingo.fit.proxy.v4.staticservice.microsoft.test.odata.services.odatawcfservice.types.CompanyCategory