use of org.motechproject.mds.domain.ClassData 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();
}
}
use of org.motechproject.mds.domain.ClassData in project motech by motech.
the class JarGeneratorServiceImpl method getImports.
private String getImports() throws IOException {
// first load the standard imports
String stdImports = loadResourceAsString(BUNDLE_IMPORTS).replaceAll("\\r|\\n", "");
// we want to prevent duplicate imports
StringBuilder sb = new StringBuilder(stdImports);
Set<String> alreadyImported = new HashSet<>();
Collections.addAll(alreadyImported, split(stdImports, ","));
// add imports for DDE classes
for (ClassData classData : MotechClassPool.getEnhancedClasses(true)) {
if (classData.isDDE()) {
String pkg = ClassName.getPackage(classData.getClassName());
if (!alreadyImported.contains(pkg)) {
sb.append(',').append(pkg);
alreadyImported.add(pkg);
}
}
}
for (String enumName : MotechClassPool.registeredEnums()) {
String pkg = ClassName.getPackage(enumName);
if (!alreadyImported.contains(pkg)) {
sb.append(',').append(pkg);
alreadyImported.add(pkg);
}
}
return sb.toString();
}
use of org.motechproject.mds.domain.ClassData in project motech by motech.
the class JarGeneratorServiceImpl method getExports.
private String getExports() {
Set<String> exports = new HashSet<>();
exports.add(PackagesGenerated.ENTITY);
exports.add(PackagesGenerated.SERVICE);
for (ClassData enhancedClass : MotechClassPool.getEnhancedClasses(false)) {
if (enhancedClass.isEnumClassData()) {
String pkg = ClassName.getPackage(enhancedClass.getClassName());
exports.add(pkg);
}
if (!MotechClassPool.isServiceInterfaceRegistered(enhancedClass.getClassName()) && enhancedClass.isDDE()) {
String pkg = ClassName.getPackage(enhancedClass.getClassName()).concat(".mdsservice");
exports.add(pkg);
}
}
return createExportPackage(exports);
}
use of org.motechproject.mds.domain.ClassData in project motech by motech.
the class EntityBuilderTest method shouldNotAddVersionFieldToTheHistoryClass.
@Test(expected = NoSuchFieldException.class)
public void shouldNotAddVersionFieldToTheHistoryClass() throws Exception {
FieldDto versionField = fieldDto("version", Long.class);
versionField.addMetadata(new MetadataDto(Constants.MetadataKeys.VERSION_FIELD, "true"));
fields.addAll(asList(fieldDto("id", Long.class), versionField, fieldDto("count", Integer.class), fieldDto("str", String.class)));
ClassData classData = entityBuilder.buildHistory(entity, fields);
assertEquals("xx.yy.history.BuilderTest__History", classData.getClassName());
Class<?> clazz = mdsClassLoader.safeDefineClass(classData.getClassName(), classData.getBytecode());
assertNotNull(clazz);
try {
assertField(clazz, "id", Long.class);
assertField(clazz, "count", Integer.class);
assertField(clazz, "str", String.class);
} catch (NoSuchFieldException e) {
LOGGER.error("Cannot find field in the history class", e);
fail();
}
assertField(clazz, "version", Long.class);
}
use of org.motechproject.mds.domain.ClassData in project motech by motech.
the class EntityBuilderTest method shouldBuildEnhancedDDE.
@Test
public void shouldBuildEnhancedDDE() throws Exception {
FieldDto strField = fieldDto("testStr", String.class);
FieldDto boolField = fieldDto("testBool", Boolean.class);
strField.setReadOnly(true);
boolField.setReadOnly(true);
fields.addAll(asList(strField, boolField, fieldDto("fromUser", DateTime.class)));
when(entity.getClassName()).thenReturn(EntBuilderTestClass.class.getName());
ClassData classData = entityBuilder.buildDDE(entity, fields, bundle);
Class<?> builtClass = MDSClassLoader.getStandaloneInstance().defineClass(classData.getClassName(), classData.getBytecode());
assertField(builtClass, "testStr", String.class, "defValForTestStr");
assertField(builtClass, "testBool", boolean.class, false, "is");
assertField(builtClass, "fromUser", DateTime.class);
// check annotations
assertNotNull(builtClass.getAnnotation(PersistenceCapable.class));
java.lang.reflect.Field field = builtClass.getDeclaredField("testStr");
assertNotNull(field.getAnnotation(Unique.class));
}
Aggregations