Search in sources :

Example 6 with EntityInfo

use of org.motechproject.mds.entityinfo.EntityInfo in project motech by motech.

the class JarGeneratorServiceImpl method generate.

@Override
public File generate(SchemaHolder schemaHolder) throws IOException {
    Path tempDir = Files.createTempDirectory("mds");
    Path tempFile = Files.createTempFile(tempDir, "mds-entities", ".jar");
    java.util.jar.Manifest manifest = createManifest();
    StringBuilder entityNamesSb = new StringBuilder();
    StringBuilder historyEntitySb = new StringBuilder();
    try (FileOutputStream fileOutput = new FileOutputStream(tempFile.toFile());
        JarOutputStream output = new JarOutputStream(fileOutput, manifest)) {
        List<EntityInfo> information = new ArrayList<>();
        for (ClassData classData : MotechClassPool.getEnhancedClasses(false)) {
            String className = classData.getClassName();
            // insert entity class, only for EUDE, note that this can also be a generated enum class
            if (!classData.isDDE()) {
                addEntry(output, JavassistUtil.toClassPath(className), classData.getBytecode());
            }
            // insert history and trash classes, these classes will not be present for enums
            ClassData historyClassData = MotechClassPool.getHistoryClassData(className);
            if (historyClassData != null) {
                addEntry(output, JavassistUtil.toClassPath(historyClassData.getClassName()), historyClassData.getBytecode());
                historyEntitySb.append(className).append('\n');
            }
            ClassData trashClassData = MotechClassPool.getTrashClassData(className);
            if (trashClassData != null) {
                addEntry(output, JavassistUtil.toClassPath(trashClassData.getClassName()), trashClassData.getBytecode());
            }
            if (!classData.isEnumClassData()) {
                EntityDto entity = schemaHolder.getEntityByClassName(classData.getClassName());
                List<FieldDto> fields = schemaHolder.getFields(entity);
                AdvancedSettingsDto advancedSettings = schemaHolder.getAdvancedSettings(entity);
                EntityInfo info = buildEntityInfo(entity, fields, advancedSettings);
                // we keep the name to construct a file containing all entity names
                // the file is required for schema generation
                entityNamesSb.append(className).append('\n');
                // insert repository
                String repositoryName = MotechClassPool.getRepositoryName(className);
                if (addClass(output, repositoryName)) {
                    info.setRepository(repositoryName);
                }
                // insert service implementation
                String serviceClass = MotechClassPool.getServiceImplName(className);
                if (addClass(output, serviceClass)) {
                    info.setServiceClass(serviceClass);
                    info.setServiceName(ClassName.getServiceName(className));
                }
                // insert the interface
                String interfaceName = MotechClassPool.getInterfaceName(className);
                if (MotechClassPool.isServiceInterfaceRegistered(className)) {
                    // we import the service interface
                    info.setInterfaceName(interfaceName);
                } else {
                    // we generated the service interface from scratch and include it in the bundle
                    if (addClass(output, interfaceName)) {
                        info.setInterfaceName(interfaceName);
                    }
                }
                information.add(info);
            }
        }
        String blueprint = mergeTemplate(information, BLUEPRINT_TEMPLATE);
        String context = mergeTemplate(information, MDS_ENTITIES_CONTEXT_TEMPLATE);
        String channel = mergeTemplate(information, MDS_CHANNEL_TEMPLATE);
        jdoListenerRegistryService.updateEntityNames();
        jdoListenerRegistryService.removeInactiveListeners(entityNamesSb.toString());
        String entityWithListenersNames = jdoListenerRegistryService.getEntitiesListenerStr();
        addEntries(output, blueprint, context, channel, entityNamesSb.toString(), historyEntitySb.toString(), entityWithListenersNames);
        addEntityInfoFiles(output, information);
        return tempFile.toFile();
    }
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) JarOutputStream(java.util.jar.JarOutputStream) EntityDto(org.motechproject.mds.dto.EntityDto) ClassData(org.motechproject.mds.domain.ClassData) EntityInfo(org.motechproject.mds.entityinfo.EntityInfo) FileOutputStream(java.io.FileOutputStream) AdvancedSettingsDto(org.motechproject.mds.dto.AdvancedSettingsDto) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 7 with EntityInfo

use of org.motechproject.mds.entityinfo.EntityInfo in project motech by motech.

the class JarGeneratorServiceImpl method buildEntityInfo.

private EntityInfo buildEntityInfo(EntityDto entity, List<FieldDto> fields, AdvancedSettingsDto advancedSettings) {
    EntityInfo info = new EntityInfo();
    info.setEntity(entity);
    info.setAdvancedSettings(advancedSettings);
    info.setFieldsInfo(getFieldsInfo(entity, fields, advancedSettings));
    return info;
}
Also used : EntityInfo(org.motechproject.mds.entityinfo.EntityInfo)

Example 8 with EntityInfo

use of org.motechproject.mds.entityinfo.EntityInfo in project motech by motech.

the class JarGeneratorServiceImpl method addEntityInfoFiles.

private void addEntityInfoFiles(JarOutputStream output, List<EntityInfo> entityInfos) throws IOException {
    final ObjectMapper objectMapper = new ObjectMapper();
    for (EntityInfo entityInfo : entityInfos) {
        String asJson = objectMapper.writeValueAsString(entityInfo);
        addEntry(output, ENTITY_INFO_DIR + entityInfo.getClassName() + ".json", asJson.getBytes(Charset.forName("UTF-8")));
    }
}
Also used : EntityInfo(org.motechproject.mds.entityinfo.EntityInfo) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 9 with EntityInfo

use of org.motechproject.mds.entityinfo.EntityInfo in project motech by motech.

the class MdsRestFacadeImpl method init.

@PostConstruct
public void init() {
    entityClass = dataService.getClassType();
    EntityInfo entity = entityInfoReader.getEntityInfo(entityClass.getName());
    moduleName = entity.getModule();
    entityName = entity.getName();
    namespace = entity.getNamespace();
    readRestOptions(entity);
    Map<String, FieldDto> fieldMap = DtoHelper.asFieldMapByName(entity.getFieldDtos());
    readLookups(entity);
    readFieldsExposedByRest(fieldMap);
    readBlobFieldsExposedByRest(fieldMap);
}
Also used : EntityInfo(org.motechproject.mds.entityinfo.EntityInfo) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) PostConstruct(javax.annotation.PostConstruct)

Aggregations

EntityInfo (org.motechproject.mds.entityinfo.EntityInfo)9 FieldDto (org.motechproject.mds.dto.FieldDto)5 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 PostConstruct (javax.annotation.PostConstruct)2 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)2 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)2 FieldInfo (org.motechproject.mds.entityinfo.FieldInfo)2 FileOutputStream (java.io.FileOutputStream)1 Path (java.nio.file.Path)1 JarOutputStream (java.util.jar.JarOutputStream)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 ClassData (org.motechproject.mds.domain.ClassData)1 RelationshipHolder (org.motechproject.mds.domain.RelationshipHolder)1 EntityDto (org.motechproject.mds.dto.EntityDto)1 EntityInfoReader (org.motechproject.mds.entityinfo.EntityInfoReader)1 EntityNotFoundException (org.motechproject.mds.exception.entity.EntityNotFoundException)1 FieldNotFoundException (org.motechproject.mds.exception.field.FieldNotFoundException)1 DefaultMotechDataService (org.motechproject.mds.service.DefaultMotechDataService)1 MotechDataService (org.motechproject.mds.service.MotechDataService)1