public void updateCustomField(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 updated is invalid: " + field); throw new ApplicationException( "Unable to update custom field with an 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 update a field that does not exist."); throw new ApplicationException("Unable to update an unknown custom field definition."); } attrib.setFunctionParameters(serializeParameters(field.getConfigurationParameters())); attrib.setDateChanged(new Date()); attrib.setSourceName(field.getSourceFieldName()); attrib.setTransformationFunction(field.getTransformationFunctionName()); attrib.setUserChangedBy(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_UPDATE_EVENT, field); } catch (DaoException e) { log.error("Failed while updating a custom field: " + e, e); throw new ApplicationException("Failed while updating 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; }