/** Will export the entity passed in as the entity version id into a string. */ public String exportEntity(Integer entityVersionId) throws ApplicationException { if (entityVersionId == null) { return ""; } Entity entity = entityDefinitionDao.loadEntity(entityVersionId); try { // 1. convert Entity to EntityModel EntityModel entityModel = ConvertUtil.mapToEntityModel(entity, EntityModel.class); // 2. set group info to EntityModel List<EntityAttributeGroup> groups = loadEntityAttributeGroups(entity); entityModel = ConvertUtil.setGroupInfoToEntityModel(groups, entityModel); // 3. set validation info to entity attribute for (EntityAttribute attribute : entity.getAttributes()) { List<EntityAttributeValidation> validations = loadEntityAttributeValidations(attribute); ValidationsType validationsType = ConvertUtil.mapToEntityValidations(validations); // set validations info to attributeType AttributesType attributesType = entityModel.getAttributes(); if (attributesType != null) { for (AttributeType attributeType : attributesType.getAttribute()) { String name = attributeType.getName(); if (attribute.getName().equals(name)) { attributeType.setValidations(validationsType); } } } } // export to xml format JAXBContext jaxbContext = JAXBContext.newInstance(EntityModel.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); OutputStream outputStream = new ByteArrayOutputStream(); jaxbMarshaller.marshal(entityModel, outputStream); return outputStream.toString(); } catch (JAXBException e) { log.warn("Unable to serialize a Entity into a string: " + e, e); throw new RuntimeException(e); } }
/** 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); } }