private void validateNameUniqueness(Entity entity) throws ApplicationException {
    @SuppressWarnings("rawtypes")
    List keys = entityDefinitionCache.getKeys();
    for (int i = 0; i < keys.size(); i++) {
      Object key = keys.get(i);
      Element elem = entityDefinitionCache.get(key);
      if (elem != null) {
        Entity item = (Entity) elem.getObjectValue();
        if (item.getDateVoided() != null) {
          continue;
        }

        if (entity.getName().equalsIgnoreCase(item.getName())
            && entity.getVersionId() == item.getVersionId()) {
          throw new ApplicationException(
              "Cannot add entity because another entity by the same name already exists.");
        }
      }
    }
  }
 public Entity loadEntityByName(String entityName) {
   @SuppressWarnings("unchecked")
   List<Object> keys = entityDefinitionCache.getKeys();
   for (Object key : keys) {
     Entity entity = getFromCache((Integer) key);
     if (entity.getName().equals(entityName)) {
       return entity;
     }
   }
   Entity entity = findLatestEntityVersionByName(entityName);
   return entity;
 }
 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);
   }
 }
  /** Will import the entity definition specified in the relative filename of the parameter. */
  public void importEntity(String filename) throws ApplicationException {
    if (filename.isEmpty()) {
      return;
    }

    try {
      File file = new File(filename);
      log.debug("Loading file " + file.getAbsolutePath());
      if (!file.isFile() || !file.canRead()) {
        log.error("Input file is not available.");
        throw new RuntimeException("Input file " + filename + " is not readable.");
      }

      // Import from file
      JAXBContext jaxbContext = JAXBContext.newInstance(EntityModel.class);
      Unmarshaller unJaxbMarshaller = jaxbContext.createUnmarshaller();

      EntityModel entityModel = (EntityModel) unJaxbMarshaller.unmarshal(new FileInputStream(file));

      // convert EntityModel to Entity, EntityAttributeGroups, EntityAttributeValidations
      Entity entity =
          ConvertUtil.mapToEntityModel(entityModel, getEntityAttributeDatatypes(), Entity.class);

      // check existing entity
      List<Entity> entities = entityDefinitionDao.findEntitiesByName(entity.getName());
      for (Entity en : entities) {
        if (en.getVersionId().intValue() == entity.getVersionId().intValue()) {
          log.error("Entity definition already exists: " + entity.getName());
          throw new RuntimeException(
              "Entity definition in the file " + filename + " already exists.");
        }
      }

      // add entity
      Entity newEntity = addEntity(entity);

      // 2. add entity groups with attribute
      List<EntityAttributeGroup> groups =
          ConvertUtil.mapToEntityGroup(
              entityModel, newEntity, org.openhie.openempi.model.EntityAttributeGroup.class);
      for (EntityAttributeGroup group : groups) {
        addEntityAttributeGroup(group);
      }

      // 3. add entity attribute validations
      for (AttributeType attributeType : entityModel.getAttributes().getAttribute()) {
        List<EntityAttributeValidation> validations =
            ConvertUtil.mapToEntityValidations(
                attributeType,
                newEntity,
                org.openhie.openempi.model.EntityAttributeValidation.class);
        for (EntityAttributeValidation validation : validations) {
          addEntityAttributeValidation(validation);
        }
      }
      return;
    } catch (Exception e) {
      log.warn("Unable to Unmarshal xml to a EntityModel: " + e, e);
      throw new RuntimeException(e);
    }
  }