fix ProeprtyService

This commit is contained in:
Peter Hormanns 2017-04-18 16:56:33 +02:00
parent 0f16953a98
commit c763facb95
2 changed files with 55 additions and 45 deletions

View File

@ -104,7 +104,7 @@ public abstract class AbstractVO implements ValueObject {
} }
private boolean isPropertyField(final Field f) { private boolean isPropertyField(final Field f) {
ReadWrite annotation = f.getAnnotation(ReadWrite.class); final ReadWrite annotation = f.getAnnotation(ReadWrite.class);
return annotation != null; return annotation != null;
} }
@ -120,12 +120,15 @@ public abstract class AbstractVO implements ValueObject {
final Class<?> type = Class.forName("de.hsadmin.module.property." + simplePropertyClassName + "Property"); final Class<?> type = Class.forName("de.hsadmin.module.property." + simplePropertyClassName + "Property");
final Constructor<?> constructor = type.getConstructor(ValueObject.class, String.class, ReadWritePolicy.class, SearchPolicy.class, boolean.class); final Constructor<?> constructor = type.getConstructor(ValueObject.class, String.class, ReadWritePolicy.class, SearchPolicy.class, boolean.class);
final Property<?> newInstance = (Property<?>) constructor.newInstance(this, f.getName(), getReadWritePolicy(f), getSearchPolicy(f), isRequired(f)); final Property<?> newInstance = (Property<?>) constructor.newInstance(this, f.getName(), getReadWritePolicy(f), getSearchPolicy(f), isRequired(f));
set(f.getName(), newInstance); final String propertyName = f.getName();
assert propertyName != null && propertyName.length() > 0;
assert newInstance != null;
propertiesMap.put(propertyName, newInstance);
if (ContainsElements.class.isAssignableFrom(type)) { if (ContainsElements.class.isAssignableFrom(type)) {
ContainsElements container = (ContainsElements) newInstance; ContainsElements container = (ContainsElements) newInstance;
container.setElementsType(getElementsType(f)); container.setElementsType(getElementsType(f));
} }
Mapping mapping = f.getAnnotation(Mapping.class); final Mapping mapping = f.getAnnotation(Mapping.class);
if (mapping != null && newInstance instanceof AbstractProperty<?>) { if (mapping != null && newInstance instanceof AbstractProperty<?>) {
AbstractProperty<?> prop = (AbstractProperty<?>) newInstance; AbstractProperty<?> prop = (AbstractProperty<?>) newInstance;
PersistentObjectMapper<?> persistentObjectMapper = mapping.boMapping().newInstance(); PersistentObjectMapper<?> persistentObjectMapper = mapping.boMapping().newInstance();
@ -137,17 +140,11 @@ public abstract class AbstractVO implements ValueObject {
} }
} }
} catch (IllegalArgumentException | IllegalAccessException | InstantiationException | InvocationTargetException } catch (IllegalArgumentException | IllegalAccessException | InstantiationException | InvocationTargetException
| NoSuchMethodException | SecurityException | UserException | ClassNotFoundException e) { | NoSuchMethodException | SecurityException | ClassNotFoundException e) {
throw new TechnicalException(e); throw new TechnicalException(e);
} }
} }
private void set(final String propertyName, final Property<?> propertyInstance) throws UserException {
assert propertyName != null && propertyName.length() > 0;
assert propertyInstance != null;
propertiesMap.put(propertyName, propertyInstance);
}
private SearchPolicy getSearchPolicy(final Field f) { private SearchPolicy getSearchPolicy(final Field f) {
final Search search = f.getAnnotation(Search.class); final Search search = f.getAnnotation(Search.class);
SearchPolicy searchPolicy = SearchPolicy.NONE; SearchPolicy searchPolicy = SearchPolicy.NONE;

View File

@ -1,7 +1,6 @@
package de.hsadmin.service.property; package de.hsadmin.service.property;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
@ -14,6 +13,7 @@ import javax.validation.constraints.Size;
import de.hsadmin.common.error.TechnicalException; import de.hsadmin.common.error.TechnicalException;
import de.hsadmin.common.error.UserException; import de.hsadmin.common.error.UserException;
import de.hsadmin.common.util.ReflectionUtil;
import de.hsadmin.login.RequestContext; import de.hsadmin.login.RequestContext;
import de.hsadmin.login.RequiredScope; import de.hsadmin.login.RequiredScope;
import de.hsadmin.login.Role; import de.hsadmin.login.Role;
@ -22,6 +22,7 @@ import de.hsadmin.module.ValueObject;
import de.hsadmin.module.impl.AbstractModule; import de.hsadmin.module.impl.AbstractModule;
import de.hsadmin.module.property.Display; import de.hsadmin.module.property.Display;
import de.hsadmin.module.property.DisplayPolicy; import de.hsadmin.module.property.DisplayPolicy;
import de.hsadmin.module.property.Property;
import de.hsadmin.module.property.ReadWrite; import de.hsadmin.module.property.ReadWrite;
import de.hsadmin.module.property.ReadWritePolicy; import de.hsadmin.module.property.ReadWritePolicy;
import de.hsadmin.module.property.Search; import de.hsadmin.module.property.Search;
@ -31,6 +32,8 @@ import de.hsadmin.xmlrpc.AbstractRemote;
@Stateless @Stateless
public class PropertyService extends AbstractModule<PropertyVO> implements PropertyServiceLocal { public class PropertyService extends AbstractModule<PropertyVO> implements PropertyServiceLocal {
public static final String DEFAULT_VALIDATION_REGEXP = "[a-zA-Z0-9\\_\\-\\.\\,\\ ]*";
@PersistenceContext(name="hsar") @PersistenceContext(name="hsar")
private EntityManager entityManager; private EntityManager entityManager;
@ -47,6 +50,7 @@ public class PropertyService extends AbstractModule<PropertyVO> implements Prope
} }
@Override @Override
@SuppressWarnings("unchecked")
@RequiredScope({@ScopePolicy(Role.ANY)}) @RequiredScope({@ScopePolicy(Role.ANY)})
public List<PropertyVO> read(final RequestContext requestContext, final PropertyVO criteria) public List<PropertyVO> read(final RequestContext requestContext, final PropertyVO criteria)
throws UserException, TechnicalException { throws UserException, TechnicalException {
@ -55,60 +59,57 @@ public class PropertyService extends AbstractModule<PropertyVO> implements Prope
final String requestedModuleName = criteria.getModule(); final String requestedModuleName = criteria.getModule();
try { try {
remoteServicesProperties.load(getClass().getClassLoader().getResourceAsStream("org/apache/xmlrpc/webserver/XmlRpcServlet.properties")); remoteServicesProperties.load(getClass().getClassLoader().getResourceAsStream("org/apache/xmlrpc/webserver/XmlRpcServlet.properties"));
final Enumeration<?> propertyNames = remoteServicesProperties.propertyNames(); final Enumeration<?> remoteModuleNames = remoteServicesProperties.propertyNames();
while (propertyNames.hasMoreElements()) { while (remoteModuleNames.hasMoreElements()) {
final String properyName = (String) propertyNames.nextElement(); final String moduleName = (String) remoteModuleNames.nextElement();
if (requestedModuleName != null && !requestedModuleName.equals(properyName)) { if (requestedModuleName != null && !requestedModuleName.equals(moduleName)) {
continue; continue;
} }
final Class<?> serviceRemoteClass = Class.forName(remoteServicesProperties.getProperty(properyName)); final Class<?> serviceRemoteClass = Class.forName(remoteServicesProperties.getProperty(moduleName));
@SuppressWarnings("unchecked")
final AbstractRemote<ValueObject> serviceRemote = (AbstractRemote<ValueObject>) serviceRemoteClass.newInstance(); final AbstractRemote<ValueObject> serviceRemote = (AbstractRemote<ValueObject>) serviceRemoteClass.newInstance();
final ValueObject valueObject = serviceRemote.createValueObject(); final ValueObject valueObject = serviceRemote.createValueObject();
final Field[] declaredFields = valueObject.getClass().getDeclaredFields(); final Class<? extends ValueObject> voClass = valueObject.getClass();
for (Field f : declaredFields) { final List<Property<?>> propertiesList = valueObject.properties();
final String fName = f.getName(); for (final Property<?> prop : propertiesList) {
if (fName.startsWith("$")) {
continue;
}
final PropertyVO vo = buildVO(); final PropertyVO vo = buildVO();
vo.setModule(properyName); final String propName = prop.getName();
vo.setName(fName); setStringValue(vo, "module", moduleName);
final ReadWrite readWrite = f.getAnnotation(ReadWrite.class); setStringValue(vo, "name", propName);
final ReadWrite readWrite = (ReadWrite) ReflectionUtil.getAnnotation(voClass, propName, ReadWrite.class);
if (readWrite == null) { if (readWrite == null) {
vo.setReadwriteable(ReadWritePolicy.NONE.name().toLowerCase()); setStringValue(vo, "readwriteable", ReadWritePolicy.NONE.name().toLowerCase());
} else { } else {
vo.setReadwriteable(readWrite.value().name().toLowerCase()); setStringValue(vo, "readwriteable", readWrite.value().name().toLowerCase());
} }
final Search search = f.getAnnotation(Search.class); final Search search = (Search) ReflectionUtil.getAnnotation(voClass, propName, Search.class);
if (search == null) { if (search == null) {
vo.setSearchable(SearchPolicy.NONE.name().toLowerCase()); setStringValue(vo, "searchable", SearchPolicy.NONE.name().toLowerCase());
} else { } else {
vo.setSearchable(search.value().name().toLowerCase()); setStringValue(vo, "searchable", search.value().name().toLowerCase());
} }
final Display sequence = f.getAnnotation(Display.class); final Display sequence = (Display) ReflectionUtil.getAnnotation(voClass, propName, Display.class);
if (sequence == null) { if (sequence == null) {
vo.setDisplaySequence(Integer.valueOf(9999)); setIntValue(vo, "displaySequence", 9999);
vo.setDisplayVisible(DisplayPolicy.ALWAYS.name().toLowerCase());; setStringValue(vo, "displayVisible", DisplayPolicy.ALWAYS.name().toLowerCase());
} else { } else {
vo.setDisplaySequence(sequence.sequence()); setIntValue(vo, "displaySequence", sequence.sequence());
vo.setDisplayVisible(sequence.visible().name().toLowerCase());; setStringValue(vo, "displayVisible", sequence.visible().name().toLowerCase());
} }
final Pattern pattern = f.getAnnotation(Pattern.class); final Pattern pattern = (Pattern) ReflectionUtil.getAnnotation(voClass, propName, Pattern.class);
if (pattern == null) { if (pattern == null) {
vo.setValidationRegexp("[a-zA-Z0-9\\_\\-\\.\\,\\ ]*"); setStringValue(vo, "validationRegexp", DEFAULT_VALIDATION_REGEXP);
} else { } else {
vo.setValidationRegexp(pattern.regexp()); setStringValue(vo, "validationRegexp", pattern.regexp());
} }
final Size size = f.getAnnotation(Size.class); final Size size = (Size) ReflectionUtil.getAnnotation(voClass, propName, Size.class);
if (size == null) { if (size == null) {
vo.setMinLength(Integer.valueOf(0)); setIntValue(vo, "minLength", 0);
vo.setMaxLength(Integer.valueOf(999)); setIntValue(vo, "maxLength", 999);
} else { } else {
vo.setMinLength(size.min()); setIntValue(vo, "minLength", size.min());
vo.setMaxLength(size.max()); setIntValue(vo, "maxLength", size.max());
} }
vo.setType(printableTypeName(valueObject.get(fName).getValueType())); setStringValue(vo, "type", printableTypeName(valueObject.get(propName).getValueType()));
emptyList.add(vo); emptyList.add(vo);
} }
} }
@ -118,6 +119,18 @@ public class PropertyService extends AbstractModule<PropertyVO> implements Prope
return emptyList; return emptyList;
} }
private void setStringValue(final PropertyVO vo, final String propertyName, final String value) throws TechnicalException, UserException {
@SuppressWarnings("unchecked")
final Property<String> property = (Property<String>) vo.get(propertyName);
property.setValue(value);
}
private void setIntValue(final PropertyVO vo, final String propertyName, final int value) throws TechnicalException, UserException {
@SuppressWarnings("unchecked")
final Property<Integer> property = (Property<Integer>) vo.get(propertyName);
property.setValue(value);
}
private String printableTypeName(Class<?> type) { private String printableTypeName(Class<?> type) {
String name = type.getName().toLowerCase(); String name = type.getName().toLowerCase();
if (name.indexOf('.') >= 0) { if (name.indexOf('.') >= 0) {