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;
}
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;
}
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");
}
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);
}
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"));
}
Aggregations