Search in sources :

Example 1 with UserPreferencesDto

use of org.motechproject.mds.dto.UserPreferencesDto in project motech by motech.

the class UserPreferencesServiceTest method shouldMergeFieldsInformation.

@Test
public void shouldMergeFieldsInformation() {
    Set<Field> selectedFields = new HashSet<>();
    selectedFields.add(getField3());
    Set<Field> unselectedFields = new HashSet<>();
    unselectedFields.add(getField2());
    UserPreferences preferences = new UserPreferences(USERNAME, CLASS_NAME, 20, selectedFields, unselectedFields);
    when(allUserPreferences.retrieveByClassNameAndUsername(CLASS_NAME, USERNAME)).thenReturn(preferences);
    UserPreferencesDto userPreferencesDto = userPreferencesService.getUserPreferences(15l, USERNAME);
    assertNotNull(userPreferencesDto);
    assertEquals(1, userPreferencesDto.getSelectedFields().size());
    assertEquals(1, userPreferencesDto.getUnselectedFields().size());
    assertEquals(2, userPreferencesDto.getVisibleFields().size());
    assertTrue(userPreferencesDto.getSelectedFields().contains("sampleField3"));
    assertTrue(userPreferencesDto.getUnselectedFields().contains("sampleField2"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("sampleField1"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("sampleField3"));
}
Also used : AllUserPreferences(org.motechproject.mds.repository.internal.AllUserPreferences) UserPreferences(org.motechproject.mds.domain.UserPreferences) Field(org.motechproject.mds.domain.Field) UserPreferencesDto(org.motechproject.mds.dto.UserPreferencesDto) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with UserPreferencesDto

use of org.motechproject.mds.dto.UserPreferencesDto in project motech by motech.

the class EntityServiceImpl method addEntityUserPreferences.

private void addEntityUserPreferences(AdvancedSettingsDto advancedSettingsDto, Entity entity) {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        UserPreferencesDto userPreferencesDto = userPreferencesService.getUserPreferences(entity.getId(), SecurityContextHolder.getContext().getAuthentication().getName());
        advancedSettingsDto.setUserPreferences(userPreferencesDto);
    }
}
Also used : Authentication(org.springframework.security.core.Authentication) UserPreferencesDto(org.motechproject.mds.dto.UserPreferencesDto)

Example 3 with UserPreferencesDto

use of org.motechproject.mds.dto.UserPreferencesDto in project motech by motech.

the class EntityServiceImpl method updateUserPreferences.

private void updateUserPreferences(Entity parent, EntityDraft draft, List<UserPreferencesDto> userPreferencesDtos) {
    for (UserPreferencesDto preferencesDto : userPreferencesDtos) {
        UserPreferences preferences = allUserPreferences.retrieveByClassNameAndUsername(preferencesDto.getClassName(), preferencesDto.getUsername());
        preferences.setSelectedFields(getFieldsForPreferences(parent, draft, preferencesDto.getSelectedFields()));
        preferences.setUnselectedFields(getFieldsForPreferences(parent, draft, preferencesDto.getUnselectedFields()));
        allUserPreferences.update(preferences);
    }
}
Also used : AllUserPreferences(org.motechproject.mds.repository.internal.AllUserPreferences) UserPreferences(org.motechproject.mds.domain.UserPreferences) UserPreferencesDto(org.motechproject.mds.dto.UserPreferencesDto)

Example 4 with UserPreferencesDto

use of org.motechproject.mds.dto.UserPreferencesDto 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)

Example 5 with UserPreferencesDto

use of org.motechproject.mds.dto.UserPreferencesDto in project motech by motech.

the class EntityServiceContextIT method shouldUpdateUserPreferencesAfterCommit.

@Test
public void shouldUpdateUserPreferencesAfterCommit() {
    EntityDto entityDto = new EntityDto();
    entityDto.setName("UserPrefTest");
    entityDto = entityService.createEntity(entityDto);
    final Long entityId = entityDto.getId();
    entityService.saveDraftEntityChanges(entityId, DraftBuilder.forNewField("disp", "f1name", Long.class.getName()));
    List<FieldDto> fields = entityService.getFields(entityId);
    assertNotNull(fields);
    assertEquals(asList("id", "creator", "owner", "modifiedBy", "creationDate", "modificationDate", "f1name"), extract(fields, on(FieldDto.class).getBasic().getName()));
    entityService.commitChanges(entityId);
    List<Field> draftFields = entityService.getEntityDraft(entityId).getFields();
    Field field = selectFirst(draftFields, having(on(Field.class).getName(), equalTo("f1name")));
    entityService.saveDraftEntityChanges(entityDto.getId(), DraftBuilder.forFieldEdit(field.getId(), "basic.name", "newName"));
    userPreferencesService.selectFields(entityId, "motech");
    userPreferencesService.unselectField(entityId, "motech", "id");
    userPreferencesService.unselectField(entityId, "motech", "owner");
    UserPreferencesDto userPreferencesDto = userPreferencesService.getUserPreferences(entityId, "motech");
    assertEquals(5, userPreferencesDto.getVisibleFields().size());
    assertTrue(userPreferencesDto.getVisibleFields().contains("f1name"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("creator"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("modifiedBy"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("creationDate"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("modificationDate"));
    entityService.commitChanges(entityId);
    userPreferencesDto = userPreferencesService.getUserPreferences(entityId, "motech");
    assertEquals(5, userPreferencesDto.getVisibleFields().size());
    assertTrue(userPreferencesDto.getVisibleFields().contains("newName"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("creator"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("modifiedBy"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("creationDate"));
    assertTrue(userPreferencesDto.getVisibleFields().contains("modificationDate"));
    assertEquals(2, userPreferencesDto.getUnselectedFields().size());
    assertTrue(userPreferencesDto.getUnselectedFields().contains("id"));
    assertTrue(userPreferencesDto.getUnselectedFields().contains("owner"));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) Field(org.motechproject.mds.domain.Field) UserPreferencesDto(org.motechproject.mds.dto.UserPreferencesDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Aggregations

UserPreferencesDto (org.motechproject.mds.dto.UserPreferencesDto)8 UserPreferences (org.motechproject.mds.domain.UserPreferences)4 AllUserPreferences (org.motechproject.mds.repository.internal.AllUserPreferences)4 Test (org.junit.Test)3 Entity (org.motechproject.mds.domain.Entity)3 ArrayList (java.util.ArrayList)2 EntityDraft (org.motechproject.mds.domain.EntityDraft)2 Field (org.motechproject.mds.domain.Field)2 EntityDto (org.motechproject.mds.dto.EntityDto)2 FieldDto (org.motechproject.mds.dto.FieldDto)2 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)2 Transactional (org.springframework.transaction.annotation.Transactional)2 HashSet (java.util.HashSet)1 MdsEntity (org.motechproject.mds.domain.MdsEntity)1 MdsVersionedEntity (org.motechproject.mds.domain.MdsVersionedEntity)1 DraftData (org.motechproject.mds.dto.DraftData)1 EntityChangedException (org.motechproject.mds.exception.entity.EntityChangedException)1 Authentication (org.springframework.security.core.Authentication)1