Search in sources :

Example 21 with Lookup

use of org.motechproject.mds.domain.Lookup in project motech by motech.

the class LookupReader method readLookup.

public void readLookup() throws IOException {
    Lookup lookup = new Lookup();
    lookup.setEntity(entity);
    jsonReader.beginObject();
    lookup.setLookupName(objectReader.readString("name"));
    lookup.setSingleObjectReturn(objectReader.readBoolean("singleObjectReturn"));
    lookup.setMethodName(objectReader.readString("methodName"));
    lookup.setFields(readFields());
    lookup.setRangeLookupFields(objectReader.readStringArray("rangeFields"));
    lookup.setCustomOperators(objectReader.readStringMap("customOperators"));
    lookup.setUseGenericParams(objectReader.readBooleanMap("useGenericParams"));
    lookup.setFieldsOrder(objectReader.readStringArray("fieldsOrder"));
    jsonReader.endObject();
    Lookup existingLookup = entity.getLookupByName(lookup.getLookupName());
    if (null == existingLookup) {
        entity.addLookup(lookup);
    } else if (!existingLookup.isReadOnly()) {
        entity.removeLookup(existingLookup.getId());
        entity.addLookup(lookup);
    } else {
        throw new LookupReadOnlyException("Cannot import lookup " + lookup.getLookupName());
    }
}
Also used : LookupReadOnlyException(org.motechproject.mds.exception.lookup.LookupReadOnlyException) Lookup(org.motechproject.mds.domain.Lookup)

Example 22 with Lookup

use of org.motechproject.mds.domain.Lookup 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 23 with Lookup

use of org.motechproject.mds.domain.Lookup 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 24 with Lookup

use of org.motechproject.mds.domain.Lookup in project motech by motech.

the class EntityHelper method removeAdditionalFieldsAndLookups.

/**
 * In case of DDE, removes all fields and lookups that are not part of the original schema (was added later).
 * In case of EUDE, removes all fields and lookups.
 * @param entity entity to remove fields and lookups from
 */
public static void removeAdditionalFieldsAndLookups(Entity entity) {
    Iterator<Field> fieldIterator = entity.getFields().iterator();
    while (fieldIterator.hasNext()) {
        Field field = fieldIterator.next();
        if (!field.isAutoGenerated() && !field.isReadOnly()) {
            fieldIterator.remove();
        }
    }
    Iterator<Lookup> lookupIterator = entity.getLookups().iterator();
    while (lookupIterator.hasNext()) {
        Lookup lookup = lookupIterator.next();
        if (!lookup.isReadOnly()) {
            lookupIterator.remove();
        }
    }
}
Also used : Field(org.motechproject.mds.domain.Field) Lookup(org.motechproject.mds.domain.Lookup)

Example 25 with Lookup

use of org.motechproject.mds.domain.Lookup in project motech by motech.

the class EntityServiceImpl method removeLookup.

private void removeLookup(Entity entity, Collection<LookupDto> lookups) {
    Iterator<Lookup> iterator = entity.getLookups().iterator();
    while (iterator.hasNext()) {
        Lookup lookup = iterator.next();
        // don't remove user defined lookups
        if (!lookup.isReadOnly()) {
            continue;
        }
        boolean found = false;
        for (LookupDto lookupDto : lookups) {
            if (lookup.getLookupName().equalsIgnoreCase(lookupDto.getLookupName())) {
                found = true;
                break;
            }
        }
        if (!found) {
            iterator.remove();
        }
    }
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) Lookup(org.motechproject.mds.domain.Lookup)

Aggregations

Lookup (org.motechproject.mds.domain.Lookup)30 Field (org.motechproject.mds.domain.Field)16 Entity (org.motechproject.mds.domain.Entity)12 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 LookupDto (org.motechproject.mds.dto.LookupDto)5 HashSet (java.util.HashSet)4 MdsEntity (org.motechproject.mds.domain.MdsEntity)4 MdsVersionedEntity (org.motechproject.mds.domain.MdsVersionedEntity)4 HashMap (java.util.HashMap)3 FieldSetting (org.motechproject.mds.domain.FieldSetting)3 RestOptions (org.motechproject.mds.domain.RestOptions)3 Type (org.motechproject.mds.domain.Type)3 TypeSetting (org.motechproject.mds.domain.TypeSetting)3 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)3 LinkedList (java.util.LinkedList)2 Query (javax.jdo.Query)2 Matchers.anyString (org.mockito.Matchers.anyString)2 Tracking (org.motechproject.mds.domain.Tracking)2 FieldDto (org.motechproject.mds.dto.FieldDto)2