use of org.motechproject.mds.domain.Field in project motech by motech.
the class AllEntityAudits method createAudit.
public EntityAudit createAudit(Entity entity, String username) {
EntityAudit audit = new EntityAudit();
audit.setOwnerUsername(username);
audit.setModificationDate(DateUtil.nowUTC());
audit.setVersion(entity.getEntityVersion());
audit.setName(entity.getName());
audit.setClassName(entity.getClassName());
audit.setNamespace(entity.getNamespace());
audit.setTableName(entity.getTableName());
audit.setModule(entity.getModule());
for (Field field : entity.getFields()) {
Field tmp = field.copy();
tmp.setId(null);
audit.addField(tmp);
}
for (Lookup lookup : entity.getLookups()) {
audit.addLookup(lookup.copy(entity.getFields()));
}
if (entity.getRestOptions() != null) {
RestOptions restOptions = entity.getRestOptions().copy();
restOptions.setEntity(audit);
audit.setRestOptions(restOptions);
}
if (entity.getTracking() != null) {
Tracking tracking = entity.getTracking().copy();
tracking.setEntity(audit);
audit.setTracking(tracking);
}
return create(audit);
}
use of org.motechproject.mds.domain.Field in project motech by motech.
the class HistoryServiceTest method shouldCreateNewRecord.
@Test
public void shouldCreateNewRecord() throws Exception {
Entity entity = mock(Entity.class);
Field idField = mock(Field.class);
doReturn("id").when(idField).getName();
Type idType = mock(Type.class);
doReturn(idType).when(idField).getType();
doReturn(Long.class.getName()).when(idType).getTypeClassName();
doReturn(Long.class).when(idType).getTypeClass();
Field valueField = mock(Field.class);
doReturn("value").when(valueField).getName();
Type valueType = mock(Type.class);
doReturn(valueType).when(valueField).getType();
doReturn(String.class.getName()).when(valueType).getTypeClassName();
doReturn(String.class).when(valueType).getTypeClass();
Field dateField = mock(Field.class);
doReturn("date").when(dateField).getName();
Type dateType = mock(Type.class);
doReturn(dateType).when(dateField).getType();
doReturn(Date.class.getName()).when(dateType).getTypeClassName();
doReturn(Date.class).when(dateType).getTypeClass();
doReturn(17L).when(entity).getEntityVersion();
doReturn(true).when(entity).isRecordHistory();
doReturn(Arrays.asList(idField, valueField, dateField)).when(entity).getFields();
doReturn(null).when(query).execute(anyLong());
final String serviceName = ClassName.getServiceName(Record.class.getName());
doReturn(true).when(applicationContext).containsBean(serviceName);
doReturn(dataService).when(applicationContext).getBean(serviceName);
doReturn(4L).when(dataService).getSchemaVersion();
Record instance = new Record();
historyService.record(instance);
verify(manager).makePersistent(recordHistoryCaptor.capture());
Record__History history = recordHistoryCaptor.getValue();
assertEquals(instance.getId(), history.getRecord__HistoryCurrentVersion());
assertEquals(Long.valueOf(4), history.getRecord__HistorySchemaVersion());
assertEquals(instance.getValue(), history.getValue());
assertEquals(instance.getDate(), history.getDate());
}
use of org.motechproject.mds.domain.Field in project motech by motech.
the class EntitySchemaBuilder method build.
public Entity build() {
Entity entity = new Entity(clazz, module, namespace, null);
entity.setRestOptions(rest.build());
entity.getRestOptions().setEntity(entity);
entity.setTracking(auditing.build());
entity.getTracking().setEntity(entity);
Map<String, Field> fieldsMap = new HashMap<>();
for (FieldSchemaBuilder fieldBuilder : fields) {
Field field = fieldBuilder.build();
field.setEntity(entity);
field.setExposedViaRest(rest.fields.contains(field.getName()));
field.setUIDisplayable(browsing.fields.contains(field.getName()));
field.setUIDisplayPosition(Integer.valueOf(browsing.fields.indexOf(field.getName())).longValue());
field.setUIFilterable(browsing.filters.contains(field.getName()));
fieldsMap.put(field.getName(), field);
entity.addField(field);
}
for (LookupSchemaBuilder lookupBuilder : lookups) {
Lookup lookup = lookupBuilder.build();
lookup.setEntity(entity);
lookup.setExposedViaRest(rest.lookups.contains(lookup.getLookupName()));
setLookupFields(lookup, lookupBuilder, fieldsMap);
entity.addLookup(lookup);
}
return entity;
}
use of org.motechproject.mds.domain.Field in project motech by motech.
the class EntitySchemaBuilder method setLookupFields.
private void setLookupFields(Lookup lookup, LookupSchemaBuilder lookupBuilder, Map<String, Field> fieldsMap) {
List<Field> lookupFields = getLookupFields(lookupBuilder.fields, fieldsMap);
lookup.setFields(lookupFields);
for (Field lookupField : lookupFields) {
lookupField.getLookups().add(lookup);
}
}
use of org.motechproject.mds.domain.Field in project motech by motech.
the class EntityHelper method addDefaultFields.
/**
* Adds default fields to entity
* @param entity entity to add fields to
* @param allTypes types repository
*/
public static void addDefaultFields(Entity entity, AllTypes allTypes) {
Type longType = allTypes.retrieveByClassName(Long.class.getName());
Type stringType = allTypes.retrieveByClassName(String.class.getName());
Type dateTimeType = allTypes.retrieveByClassName(DateTime.class.getName());
Field idField = new Field(entity, Constants.Util.ID_FIELD_NAME, Constants.Util.ID_DISPLAY_FIELD_NAME, longType, true, true, true);
idField.addMetadata(new FieldMetadata(idField, AUTO_GENERATED, TRUE));
Field creatorField = new Field(entity, Constants.Util.CREATOR_FIELD_NAME, Constants.Util.CREATOR_DISPLAY_FIELD_NAME, stringType, true, false, true);
creatorField.addMetadata(new FieldMetadata(creatorField, AUTO_GENERATED, TRUE));
Field ownerField = new Field(entity, Constants.Util.OWNER_FIELD_NAME, Constants.Util.OWNER_DISPLAY_FIELD_NAME, stringType, false, false, true);
ownerField.addMetadata(new FieldMetadata(ownerField, AUTO_GENERATED_EDITABLE, TRUE));
Field modifiedByField = new Field(entity, Constants.Util.MODIFIED_BY_FIELD_NAME, Constants.Util.MODIFIED_BY_DISPLAY_FIELD_NAME, stringType, true, false, true);
modifiedByField.addMetadata(new FieldMetadata(modifiedByField, AUTO_GENERATED, TRUE));
Field modificationDateField = new Field(entity, Constants.Util.MODIFICATION_DATE_FIELD_NAME, Constants.Util.MODIFICATION_DATE_DISPLAY_FIELD_NAME, dateTimeType, true, false, true);
modificationDateField.addMetadata(new FieldMetadata(modificationDateField, AUTO_GENERATED, TRUE));
Field creationDateField = new Field(entity, Constants.Util.CREATION_DATE_FIELD_NAME, Constants.Util.CREATION_DATE_DISPLAY_FIELD_NAME, dateTimeType, true, false, true);
creationDateField.addMetadata(new FieldMetadata(creationDateField, AUTO_GENERATED, TRUE));
entity.addField(idField);
entity.addField(creatorField);
entity.addField(ownerField);
entity.addField(modifiedByField);
entity.addField(creationDateField);
entity.addField(modificationDateField);
}
Aggregations