/* TODO: This implementation of Comparable is not consistent with equals()! */ public class PreferenceEntity implements Comparable, Serializable {
Log log = Logging.getLog(PreferenceEntity.class);
private Class clazz; private String description; private String entityName; private String mappedTo; private SortedSet<Property> properties = new TreeSet<Property>(); private Map<String, Property> propertiesByName = new HashMap<String,Property>(); private SortedSet<Property> propertiesSystemVisible = new TreeSet<Property>(); private SortedSet<Property> propertiesUserVisible = new TreeSet<Property>(); private SortedSet<Property> propertiesInstanceVisible = new TreeSet<Property>();
public PreferenceEntity(Class<?> entityClass) { if (!entityClass.isAnnotationPresent(Preferences.class)) { throw new RuntimeException("Configured as preferences but missing @Preferences: " + entityClass.getName()); }
// @PreferenceProperty fields for (Field field : entityClass.getDeclaredFields()) { if (field.isAnnotationPresent(PreferenceProperty.class)) { if (!field.isAccessible()) field.setAccessible(true);
if (property.isSystemVisible()) propertiesSystemVisible.add(property); if (property.isUserVisible()) propertiesUserVisible.add(property); if (property.isInstanceVisible()) propertiesInstanceVisible.add(property); properties.add(property); propertiesByName.put(property.getFieldName(), property); } } }
public Class getClazz() { return clazz; }
public String getDescription() { return description; }
public String getEntityName() { return entityName; }
public String getMappedTo() { return mappedTo; }
public SortedSet<Property> getProperties() { return properties; }
public Map<String, Property> getPropertiesByName() { return propertiesByName; }
public SortedSet<Property> getPropertiesSystemVisible() { return propertiesSystemVisible; }
public SortedSet<Property> getPropertiesUserVisible() { return propertiesUserVisible; }
public SortedSet<Property> getPropertiesInstanceVisible() { return propertiesInstanceVisible; }
public boolean isSystemPropertiesVisible() { return propertiesSystemVisible.size() != 0; }
public boolean isUserPropertiesVisible() { return propertiesUserVisible.size() != 0; }
public boolean isInstancePropertiesVisible() { return propertiesInstanceVisible.size() != 0; }
public Object materialize(Set<PreferenceValue> valueHolders) { log.trace("materializing preference entity instance: " + getEntityName()); Map<Property, InvalidValue[]> invalidProperties = validate(valueHolders); if (invalidProperties.size() > 0) { log.error("can't materialize preference entity: " + getEntityName()); for (Map.Entry<Property, InvalidValue[]> invalidProperty : invalidProperties.entrySet()) { log.error("invalid value for : " + invalidProperty.getKey()); for (InvalidValue invalidValue : invalidProperty.getValue()) { log.error("validation error: " + invalidValue.getMessage()); } } throw new RuntimeException("could not materialize preference entity '" + getEntityName() + "', check the log"); }
log.trace("validating properties with no values, checking for @NotNull by given visibility"); // Now validate all the properties for which we didn't have values, they need to be !@NotNull if (uncheckedProperties.size() > 0) { boolean uncheckedAreNullable = true; for (Property uncheckedProperty : uncheckedProperties) { if (uncheckedProperty.getVisibility().containsAll(visibilities) && !uncheckedProperty.isNullable()) { log.error("missing value for @NotNull " + uncheckedProperty); uncheckedAreNullable = false; } } if (!uncheckedAreNullable) { throw new IllegalStateException("missing values for validation of " + getEntityName() + ", check the log"); } }
return invalidProperties; }
public int compareTo(Object o) { return getDescription().compareTo(((PreferenceEntity) o).getDescription()); }