use of org.motechproject.mds.domain.Entity in project motech by motech.
the class EntityServiceImpl method getLookups.
private List<LookupDto> getLookups(Long entityId, boolean forDraft) {
Entity entity = (forDraft) ? getEntityDraft(entityId) : allEntities.retrieveById(entityId);
assertEntityExists(entity, entityId);
List<LookupDto> lookupDtos = new ArrayList<>();
for (Lookup lookup : entity.getLookups()) {
lookupDtos.add(lookup.toDto());
}
return lookupDtos;
}
use of org.motechproject.mds.domain.Entity in project motech by motech.
the class EntityServiceImpl method addDisplayedFields.
@Override
@Transactional
public void addDisplayedFields(EntityDto entityDto, Map<String, Long> positions) {
Entity entity = allEntities.retrieveById(entityDto.getId());
assertEntityExists(entity, entityDto.getId());
List<Field> fields = entity.getFields();
if (MapUtils.isEmpty(positions)) {
for (long i = 0; i < fields.size(); ++i) {
Field field = fields.get((int) i);
// user fields and auto generated fields are ignored
if (isFieldFromDde(field)) {
field.setUIDisplayable(true);
field.setUIDisplayPosition(i);
}
}
} else {
for (Field field : fields) {
String fieldName = field.getName();
boolean isUIDisplayable = positions.containsKey(fieldName);
Long uiDisplayPosition = positions.get(fieldName);
field.setUIDisplayable(isUIDisplayable);
field.setUIDisplayPosition(uiDisplayPosition);
}
}
}
use of org.motechproject.mds.domain.Entity in project motech by motech.
the class EntityServiceImpl method addRelatedField.
private void addRelatedField(Field draftField, List<String> modulesToRefresh) {
Entity entity = allEntities.retrieveByClassName(draftField.getMetadataValue(RELATED_CLASS));
String fieldName = draftField.getMetadataValue(RELATED_FIELD);
String collectionType = draftField.getMetadataValue(RELATIONSHIP_COLLECTION_TYPE);
String relatedClass = draftField.getEntity().getClassName();
Set<Lookup> fieldLookups = new HashSet<>();
Field relatedField = new Field(entity, fieldName, fieldName, false, false, false, false, false, null, null, null, fieldLookups);
Type type = allTypes.retrieveByClassName(TypeDto.MANY_TO_MANY_RELATIONSHIP.getTypeClass());
relatedField.setType(type);
if (type.hasSettings()) {
for (TypeSetting setting : type.getSettings()) {
relatedField.addSetting(new FieldSetting(relatedField, setting));
}
}
relatedField.setUIDisplayable(true);
relatedField.setUIDisplayPosition((long) entity.getFields().size());
FieldHelper.createMetadataForManyToManyRelationship(relatedField, relatedClass, collectionType, draftField.getName(), false);
entity.addField(relatedField);
entity.incrementVersion();
addModuleToRefresh(entity, modulesToRefresh);
}
use of org.motechproject.mds.domain.Entity in project motech by motech.
the class EntityServiceImpl method getSchema.
@Override
@Transactional
public SchemaHolder getSchema() {
StopWatch stopWatch = new StopWatch();
SchemaHolder entitiesHolder = new SchemaHolder();
LOGGER.debug("Retrieving entities for processing");
stopWatch.start();
List<Entity> entities = allEntities.getActualEntities();
stopWatch.stop();
LOGGER.debug("{} entities retrieved in {} ms", entities.size(), stopWatch.getTime());
StopWatchHelper.restart(stopWatch);
for (Entity entity : entities) {
LOGGER.debug("Preparing entity: {}", entity.getClassName());
StopWatchHelper.restart(stopWatch);
EntityDto entityDto = entity.toDto();
stopWatch.stop();
LOGGER.debug("Entity dto created in {} ms", stopWatch.getTime());
StopWatchHelper.restart(stopWatch);
AdvancedSettingsDto advSettingsDto = entity.advancedSettingsDto();
stopWatch.stop();
LOGGER.debug("Advanced settings dto created in {} ms", stopWatch.getTime());
StopWatchHelper.restart(stopWatch);
List<FieldDto> fieldDtos = entity.getFieldDtos();
stopWatch.stop();
LOGGER.debug("Field dtos created in {} ms", stopWatch.getTime());
StopWatchHelper.restart(stopWatch);
entitiesHolder.addEntity(entityDto, advSettingsDto, fieldDtos);
stopWatch.stop();
LOGGER.debug("Result stored in {} ms", stopWatch.getTime());
}
LOGGER.debug("Retrieving types for processing");
List<Type> types = allTypes.retrieveAll();
for (Type type : types) {
TypeDto typeDto = type.toDto();
entitiesHolder.addType(typeDto);
entitiesHolder.addTypeValidation(typeDto, type.getTypeValidationDtos());
}
LOGGER.debug("Entities holder ready");
return entitiesHolder;
}
use of org.motechproject.mds.domain.Entity in project motech by motech.
the class EnumBuilderImplTest method testBuild.
@Test
public void testBuild() throws Exception {
String[] expectedValues = { "A", "B", "C", "D", "E" };
TypeSetting typeSetting = new TypeSetting();
typeSetting.setName(Constants.Settings.COMBOBOX_VALUES);
FieldSetting fieldSetting = new FieldSetting(null, typeSetting, StringUtils.join(expectedValues, "\n"));
Type type = new Type("mds.field.combobox", null, null);
Field field = new Field(null, "swag", "swag", false, false, false, false, false, null, null, null, null);
field.setType(type);
field.addSetting(fieldSetting);
Entity entity = new Entity(CLASS_NAME);
entity.addField(field);
ComboboxHolder holder = new ComboboxHolder(entity, field);
ClassData data = new EnumBuilderImpl().build(holder);
MDSClassLoader.getInstance().safeDefineClass(data.getClassName(), data.getBytecode());
Class<?> enumClass = MDSClassLoader.getInstance().loadClass(data.getClassName());
assertTrue("The class definition should be enum", enumClass.isEnum());
Object[] enumConstants = enumClass.getEnumConstants();
String[] actualValues = new String[enumConstants.length];
for (int i = 0; i < enumConstants.length; i++) {
actualValues[i] = enumConstants[i].toString();
}
Arrays.sort(expectedValues, String.CASE_INSENSITIVE_ORDER);
Arrays.sort(actualValues, String.CASE_INSENSITIVE_ORDER);
assertArrayEquals(expectedValues, actualValues);
}
Aggregations