Search in sources :

Example 56 with Field

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);
}
Also used : EntityAudit(org.motechproject.mds.domain.EntityAudit) Field(org.motechproject.mds.domain.Field) Tracking(org.motechproject.mds.domain.Tracking) Lookup(org.motechproject.mds.domain.Lookup) RestOptions(org.motechproject.mds.domain.RestOptions)

Example 57 with Field

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());
}
Also used : Entity(org.motechproject.mds.domain.Entity) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) Matchers.anyLong(org.mockito.Matchers.anyLong) Record(org.motechproject.mds.testutil.records.Record) Matchers.anyString(org.mockito.Matchers.anyString) Record__History(org.motechproject.mds.testutil.records.history.Record__History) Date(java.util.Date) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 58 with Field

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;
}
Also used : Entity(org.motechproject.mds.domain.Entity) Field(org.motechproject.mds.domain.Field) HashMap(java.util.HashMap) Lookup(org.motechproject.mds.domain.Lookup)

Example 59 with Field

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);
    }
}
Also used : Field(org.motechproject.mds.domain.Field)

Example 60 with Field

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);
}
Also used : Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) FieldMetadata(org.motechproject.mds.domain.FieldMetadata) DateTime(org.joda.time.DateTime)

Aggregations

Field (org.motechproject.mds.domain.Field)73 Entity (org.motechproject.mds.domain.Entity)33 Test (org.junit.Test)24 Lookup (org.motechproject.mds.domain.Lookup)16 MdsEntity (org.motechproject.mds.domain.MdsEntity)15 MdsVersionedEntity (org.motechproject.mds.domain.MdsVersionedEntity)15 Type (org.motechproject.mds.domain.Type)14 ArrayList (java.util.ArrayList)13 Transactional (org.springframework.transaction.annotation.Transactional)13 FieldDto (org.motechproject.mds.dto.FieldDto)12 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)12 HashSet (java.util.HashSet)8 Matchers.anyString (org.mockito.Matchers.anyString)6 FieldSetting (org.motechproject.mds.domain.FieldSetting)6 TypeSetting (org.motechproject.mds.domain.TypeSetting)6 UserPreferences (org.motechproject.mds.domain.UserPreferences)5 EntityDto (org.motechproject.mds.dto.EntityDto)5 AllUserPreferences (org.motechproject.mds.repository.internal.AllUserPreferences)5 FieldMetadata (org.motechproject.mds.domain.FieldMetadata)4 LookupDto (org.motechproject.mds.dto.LookupDto)4