private void preloadCache() { List<Entity> entities = loadEntities(); log.info("Pre-loaded configuration for " + entities.size() + " entities."); Map<String, List<CustomField>> customFieldsListByEntityName = new HashMap<String, List<CustomField>>(); Map<String, Map<String, CustomField>> customFieldsMapByEntityName = new HashMap<String, Map<String, CustomField>>(); for (Entity entity : entities) { List<CustomField> customFields = loadCustomFields(entity.getName()); customFieldsListByEntityName.put(entity.getName(), customFields); log.info( "Pre-loaded a list of " + customFields.size() + " custom fields for entity " + entity.getName()); Map<String, CustomField> customFieldMap = new HashMap<String, CustomField>(); for (CustomField field : customFields) { customFieldMap.put(field.getFieldName(), field); } customFieldsMapByEntityName.put(entity.getName(), customFieldMap); Context.getConfiguration() .registerConfigurationEntry( entity.getName(), ConfigurationRegistry.CUSTOM_FIELD_LIST_BY_ENTITY_NAME_MAP, customFieldsListByEntityName); Context.getConfiguration() .registerConfigurationEntry( entity.getName(), ConfigurationRegistry.CUSTOM_FIELD_MAP_BY_ENTITY_NAME_MAP, customFieldsMapByEntityName); } }
public void deleteCustomField(CustomField field) throws ApplicationException { if (field == null || ConvertUtil.isNullOrEmpty(field.getEntityName()) || ConvertUtil.isNullOrEmpty(field.getFieldName())) { log.warn("The custom field to be deleted is invalid: " + field); throw new ApplicationException("Unable to add invalid custom field definition."); } Entity entity = findLatestEntityVersionByName(field.getEntityName()); EntityAttribute attrib = null; for (EntityAttribute item : entity.getAttributes()) { if (item.getName().equalsIgnoreCase(field.getFieldName()) && item.getDateVoided() == null) { attrib = item; } } if (attrib == null) { log.info("The user attempted to delete a custom field that does not exist."); throw new ApplicationException("Unable to delete an unknown custom field definition."); } attrib.setDateVoided(new Date()); attrib.setUserVoidedBy(Context.getUserContext().getUser()); try { entityDefinitionDao.updateEntity(entity); // Generate a notification event to inform interested listeners via the lightweight mechanism // that this event has occurred. Context.notifyObserver(ObservationEventType.CUSTOM_FIELD_DELETE_EVENT, field); } catch (DaoException e) { log.error("Failed while deleting a custom field: " + e, e); throw new ApplicationException("Failed while deleting the custom field: " + e.getMessage()); } }
private EntityAttribute buildAttributeFromCustomField(Entity entity, CustomField field) { EntityAttribute attrib = new EntityAttribute(); EntityAttributeDatatype type = getDatatypeByCode(EntityAttributeDatatype.STRING_DATATYPE_CD); attrib.setDatatype(type); attrib.setDateCreated(new Date()); attrib.setDescription(field.getFieldName()); attrib.setDisplayName(field.getFieldName()); attrib.setDisplayOrder(1000); attrib.setEntity(entity); attrib.setFunctionParameters(serializeParameters(field.getConfigurationParameters())); attrib.setIndexed(false); attrib.setSearchable(false); attrib.setCaseInsensitive(false); attrib.setIsCustom(true); attrib.setName(field.getFieldName()); attrib.setSourceName(field.getSourceFieldName()); attrib.setTransformationFunction(field.getTransformationFunctionName()); attrib.setUserCreatedBy(Context.getUserContext().getUser()); return attrib; }
private CustomField buildCustomFieldFromAttribute(EntityAttribute attrib) { CustomField field = new CustomField(); field.setEntityName(attrib.getEntity().getName()); field.setFieldName(attrib.getName()); field.setSourceFieldName(attrib.getSourceName()); field.setTransformationFunctionName(attrib.getTransformationFunction()); if (attrib.getFunctionParameters() != null) { field.setConfigurationParameters(deserializeParameters(attrib.getFunctionParameters())); } return field; }
public CustomField addCustomField(CustomField field) throws ApplicationException { if (field == null || ConvertUtil.isNullOrEmpty(field.getEntityName()) || ConvertUtil.isNullOrEmpty(field.getFieldName()) || ConvertUtil.isNullOrEmpty(field.getSourceFieldName())) { log.info("The custom field to be added is invalid: " + field); throw new ApplicationException("Unable to add invalid custom field definition."); } // Validate that a custom field by the same name does not already exist. List<CustomField> fields = loadCustomFields(field.getEntityName()); for (CustomField custom : fields) { if (custom.getFieldName().equalsIgnoreCase(field.getFieldName())) { log.warn( "User attempted to add a custom field " + field + " that already exists in the system."); throw new ApplicationException( "Unable to add a custom field that already exists in the system."); } } Entity entity = findLatestEntityVersionByName(field.getEntityName()); try { EntityAttribute attribute = buildAttributeFromCustomField(entity, field); entity.addAttribute(attribute); // TODO: Validate that the source field is known // TODO: Validate that the transformation function is known attribute = entityDefinitionDao.addCustomField(entity, attribute); // Generate a notification event to inform interested listeners via the lightweight mechanism // that this event has occurred. Context.notifyObserver(ObservationEventType.CUSTOM_FIELD_ADD_EVENT, field); Context.notifyObserver(ObservationEventType.ENTITY_ATTRIBUTE_UPDATE_EVENT, entity); return field; } catch (DaoException e) { log.error("Failed while saving a custom field: " + e, e); throw new ApplicationException("Failed while saving the custom field: " + e.getMessage()); } }
@SuppressWarnings("unchecked") public void update(Observable o, Object eventData) { if (!(o instanceof EventObservable) || eventData == null || !(eventData instanceof CustomField)) { log.warn("Received unexpected event with data of " + eventData); return; } EventObservable event = (EventObservable) o; CustomField customField = (CustomField) eventData; Map<String, List<CustomField>> customFieldsListByEntityName = (Map<String, List<CustomField>>) Context.getConfiguration() .lookupConfigurationEntry( customField.getEntityName(), ConfigurationRegistry.CUSTOM_FIELD_LIST_BY_ENTITY_NAME_MAP); Map<String, Map<String, CustomField>> customFieldsMapByEntityName = (Map<String, Map<String, CustomField>>) Context.getConfiguration() .lookupConfigurationEntry( customField.getEntityName(), ConfigurationRegistry.CUSTOM_FIELD_MAP_BY_ENTITY_NAME_MAP); List<CustomField> fieldList = customFieldsListByEntityName.get(customField.getEntityName()); Map<String, CustomField> fieldMap = customFieldsMapByEntityName.get(customField.getEntityName()); if (event.getType() == ObservationEventType.CUSTOM_FIELD_ADD_EVENT) { log.debug( "A new custom field was added; we need to update the in-memory registry: " + customField); fieldList.add(customField); fieldMap.put(customField.getFieldName(), customField); } else if (event.getType() == ObservationEventType.CUSTOM_FIELD_UPDATE_EVENT) { log.debug( "A custom field was updated; we need to update the in-memory registry: " + customField); boolean found = false; for (CustomField item : fieldList) { if (item.getFieldName().equals(customField.getFieldName())) { found = true; item.setConfigurationParameters(customField.getConfigurationParameters()); item.setSourceFieldName(customField.getSourceFieldName()); item.setTransformationFunctionName(customField.getTransformationFunctionName()); log.debug( "As a result of an update custom field event, update field " + item + " in the list."); } } if (!found) { log.warn( "Received an event to update an existing custom field but the field is not found in the registry."); } fieldMap.put(customField.getFieldName(), customField); } else if (event.getType() == ObservationEventType.CUSTOM_FIELD_DELETE_EVENT) { log.debug( "A new custom field was deleted; we need to update the in-memory registry: " + customField); CustomField foundField = null; for (CustomField item : fieldList) { if (item.getFieldName().equals(customField.getFieldName())) { foundField = item; } } if (foundField != null) { fieldList.remove(foundField); log.debug( "As a result of a delete custom field event, deleted field " + foundField + " from the list."); } fieldMap.remove(customField.getFieldName()); } }