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()); } }