Search in sources :

Example 1 with EntityDraft

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

the class EntityServiceImpl method getEntityDraft.

@Override
@Transactional
public EntityDraft getEntityDraft(Long entityId, String username) {
    Entity entity = allEntities.retrieveById(entityId);
    assertEntityExists(entity, entityId);
    if (entity instanceof EntityDraft) {
        return (EntityDraft) entity;
    }
    if (username == null) {
        throw new AccessDeniedException("Cannot save draft - no user");
    }
    // get the draft
    EntityDraft draft = allEntityDrafts.retrieve(entity, username);
    if (draft == null) {
        draft = allEntityDrafts.create(entity, username);
    }
    return draft;
}
Also used : MdsEntity(org.motechproject.mds.domain.MdsEntity) Entity(org.motechproject.mds.domain.Entity) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity) AccessDeniedException(org.springframework.security.access.AccessDeniedException) EntityDraft(org.motechproject.mds.domain.EntityDraft) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with EntityDraft

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

the class EntityServiceImpl method listWorkInProgress.

@Override
@Transactional
public List<EntityDto> listWorkInProgress() {
    String username = getUsername();
    List<EntityDraft> drafts = allEntityDrafts.retrieveAll(username);
    List<EntityDto> entityDtoList = new ArrayList<>();
    for (EntityDraft draft : drafts) {
        if (draft.isChangesMade()) {
            entityDtoList.add(draft.toDto());
        }
    }
    return entityDtoList;
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) ArrayList(java.util.ArrayList) EntityDraft(org.motechproject.mds.domain.EntityDraft) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with EntityDraft

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

the class EntityServiceContextIT method testRelatedFieldGenerationForManyToManyRelationship.

@Test
public void testRelatedFieldGenerationForManyToManyRelationship() {
    EntityDto entityDto1 = new EntityDto();
    entityDto1.setName("RelationTestEnt1");
    entityDto1 = entityService.createEntity(entityDto1);
    EntityDto entityDto2 = new EntityDto();
    entityDto2.setName("RelationTestEnt2");
    entityDto2 = entityService.createEntity(entityDto2);
    EntityDto entityDto3 = new EntityDto();
    entityDto3.setName("RelationTestEnt3");
    entityDto3 = entityService.createEntity(entityDto3);
    EntityDraft entityDraft1 = entityService.getEntityDraft(entityDto1.getId());
    Set<Lookup> fieldLookups = new HashSet<>();
    Field field = new Field(entityDraft1, "newField", "Display Name", fieldLookups);
    Type type = allTypes.retrieveByClassName(TypeDto.MANY_TO_MANY_RELATIONSHIP.getTypeClass());
    field.setType(type);
    if (type.hasSettings()) {
        for (TypeSetting setting : type.getSettings()) {
            field.addSetting(new FieldSetting(field, setting));
        }
    }
    FieldMetadata metadata = new FieldMetadata(field, RELATED_CLASS);
    metadata.setValue(entityDto2.getClassName());
    field.addMetadata(metadata);
    metadata = new FieldMetadata(field, RELATIONSHIP_COLLECTION_TYPE);
    metadata.setValue("java.util.Set");
    field.addMetadata(metadata);
    metadata = new FieldMetadata(field, RELATED_FIELD);
    metadata.setValue("relatedField");
    field.addMetadata(metadata);
    metadata = new FieldMetadata(field, OWNING_SIDE);
    metadata.setValue("true");
    entityDraft1.addField(field);
    allEntityDrafts.update(entityDraft1);
    entityService.commitChanges(entityDto1.getId());
    FieldDto relatedField = getField(entityDto2.getId(), "relatedField");
    // Changing related class
    entityDraft1 = entityService.getEntityDraft(entityDto1.getId());
    DraftData draftData = DraftBuilder.forFieldEdit(entityDraft1.getField("newField").getId(), "metadata.0.value", entityDto3.getClassName());
    entityService.saveDraftEntityChanges(entityDto1.getId(), draftData);
    entityService.commitChanges(entityDto1.getId());
    // We changed related entity, so the old related entity field must be removed
    relatedField = getField(entityDto2.getId(), "relatedField");
    assertNull(relatedField);
    relatedField = getField(entityDto3.getId(), "relatedField");
    assertRelatedField(entityDto1, relatedField, "java.util.Set");
    entityDraft1 = entityService.getEntityDraft(entityDto1.getId());
    draftData = DraftBuilder.forFieldEdit(entityDraft1.getField("newField").getId(), "metadata.1.value", "java.util.List");
    entityService.saveDraftEntityChanges(entityDto1.getId(), draftData);
    draftData = DraftBuilder.forFieldEdit(entityDraft1.getField("newField").getId(), "metadata.2.value", "newNameForRelatedField");
    entityService.saveDraftEntityChanges(entityDto1.getId(), draftData);
    entityService.commitChanges(entityDto1.getId());
    relatedField = getField(entityDto3.getId(), "newNameForRelatedField");
    assertRelatedField(entityDto1, relatedField, "java.util.List");
}
Also used : FieldSetting(org.motechproject.mds.domain.FieldSetting) EntityDto(org.motechproject.mds.dto.EntityDto) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) FieldMetadata(org.motechproject.mds.domain.FieldMetadata) TypeSetting(org.motechproject.mds.domain.TypeSetting) Lookup(org.motechproject.mds.domain.Lookup) DraftData(org.motechproject.mds.dto.DraftData) EntityDraft(org.motechproject.mds.domain.EntityDraft) HashSet(java.util.HashSet) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 4 with EntityDraft

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

the class AllEntityDrafts method create.

public EntityDraft create(Entity entity, String username) {
    EntityDraft draft = new EntityDraft();
    setProperties(draft, entity, username);
    return create(draft);
}
Also used : EntityDraft(org.motechproject.mds.domain.EntityDraft)

Example 5 with EntityDraft

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

the class MdsBundleIT method testUserPreferences.

@Test
public void testUserPreferences() {
    EntityDto entityDto = createEntityForPreferencesTest();
    // first retrieve - should create default user preferences for entity
    UserPreferencesDto userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(new Integer(50), userPreferencesDto.getGridRowsNumber());
    assertEquals(3, userPreferencesDto.getVisibleFields().size());
    assertTrue(userPreferencesDto.getVisibleFields().contains("someBoolean"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("someString"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("someInteger"));
    assertEquals(0, userPreferencesDto.getSelectedFields().size());
    assertEquals(0, userPreferencesDto.getUnselectedFields().size());
    userPreferencesService.updateGridSize(entityDto.getId(), USERNAME, 100);
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(new Integer(100), userPreferencesDto.getGridRowsNumber());
    // if null then default value from settings will be used
    userPreferencesService.updateGridSize(entityDto.getId(), USERNAME, null);
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(new Integer(50), userPreferencesDto.getGridRowsNumber());
    userPreferencesService.unselectField(entityDto.getId(), USERNAME, "someString");
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(2, userPreferencesDto.getVisibleFields().size());
    assertTrue(userPreferencesDto.getVisibleFields().contains("someBoolean"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("someInteger"));
    assertEquals(0, userPreferencesDto.getSelectedFields().size());
    assertEquals(1, userPreferencesDto.getUnselectedFields().size());
    assertTrue(userPreferencesDto.getUnselectedFields().contains("someString"));
    userPreferencesService.selectField(entityDto.getId(), USERNAME, "otherInteger");
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(3, userPreferencesDto.getVisibleFields().size());
    assertTrue(userPreferencesDto.getVisibleFields().contains("someBoolean"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("someInteger"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("otherInteger"));
    assertEquals(1, userPreferencesDto.getSelectedFields().size());
    assertTrue(userPreferencesDto.getSelectedFields().contains("otherInteger"));
    assertEquals(1, userPreferencesDto.getUnselectedFields().size());
    assertTrue(userPreferencesDto.getUnselectedFields().contains("someString"));
    userPreferencesService.selectField(entityDto.getId(), USERNAME, "someString");
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(4, userPreferencesDto.getVisibleFields().size());
    assertTrue(userPreferencesDto.getVisibleFields().contains("someBoolean"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("otherInteger"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("otherInteger"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("someString"));
    assertEquals(2, userPreferencesDto.getSelectedFields().size());
    assertTrue(userPreferencesDto.getSelectedFields().contains("otherInteger"));
    assertTrue(userPreferencesDto.getSelectedFields().contains("someString"));
    assertEquals(0, userPreferencesDto.getUnselectedFields().size());
    userPreferencesService.unselectFields(entityDto.getId(), USERNAME);
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(0, userPreferencesDto.getVisibleFields().size());
    assertEquals(0, userPreferencesDto.getSelectedFields().size());
    assertEquals(10, userPreferencesDto.getUnselectedFields().size());
    userPreferencesService.selectFields(entityDto.getId(), USERNAME);
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(10, userPreferencesDto.getVisibleFields().size());
    assertEquals(10, userPreferencesDto.getSelectedFields().size());
    assertEquals(0, userPreferencesDto.getUnselectedFields().size());
    // if field will be removed from entity then it should be removed also from preferences (CASCADE)
    EntityDraft draft = entityService.getEntityDraft(entityDto.getId());
    List<FieldDto> fields1 = entityService.getEntityFields(draft.getId());
    Long someIntegerId = getFieldIdByName(fields1, "someInteger");
    DraftData draftData = DraftBuilder.forFieldRemoval(someIntegerId);
    entityService.saveDraftEntityChanges(entityDto.getId(), draftData);
    entityService.commitChanges(entityDto.getId());
    userPreferencesDto = userPreferencesService.getUserPreferences(entityDto.getId(), USERNAME);
    assertEquals(9, userPreferencesDto.getVisibleFields().size());
    assertTrue(userPreferencesDto.getSelectedFields().contains("someBoolean"));
    assertTrue(userPreferencesDto.getSelectedFields().contains("someString"));
    assertFalse(userPreferencesDto.getSelectedFields().contains("someInteger"));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) DraftData(org.motechproject.mds.dto.DraftData) UserPreferencesDto(org.motechproject.mds.dto.UserPreferencesDto) EntityDraft(org.motechproject.mds.domain.EntityDraft) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Aggregations

EntityDraft (org.motechproject.mds.domain.EntityDraft)9 EntityDto (org.motechproject.mds.dto.EntityDto)5 Test (org.junit.Test)4 Entity (org.motechproject.mds.domain.Entity)4 Transactional (org.springframework.transaction.annotation.Transactional)4 MdsEntity (org.motechproject.mds.domain.MdsEntity)3 MdsVersionedEntity (org.motechproject.mds.domain.MdsVersionedEntity)3 DraftData (org.motechproject.mds.dto.DraftData)3 FieldDto (org.motechproject.mds.dto.FieldDto)3 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)3 ArrayList (java.util.ArrayList)2 UserPreferencesDto (org.motechproject.mds.dto.UserPreferencesDto)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Field (org.motechproject.mds.domain.Field)1 FieldMetadata (org.motechproject.mds.domain.FieldMetadata)1 FieldSetting (org.motechproject.mds.domain.FieldSetting)1 Lookup (org.motechproject.mds.domain.Lookup)1 Type (org.motechproject.mds.domain.Type)1 TypeSetting (org.motechproject.mds.domain.TypeSetting)1