use of org.motechproject.mds.dto.FieldDto in project motech by motech.
the class EntityMetadataBuilderTest method shouldAddOneToManyRelationshipMetadata.
@Test
public void shouldAddOneToManyRelationshipMetadata() {
FieldDto oneToManyField = fieldDto("oneToManyName", OneToManyRelationship.class);
oneToManyField.addMetadata(new MetadataDto(RELATED_CLASS, "org.motechproject.test.MyClass"));
FieldMetadata fmd = mock(FieldMetadata.class);
CollectionMetadata collMd = mock(CollectionMetadata.class);
when(entity.getName()).thenReturn(ENTITY_NAME);
when(entity.getId()).thenReturn(2L);
when(schemaHolder.getFields(entity)).thenReturn(singletonList(oneToManyField));
when(entity.getTableName()).thenReturn(TABLE_NAME);
when(jdoMetadata.newPackageMetadata(PACKAGE)).thenReturn(packageMetadata);
when(packageMetadata.newClassMetadata(ENTITY_NAME)).thenReturn(classMetadata);
when(classMetadata.newFieldMetadata("oneToManyName")).thenReturn(fmd);
when(fmd.getCollectionMetadata()).thenReturn(collMd);
when(fmd.getName()).thenReturn("oneToManyName");
entityMetadataBuilder.addEntityMetadata(jdoMetadata, entity, Sample.class, schemaHolder);
verifyCommonClassMetadata();
verify(fmd).setDefaultFetchGroup(true);
verify(collMd).setEmbeddedElement(false);
verify(collMd).setSerializedElement(false);
verify(collMd).setElementType("org.motechproject.test.MyClass");
}
use of org.motechproject.mds.dto.FieldDto in project motech by motech.
the class EntityMetadataBuilderTest method shouldSetIndexOnMetadataLookupField.
@Test
public void shouldSetIndexOnMetadataLookupField() throws Exception {
FieldDto lookupField = fieldDto("lookupField", String.class);
LookupDto lookup = new LookupDto();
lookup.setLookupName("A lookup");
lookup.setLookupFields(singletonList(new LookupFieldDto("lookupField", LookupFieldType.VALUE)));
lookup.setIndexRequired(true);
lookupField.setLookups(singletonList(lookup));
FieldMetadata fmd = mock(FieldMetadata.class);
when(entity.getName()).thenReturn(ENTITY_NAME);
when(entity.getId()).thenReturn(14L);
when(schemaHolder.getFields(entity)).thenReturn(singletonList(lookupField));
when(entity.getTableName()).thenReturn(TABLE_NAME);
when(jdoMetadata.newPackageMetadata(PACKAGE)).thenReturn(packageMetadata);
when(packageMetadata.newClassMetadata(ENTITY_NAME)).thenReturn(classMetadata);
when(classMetadata.newFieldMetadata("lookupField")).thenReturn(fmd);
when(fmd.newIndexMetadata()).thenReturn(indexMetadata);
PowerMockito.mockStatic(FieldUtils.class);
when(FieldUtils.getDeclaredField(eq(Sample.class), anyString(), eq(true))).thenReturn(Sample.class.getDeclaredField("notInDefFg"));
entityMetadataBuilder.addEntityMetadata(jdoMetadata, entity, Sample.class, schemaHolder);
verifyCommonClassMetadata();
verify(fmd).newIndexMetadata();
verify(indexMetadata).setName("lkp_idx_" + ENTITY_NAME + "_lookupField_14");
}
use of org.motechproject.mds.dto.FieldDto in project motech by motech.
the class EntityMetadataBuilderTest method shouldAddObjectValueGeneratorToAppropriateFields.
@Test
public void shouldAddObjectValueGeneratorToAppropriateFields() throws Exception {
when(entity.getName()).thenReturn(ENTITY_NAME);
when(entity.getTableName()).thenReturn(TABLE_NAME);
when(jdoMetadata.newPackageMetadata(anyString())).thenReturn(packageMetadata);
when(packageMetadata.newClassMetadata(anyString())).thenReturn(classMetadata);
List<FieldDto> fields = new ArrayList<>();
// for these fields the appropriate generator should be added
fields.add(fieldDto(1L, CREATOR_FIELD_NAME, String.class.getName(), CREATOR_DISPLAY_FIELD_NAME, null));
fields.add(fieldDto(2L, OWNER_FIELD_NAME, String.class.getName(), OWNER_DISPLAY_FIELD_NAME, null));
fields.add(fieldDto(3L, CREATION_DATE_FIELD_NAME, DateTime.class.getName(), CREATION_DATE_DISPLAY_FIELD_NAME, null));
fields.add(fieldDto(4L, MODIFIED_BY_FIELD_NAME, String.class.getName(), MODIFIED_BY_DISPLAY_FIELD_NAME, null));
fields.add(fieldDto(5L, MODIFICATION_DATE_FIELD_NAME, DateTime.class.getName(), MODIFICATION_DATE_DISPLAY_FIELD_NAME, null));
doReturn(fields).when(schemaHolder).getFields(CLASS_NAME);
final List<FieldMetadata> list = new ArrayList<>();
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
// we create a mock ...
FieldMetadata metadata = mock(FieldMetadata.class);
// ... and it should return correct name
doReturn(invocation.getArguments()[0]).when(metadata).getName();
// Because we want to check that appropriate methods was executed
// we added metadata to list and later we will verify conditions
list.add(metadata);
// in the end we have to return the mock
return metadata;
}
}).when(classMetadata).newFieldMetadata(anyString());
PowerMockito.mockStatic(FieldUtils.class);
when(FieldUtils.getDeclaredField(eq(Sample.class), anyString(), eq(true))).thenReturn(Sample.class.getDeclaredField("notInDefFg"));
entityMetadataBuilder.addEntityMetadata(jdoMetadata, entity, Sample.class, schemaHolder);
for (FieldMetadata metadata : list) {
String name = metadata.getName();
// the id field should not have set object value generator metadata
int invocations = "id".equalsIgnoreCase(name) ? 0 : 1;
verify(classMetadata).newFieldMetadata(name);
verify(metadata, times(invocations)).setPersistenceModifier(PersistenceModifier.PERSISTENT);
verify(metadata, times(invocations)).setDefaultFetchGroup(true);
verify(metadata, times(invocations)).newExtensionMetadata(DATANUCLEUS, VALUE_GENERATOR, "ovg." + name);
}
}
use of org.motechproject.mds.dto.FieldDto in project motech by motech.
the class MdsRestFacadeImpl method getLookupFieldsMapping.
private Map<String, FieldDto> getLookupFieldsMapping(EntityInfo entity, LookupDto lookup) {
Map<String, FieldDto> fieldMap = new HashMap<>();
for (LookupFieldDto lookupField : lookup.getLookupFields()) {
FieldDto field;
if (StringUtils.isNotBlank(lookupField.getRelatedName())) {
RelationshipHolder relHolder = new RelationshipHolder(entity.getField(lookupField.getName()).getField());
EntityInfo relatedEntity = entityInfoReader.getEntityInfo(relHolder.getRelatedClass());
field = relatedEntity.getField(lookupField.getRelatedName()).getField();
} else {
field = entity.getField(lookupField.getName()).getField();
}
fieldMap.put(lookupField.getLookupFieldName(), field);
}
return fieldMap;
}
use of org.motechproject.mds.dto.FieldDto in project motech by motech.
the class InstanceServiceImpl method convertToHistoryRecord.
private HistoryRecord convertToHistoryRecord(Object object, EntityDto entity, Long instanceId, MotechDataService service) {
Long entityId = entity.getId();
List<FieldDto> fields = getEntityFields(entityId);
Map<String, List<FieldDto>> relatedEntitiesFields = getRelatedEntitiesFields(fields);
EntityRecord entityRecord = instanceToRecord(object, entity, entityService.getEntityFieldsForUI(entityId), service, EntityType.HISTORY, relatedEntitiesFields);
Long historyInstanceSchemaVersion = (Long) PropertyUtil.safeGetProperty(object, HistoryTrashClassHelper.historySchemaVersion(object.getClass()));
Long currentSchemaVersion = entityService.getCurrentSchemaVersion(entity.getClassName());
return new HistoryRecord(entityRecord.getId(), instanceId, historyInstanceSchemaVersion.equals(currentSchemaVersion), entityRecord.getFields());
}
Aggregations