Search in sources :

Example 11 with Lookup

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

the class AllEntitiesContextIT method shouldCascadeDeleteLookups.

@Test
public void shouldCascadeDeleteLookups() {
    Query query = getPersistenceManager().newQuery(Entity.class);
    query.setFilter("className == name");
    query.declareParameters("java.lang.String name");
    query.setUnique(true);
    Entity found = (Entity) query.execute(EXAMPLE_CLASS_WITH_LOOKUPS);
    List<Lookup> lookups = new ArrayList<>(found.getLookups());
    allEntities.delete(found.getId());
    assertFalse("Lookup was not deleted", getLookups().contains(lookups.get(0)));
    assertFalse("Lookup was not deleted", getLookups().contains(lookups.get(1)));
}
Also used : Entity(org.motechproject.mds.domain.Entity) Query(javax.jdo.Query) ArrayList(java.util.ArrayList) Lookup(org.motechproject.mds.domain.Lookup) Test(org.junit.Test)

Example 12 with Lookup

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

the class AllEntitiesContextIT method shouldCascadeSaveLookup.

@Test
public void shouldCascadeSaveLookup() throws Exception {
    Lookup lookup = new Lookup(SAMPLE_LOOKUP, true, false, null);
    Entity entity = getEntities().get(0);
    List<Lookup> lookupSet = new LinkedList<>();
    lookupSet.add(lookup);
    entity.setLookups(lookupSet);
    int indexOfLookup = getLookups().indexOf(lookup);
    assertTrue(String.format("'%s' not found in database", SAMPLE_LOOKUP), indexOfLookup >= 0);
    assertEquals("Lookup was not associated with an entity", entity, getLookups().get(indexOfLookup).getEntity());
}
Also used : Entity(org.motechproject.mds.domain.Entity) Lookup(org.motechproject.mds.domain.Lookup) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 13 with Lookup

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

the class AllEntitiesContextIT method shouldUpdateLookup.

@Test
public void shouldUpdateLookup() throws Exception {
    Query query = getPersistenceManager().newQuery(Entity.class);
    query.setFilter("className == name");
    query.declareParameters("java.lang.String name");
    query.setUnique(true);
    Entity entity = (Entity) query.execute(EXAMPLE_CLASS_WITH_LOOKUPS);
    for (Lookup lookup : entity.getLookups()) {
        if (EXAMPLE_LOOKUP_1.equals(lookup.getLookupName())) {
            lookup.setSingleObjectReturn(false);
        } else if (EXAMPLE_LOOKUP_2.equals(lookup.getLookupName())) {
            lookup.setExposedViaRest(true);
        }
    }
    for (Lookup lookup : getLookups()) {
        if (EXAMPLE_LOOKUP_1.equals(lookup.getLookupName())) {
            assertEquals("Lookup was not updated properly", false, lookup.isSingleObjectReturn());
            assertEquals("Lookup was not updated properly", false, lookup.isExposedViaRest());
        } else if (EXAMPLE_LOOKUP_2.equals(lookup.getLookupName())) {
            assertEquals("Lookup was not updated properly", true, lookup.isSingleObjectReturn());
            assertEquals("Lookup was not updated properly", true, lookup.isExposedViaRest());
        }
    }
}
Also used : Entity(org.motechproject.mds.domain.Entity) Query(javax.jdo.Query) Lookup(org.motechproject.mds.domain.Lookup) Test(org.junit.Test)

Example 14 with Lookup

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

the class EntityValidator method validateEntityLookupsFieldsReferences.

private void validateEntityLookupsFieldsReferences(EntityDraft draft) {
    Map<String, Lookup> draftLookups = new HashMap<>();
    List<String> validateLookups = new ArrayList<>();
    for (Lookup lookup : draft.getLookups()) {
        draftLookups.put(lookup.getLookupName(), lookup);
    }
    for (Lookup lookup : draft.getParentEntity().getLookups()) {
        Lookup draftLookup = draftLookups.get(lookup.getLookupName());
        if (draftLookup == null || lookupFieldsChanged(lookup, draftLookup)) {
            validateLookups.add(lookup.getLookupName());
        }
    }
    validateLookupsReferences(validateLookups, draft.getClassName());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Lookup(org.motechproject.mds.domain.Lookup)

Example 15 with Lookup

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

the class AllEntityDrafts method setProperties.

public void setProperties(EntityDraft draft, Entity entity, String username) {
    draft.setParentEntity(entity);
    draft.setParentVersion(entity.getEntityVersion());
    draft.setDraftOwnerUsername(username);
    draft.setLastModificationDate(DateUtil.nowUTC());
    draft.setName(entity.getName());
    draft.setClassName(entity.getClassName());
    draft.setNamespace(entity.getNamespace());
    draft.setModule(entity.getModule());
    draft.setSecurityMode(entity.getSecurityMode());
    draft.setSecurityMembers(new HashSet<>(entity.getSecurityMembers()));
    draft.setReadOnlySecurityMode(entity.getReadOnlySecurityMode());
    draft.setReadOnlySecurityMembers(new HashSet<>(entity.getReadOnlySecurityMembers()));
    draft.getFields().clear();
    for (Field field : entity.getFields()) {
        draft.addField(field.copy());
    }
    draft.getLookups().clear();
    for (Lookup lookup : entity.getLookups()) {
        Lookup newLookup = lookup.copy(draft.getFields());
        draft.addLookup(newLookup);
    }
    if (entity.getRestOptions() != null) {
        RestOptions restOptions = entity.getRestOptions().copy();
        restOptions.setEntity(draft);
        draft.setRestOptions(restOptions);
    }
    if (entity.getTracking() != null) {
        Tracking tracking = entity.getTracking().copy();
        tracking.setEntity(draft);
        draft.setTracking(tracking);
    }
}
Also used : Field(org.motechproject.mds.domain.Field) Tracking(org.motechproject.mds.domain.Tracking) Lookup(org.motechproject.mds.domain.Lookup) RestOptions(org.motechproject.mds.domain.RestOptions)

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