Search in sources :

Example 6 with EntityDraft

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

the class AllEntityDraftsContextIT method shouldCreateAndDeleteDrafts.

@Test
public void shouldCreateAndDeleteDrafts() {
    EntityDto dto = new EntityDto();
    dto.setClassName("DraftCls");
    Entity entity = allEntities.create(dto);
    allEntityDrafts.create(entity, USERNAME);
    allEntityDrafts.create(entity, USERNAME_2);
    EntityDraft draft = allEntityDrafts.retrieve(entity, USERNAME);
    assertNotNull(draft);
    assertEquals("DraftCls", draft.getClassName());
    assertEquals(USERNAME, draft.getDraftOwnerUsername());
    draft = allEntityDrafts.retrieve(entity, USERNAME_2);
    assertNotNull(draft);
    assertEquals("DraftCls", draft.getClassName());
    assertEquals(USERNAME_2, draft.getDraftOwnerUsername());
    assertNull(allEntityDrafts.retrieve(entity, "otherUser"));
    allEntityDrafts.deleteAll(entity);
    assertTrue(allEntityDrafts.retrieveAll(entity).isEmpty());
    assertNull(allEntityDrafts.retrieve(entity, USERNAME));
    assertNull(allEntityDrafts.retrieve(entity, USERNAME_2));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) Entity(org.motechproject.mds.domain.Entity) EntityDraft(org.motechproject.mds.domain.EntityDraft) Test(org.junit.Test)

Example 7 with EntityDraft

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

the class EntityServiceContextIT method shouldAddNewFieldForLookupAndSaveEntity.

@Test
public void shouldAddNewFieldForLookupAndSaveEntity() throws IOException {
    EntityDto entityDto = new EntityDto();
    entityDto.setName("myEntity");
    entityDto = entityService.createEntity(entityDto);
    // add a new field to draft
    EntityDraft entityDraft = entityService.getEntityDraft(entityDto.getId());
    entityService.saveDraftEntityChanges(entityDraft.getId(), DraftBuilder.forNewField("disp", "testFieldName", Long.class.getName()));
    FieldDto field = selectFirst(entityService.getFields(entityDraft.getId()), having(on(FieldDto.class).getBasic().getName(), equalTo("testFieldName")));
    LookupDto lookup = new LookupDto("lookup", false, false, null, true);
    entityService.addLookups(entityDraft.getId(), Collections.singletonList(lookup));
    Map<String, Object> values = new HashMap<>();
    values.put("path", "indexes.0.$addField");
    values.put("advanced", true);
    values.put("value", Collections.singletonList(field.getId()));
    DraftData draftData = new DraftData();
    draftData.setEdit(true);
    draftData.setValues(values);
    entityService.saveDraftEntityChanges(entityDraft.getId(), draftData);
    entityService.commitChanges(entityDto.getId());
    assertNotNull(entityService.getLookupByName(entityDto.getId(), "lookup").getLookupField("testFieldName"));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) HashMap(java.util.HashMap) LookupDto(org.motechproject.mds.dto.LookupDto) DraftData(org.motechproject.mds.dto.DraftData) EntityDraft(org.motechproject.mds.domain.EntityDraft) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 8 with EntityDraft

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

the class EntityServiceImpl method commitChanges.

@Override
@Transactional
public List<String> commitChanges(Long entityId, String changesOwner) {
    List<String> modulesToRefresh = new ArrayList<>();
    EntityDraft draft = getEntityDraft(entityId, changesOwner);
    if (draft.isOutdated()) {
        throw new EntityChangedException();
    }
    entityValidator.validateEntity(draft);
    Entity parent = draft.getParentEntity();
    String username = draft.getDraftOwnerUsername();
    mdsConstructor.removeFields(parent, draft.getFieldsToRemove());
    mdsConstructor.updateFields(parent, draft.getFieldNameChanges());
    mdsConstructor.removeUniqueIndexes(parent, draft.getUniqueIndexesToDrop());
    mdsConstructor.updateRequired(parent, draft.getFieldNameRequired());
    comboboxDataMigrationHelper.migrateComboboxDataIfNecessary(parent, draft);
    List<UserPreferencesDto> oldEntityPreferences = userPreferencesService.getEntityPreferences(parent.getId());
    configureRelatedFields(parent, draft, modulesToRefresh);
    parent.updateFromDraft(draft);
    updateUserPreferences(parent, draft, oldEntityPreferences);
    if (username != null) {
        allEntityAudits.createAudit(parent, username);
    }
    allEntityDrafts.delete(draft);
    addModuleToRefresh(parent, modulesToRefresh);
    return modulesToRefresh;
}
Also used : MdsEntity(org.motechproject.mds.domain.MdsEntity) Entity(org.motechproject.mds.domain.Entity) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity) EntityChangedException(org.motechproject.mds.exception.entity.EntityChangedException) ArrayList(java.util.ArrayList) UserPreferencesDto(org.motechproject.mds.dto.UserPreferencesDto) EntityDraft(org.motechproject.mds.domain.EntityDraft) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with EntityDraft

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

the class EntityServiceImpl method updateDraft.

@Override
@Transactional
public EntityDto updateDraft(Long entityId) {
    Entity entity = allEntities.retrieveById(entityId);
    EntityDraft draft = getEntityDraft(entityId);
    allEntityDrafts.setProperties(draft, entity);
    draft.setChangesMade(false);
    return draft.toDto();
}
Also used : MdsEntity(org.motechproject.mds.domain.MdsEntity) Entity(org.motechproject.mds.domain.Entity) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity) EntityDraft(org.motechproject.mds.domain.EntityDraft) Transactional(org.springframework.transaction.annotation.Transactional)

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