コード例 #1
0
  @Override
  public List<EntityRecord> getEntityRecordsFromLookup(
      Long entityId, String lookupName, Map<String, Object> lookupMap, QueryParams queryParams) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);

    LookupDto lookup = getLookupByName(entityId, lookupName);
    List<FieldDto> fields = entityService.getEntityFields(entityId);
    Map<String, FieldDto> fieldMap = entityService.getLookupFieldsMapping(entityId, lookupName);

    MotechDataService service = getServiceForEntity(entity);

    try {
      LookupExecutor lookupExecutor = new LookupExecutor(service, lookup, fieldMap);

      Object result = lookupExecutor.execute(lookupMap, queryParams);

      if (lookup.isSingleObjectReturn()) {
        EntityRecord record = instanceToRecord(result, entity, fields, service);
        return (record == null) ? new ArrayList<EntityRecord>() : Collections.singletonList(record);
      } else {
        List instances = (List) result;
        return instancesToRecords(instances, entity, fields, service);
      }
    } catch (RuntimeException e) {
      throw new LookupExecutionException(e);
    }
  }
コード例 #2
0
  @Override
  public List<FieldInstanceDto> getInstanceFields(Long entityId, Long instanceId) {
    EntityDto entity = entityService.getEntity(entityId);
    validateCredentialsForReading(entity);

    List<FieldDto> fields = entityService.getEntityFields(entityId);

    List<FieldInstanceDto> result = new ArrayList<>();
    for (FieldDto field : fields) {
      FieldInstanceDto fieldInstanceDto =
          new FieldInstanceDto(field.getId(), instanceId, field.getBasic());
      result.add(fieldInstanceDto);
    }

    return result;
  }
コード例 #3
0
 private EntityDto getEntity(Long entityId) {
   EntityDto entityDto = entityService.getEntity(entityId);
   if (entityDto == null) {
     throw new EntityNotFoundException(entityId);
   }
   return entityDto;
 }
コード例 #4
0
ファイル: LookupServiceImpl.java プロジェクト: koshalt/ebodac
 private EntityDto getEntityByEntityClassName(String entityName) {
   EntityDto entity = entityService.getEntityByClassName(entityName);
   if (entity == null) {
     throw new EbodacLookupException("Can not find entity named: " + entityName);
   }
   return entity;
 }
コード例 #5
0
  @Override
  public void revertInstanceFromTrash(Long entityId, Long instanceId) {
    EntityDto entity = getEntity(entityId);
    validateCredentials(entity);
    validateNonEditableProperty(entity);
    MotechDataService service = getServiceForEntity(entity);

    Object trash = service.findTrashInstanceById(instanceId, entityId);
    List<FieldRecord> fieldRecords = new LinkedList<>();

    try {
      for (FieldDto field : entityService.getEntityFields(entity.getId())) {
        if (ID_FIELD_NAME.equalsIgnoreCase(field.getBasic().getDisplayName())) {
          continue;
        }
        Field f =
            FieldUtils.getField(
                trash.getClass(), StringUtils.uncapitalize(field.getBasic().getName()), true);
        FieldRecord record = new FieldRecord(field);
        record.setValue(f.get(trash));
        fieldRecords.add(record);
      }

      Class<?> entityClass = getEntityClass(entity);

      Object newInstance = entityClass.newInstance();
      updateFields(newInstance, fieldRecords, service, null);

      service.revertFromTrash(newInstance, trash);
    } catch (Exception e) {
      throw new RevertFromTrashException(entity.getName(), instanceId, e);
    }
  }
コード例 #6
0
 private LookupDto getLookupByName(Long entityId, String lookupName) {
   LookupDto lookup = entityService.getLookupByName(entityId, lookupName);
   if (lookup == null) {
     throw new LookupNotFoundException(entityId, lookupName);
   }
   return lookup;
 }
コード例 #7
0
  @Test(expected = EntityInstancesNonEditableException.class)
  public void shouldThrowExceptionWhileDeletingInstanceInNonEditableEntity() {
    EntityDto nonEditableEntity = new EntityDto();
    nonEditableEntity.setNonEditable(true);

    when(entityService.getEntity(ANOTHER_ENTITY_ID)).thenReturn(nonEditableEntity);

    instanceService.deleteInstance(ANOTHER_ENTITY_ID, INSTANCE_ID);
  }
コード例 #8
0
  private HistoryRecord convertToHistoryRecord(
      Object object, EntityDto entity, Long instanceId, MotechDataService service) {
    Long entityId = entity.getId();

    EntityRecord entityRecord =
        instanceToRecord(object, entity, entityService.getEntityFields(entityId), service);
    Long historyInstanceSchemaVersion =
        (Long)
            PropertyUtil.safeGetProperty(
                object, HistoryTrashClassHelper.schemaVersion(object.getClass()));
    Long currentSchemaVersion = entityService.getCurrentSchemaVersion(entity.getClassName());

    return new HistoryRecord(
        entityRecord.getId(),
        instanceId,
        historyInstanceSchemaVersion.equals(currentSchemaVersion),
        entityRecord.getFields());
  }
コード例 #9
0
  @Test
  public void shouldCreateInstanceOfSubclassedEntityWithRelation() {
    mockEntity(SubclassSample.class, ENTITY_ID, entity);
    mockDataService(SubclassSample.class, motechDataService);
    when(motechDataService.retrieve("id", INSTANCE_ID)).thenReturn(new SubclassSample());
    when(entityService.getEntityFieldsForUI(ENTITY_ID))
        .thenReturn(
            asList(
                FieldTestHelper.fieldDto(
                    1L, "superclassInteger", Integer.class.getName(), "Superclass Integer", 7),
                FieldTestHelper.fieldDto(
                    2L, "subclassString", String.class.getName(), "Subclass String", "test"),
                FieldTestHelper.fieldDto(
                    3L,
                    "superclassRelation",
                    TypeDto.ONE_TO_ONE_RELATIONSHIP.getTypeClass(),
                    "Superclass Relationship",
                    null)));

    long relationEntityId = ANOTHER_ENTITY_ID;
    long relationInstanceId = INSTANCE_ID + 1;
    EntityDto relationEntity = mock(EntityDto.class);
    MotechDataService relationDataService = mock(MotechDataService.class);
    mockEntity(TestSample.class, relationEntityId, relationEntity);
    mockDataService(TestSample.class, relationDataService);
    TestSample relatedInstance = new TestSample("test sample", 42);
    when(relationDataService.retrieve("id", relationInstanceId)).thenReturn(relatedInstance);
    when(relationDataService.findById(relationInstanceId)).thenReturn(relatedInstance);

    RelationshipsUpdate relationshipsUpdate = new RelationshipsUpdate();
    relationshipsUpdate.getAddedIds().add(relationInstanceId);

    EntityRecord createRecord =
        new EntityRecord(
            null,
            ENTITY_ID,
            asList(
                FieldTestHelper.fieldRecord("superclassInteger", Integer.class.getName(), "", 77),
                FieldTestHelper.fieldRecord(
                    "subclassString", String.class.getName(), "", "test test"),
                FieldTestHelper.fieldRecord(
                    TypeDto.ONE_TO_ONE_RELATIONSHIP,
                    "superclassRelation",
                    "",
                    relationshipsUpdate)));

    ArgumentCaptor<SubclassSample> createCaptor = ArgumentCaptor.forClass(SubclassSample.class);
    instanceService.saveInstance(createRecord);
    verify(motechDataService).create(createCaptor.capture());

    SubclassSample instance = createCaptor.getValue();
    assertEquals(77, (int) instance.getSuperclassInteger());
    assertEquals("test test", instance.getSubclassString());
    assertNotNull(instance.getSuperclassRelation());
    assertEquals(relatedInstance.getStrField(), instance.getSuperclassRelation().getStrField());
    assertEquals(relatedInstance.getIntField(), instance.getSuperclassRelation().getIntField());
  }
コード例 #10
0
  @Override
  public List<EntityRecord> getEntityRecords(Long entityId, QueryParams queryParams) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);
    List<FieldDto> fields = entityService.getEntityFields(entityId);

    MotechDataService service = getServiceForEntity(entity);
    List instances = service.retrieveAll(queryParams);

    return instancesToRecords(instances, entity, fields, service);
  }
コード例 #11
0
  @Override
  public List<EntityRecord> getTrashRecords(Long entityId, QueryParams queryParams) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);

    MotechDataService service = getServiceForEntity(entity);
    List<FieldDto> fields = entityService.getEntityFields(entityId);
    Collection collection = trashService.getInstancesFromTrash(entity.getClassName(), queryParams);

    return instancesToRecords(collection, entity, fields, service);
  }
コード例 #12
0
  @Override
  public EntityRecord getSingleTrashRecord(Long entityId, Long instanceId) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);

    MotechDataService service = getServiceForEntity(entity);
    List<FieldDto> fields = entityService.getEntityFields(entityId);
    Object instance = trashService.findTrashById(instanceId, entityId);

    return instanceToRecord(instance, entity, fields, service);
  }
コード例 #13
0
  @Test(expected = EntityInstancesNonEditableException.class)
  public void shouldThrowExceptionWhileSavingInstanceInNonEditableEntity() {
    EntityDto nonEditableEntity = new EntityDto();
    nonEditableEntity.setNonEditable(true);
    nonEditableEntity.setId(ANOTHER_ENTITY_ID);
    EntityRecord entityRecord =
        new EntityRecord(null, ANOTHER_ENTITY_ID, new ArrayList<FieldRecord>());

    when(entityService.getEntity(ANOTHER_ENTITY_ID)).thenReturn(nonEditableEntity);

    instanceService.saveInstance(entityRecord);
  }
コード例 #14
0
  private void mockAnotherEntity() {
    EntityDto entityWithRelatedField = mock(EntityDto.class);
    when(entityService.getEntity(ANOTHER_ENTITY_ID)).thenReturn(entityWithRelatedField);
    when(entityWithRelatedField.getClassName()).thenReturn(AnotherSample.class.getName());
    when(entityWithRelatedField.getName()).thenReturn(AnotherSample.class.getSimpleName());

    ServiceReference serviceReferenceForAnotherSample = mock(ServiceReference.class);
    when(bundleContext.getServiceReference(
            ClassName.getInterfaceName(AnotherSample.class.getName())))
        .thenReturn(serviceReferenceForAnotherSample);
    when(bundleContext.getService(serviceReferenceForAnotherSample))
        .thenReturn(serviceForAnotherSample);
  }
コード例 #15
0
  private void mockAnotherEntityFields() {
    FieldDto relatedField =
        FieldTestHelper.fieldDto(
            2L, "testClasses", OneToManyRelationship.class.getName(), "Test Classes", null);
    relatedField.addMetadata(
        new MetadataDto(Constants.MetadataKeys.RELATED_CLASS, TestClass.class.getName()));

    when(entityService.getEntityFieldsForUI(ANOTHER_ENTITY_ID))
        .thenReturn(
            asList(
                FieldTestHelper.fieldDto(1L, "id", Long.class.getName(), "Id", null),
                relatedField));
  }
コード例 #16
0
  @Test(expected = SecurityException.class)
  public void shouldThrowExceptionWhileReadingInstanceWithoutAnyPermission() {
    EntityDto entityDto = new EntityDto();
    entityDto.setReadOnlySecurityMode(SecurityMode.NO_ACCESS);
    entityDto.setSecurityMode(SecurityMode.NO_ACCESS);

    EntityRecord entityRecord =
        new EntityRecord(ANOTHER_ENTITY_ID, null, new ArrayList<FieldRecord>());

    when(entityService.getEntity(ANOTHER_ENTITY_ID)).thenReturn(entityDto);

    instanceService.getEntityRecords(entityRecord.getId());
  }
コード例 #17
0
  @Override
  public EntityRecord newInstance(Long entityId) {
    validateCredentials(getEntity(entityId));
    List<FieldDto> fields = entityService.getEntityFields(entityId);
    List<FieldRecord> fieldRecords = new ArrayList<>();

    for (FieldDto field : fields) {
      FieldRecord fieldRecord = new FieldRecord(field);
      fieldRecords.add(fieldRecord);
    }
    populateDefaultFields(fieldRecords);

    return new EntityRecord(null, entityId, fieldRecords);
  }
コード例 #18
0
  @Test
  public void shouldLoadBlobField() throws InstanceNotFoundException {
    EntityDto entityDto = new EntityDto();
    entityDto.setReadOnlySecurityMode(null);
    entityDto.setSecurityMode(null);
    entityDto.setClassName(TestSample.class.getName());

    when(entityService.getEntity(ENTITY_ID + 1)).thenReturn(entityDto);
    mockDataService();
    TestSample instance = Mockito.mock(TestSample.class);
    when(motechDataService.findById(ENTITY_ID + 1)).thenReturn(instance);

    instanceService.getInstanceField(12l, ENTITY_ID + 1, "blobField");
    verify(motechDataService).getDetachedField(instance, "blobField");
  }
コード例 #19
0
  @Override
  public EntityRecord getEntityInstance(Long entityId, Long instanceId) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);
    MotechDataService service = getServiceForEntity(entity);

    Object instance = service.retrieve(ID_FIELD_NAME, instanceId);
    if (instance == null) {
      throw new ObjectNotFoundException(entity.getName(), instanceId);
    }

    List<FieldDto> fields = entityService.getEntityFields(entityId);

    return instanceToRecord(instance, entity, fields, service);
  }
コード例 #20
0
  @Test
  public void shouldAcceptUserWithNoPermissionWhileReadingInstanceWithNoSecurityMode() {
    EntityDto entityDto = new EntityDto();
    entityDto.setReadOnlySecurityMode(null);
    entityDto.setSecurityMode(null);
    entityDto.setClassName(TestSample.class.getName());

    EntityRecord entityRecord = new EntityRecord(ANOTHER_ENTITY_ID, null, new ArrayList<>());

    when(entityService.getEntity(ANOTHER_ENTITY_ID)).thenReturn(entityDto);

    mockDataService();
    instanceService.getEntityRecords(entityRecord.getId());

    verify(entityService).getEntityFieldsForUI(ANOTHER_ENTITY_ID);
  }
コード例 #21
0
  @Test
  public void shouldAutoPopulateOwnerAndCreator() {
    when(entityService.getEntityFieldsForUI(ENTITY_ID))
        .thenReturn(
            asList(
                FieldTestHelper.fieldDto(1L, "owner", String.class.getName(), "String field", null),
                FieldTestHelper.fieldDto(
                    1L, "creator", String.class.getName(), "String field", null)));
    mockEntity();
    setUpSecurityContext();

    EntityRecord record = instanceService.newInstance(ENTITY_ID);

    List<FieldRecord> fieldRecords = record.getFields();
    assertEquals(
        asList("motech", "motech"), extract(fieldRecords, on(FieldRecord.class).getValue()));
  }
コード例 #22
0
  @Test
  public void shouldNotUpdateGridSizeWhenUsernameIsBlank() {
    EntityDto entityDto = new EntityDto();
    entityDto.setReadOnlySecurityMode(null);
    entityDto.setSecurityMode(null);
    entityDto.setClassName(TestSample.class.getName());

    EntityRecord entityRecord = new EntityRecord(ENTITY_ID + 1, null, new ArrayList<FieldRecord>());

    when(entityService.getEntity(ENTITY_ID + 1)).thenReturn(entityDto);

    mockDataService();
    instanceService.getEntityRecords(entityRecord.getId(), new QueryParams(1, 100));

    verify(entityService).getEntityFieldsForUI(ENTITY_ID + 1);
    verify(userPreferencesService, never()).updateGridSize(anyLong(), anyString(), anyInt());
  }
コード例 #23
0
  private void mockLookups() {
    LookupDto lookup =
        new LookupDto(
            TestDataService.LOOKUP_1_NAME,
            true,
            true,
            asList(FieldTestHelper.lookupFieldDto(1L, "strField")),
            true,
            "singleObject",
            asList("strField"));
    when(entityService.getLookupByName(ENTITY_ID, TestDataService.LOOKUP_1_NAME))
        .thenReturn(lookup);
    Map<String, FieldDto> mapping = new HashMap<>();
    mapping.put(
        "strField",
        FieldTestHelper.fieldDto(
            1L, "strField", String.class.getName(), "String field", "Default"));
    when(entityService.getLookupFieldsMapping(ENTITY_ID, TestDataService.LOOKUP_1_NAME))
        .thenReturn(mapping);

    lookup =
        new LookupDto(
            TestDataService.LOOKUP_2_NAME,
            false,
            true,
            asList(FieldTestHelper.lookupFieldDto(1L, "strField")),
            false,
            "multiObject",
            asList("strField"));
    when(entityService.getLookupByName(ENTITY_ID, TestDataService.LOOKUP_2_NAME))
        .thenReturn(lookup);
    when(entityService.getLookupFieldsMapping(ENTITY_ID, TestDataService.LOOKUP_2_NAME))
        .thenReturn(mapping);

    lookup =
        new LookupDto(
            TestDataService.NULL_EXPECTING_LOOKUP_NAME,
            false,
            true,
            asList(FieldTestHelper.lookupFieldDto(3L, "dtField")),
            false,
            "nullParamExpected",
            asList("dtField"));
    when(entityService.getLookupByName(ENTITY_ID, TestDataService.NULL_EXPECTING_LOOKUP_NAME))
        .thenReturn(lookup);
    mapping = new HashMap<>();
    mapping.put(
        "dtField",
        FieldTestHelper.fieldDto(3L, "dtField", DateTime.class.getName(), "DateTime field", null));
    when(entityService.getLookupFieldsMapping(
            ENTITY_ID, TestDataService.NULL_EXPECTING_LOOKUP_NAME))
        .thenReturn(mapping);
  }
コード例 #24
0
  @Override
  public long countRecordsByLookup(
      Long entityId, String lookupName, Map<String, Object> lookupMap) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);

    LookupDto lookup = getLookupByName(entityId, lookupName);
    Map<String, FieldDto> fieldMap = entityService.getLookupFieldsMapping(entityId, lookupName);

    MotechDataService service = getServiceForEntity(entity);

    try {
      LookupExecutor lookupExecutor = new LookupExecutor(service, lookup, fieldMap);
      return lookupExecutor.executeCount(lookupMap);
    } catch (RuntimeException e) {
      throw new LookupExecutionException(e);
    }
  }
コード例 #25
0
  @Test
  public void shouldUpdateGridSize() {
    setUpSecurityContext();

    EntityDto entityDto = new EntityDto();
    entityDto.setReadOnlySecurityMode(null);
    entityDto.setSecurityMode(null);
    entityDto.setClassName(TestSample.class.getName());

    EntityRecord entityRecord = new EntityRecord(ENTITY_ID + 1, null, new ArrayList<>());

    when(entityService.getEntity(ENTITY_ID + 1)).thenReturn(entityDto);

    mockDataService();
    instanceService.getEntityRecords(entityRecord.getId(), new QueryParams(1, 100));

    verify(entityService).getEntityFieldsForUI(ENTITY_ID + 1);
    verify(userPreferencesService).updateGridSize(ENTITY_ID + 1, "motech", 100);
  }
コード例 #26
0
  @Test
  public void shouldNotAutoPopulateOwnerAndCreatorForNonEditableFields() {
    FieldDto ownerField =
        FieldTestHelper.fieldDto(1L, "owner", String.class.getName(), "String field", null);
    ownerField.setNonEditable(true);
    FieldDto creatorField =
        FieldTestHelper.fieldDto(1L, "creator", String.class.getName(), "String field", null);
    creatorField.setNonEditable(true);

    when(entityService.getEntityFieldsForUI(ENTITY_ID))
        .thenReturn(asList(ownerField, creatorField));
    mockEntity();
    setUpSecurityContext();

    EntityRecord record = instanceService.newInstance(ENTITY_ID);

    List<FieldRecord> fieldRecords = record.getFields();
    assertEquals(asList(null, null), extract(fieldRecords, on(FieldRecord.class).getValue()));
  }
コード例 #27
0
  private void mockSampleFields() {

    when(entityService.getEntityFieldsForUI(ENTITY_ID))
        .thenReturn(
            asList(
                FieldTestHelper.fieldDto(
                    1L, "strField", String.class.getName(), "String field", "Default"),
                FieldTestHelper.fieldDto(
                    2L, "intField", Integer.class.getName(), "Integer field", 7),
                FieldTestHelper.fieldDto(
                    3L, "dtField", DateTime.class.getName(), "DateTime field", null),
                FieldTestHelper.fieldDto(4L, "timeField", Time.class.getName(), "Time field", null),
                // In case of EUDE was created with field name starting from capital letter
                // that capitalized field name will be passed in FieldDto (as LongField in this
                // case).
                // InstanceService should be able to make operations on record regardless of field
                // starts with a capital letter or not.
                FieldTestHelper.fieldDto(
                    5L, "LongField", Long.class.getName(), "Long field", null)));
  }
コード例 #28
0
ファイル: SchemaComparator.java プロジェクト: aes42/motech
  /**
   * Returns true if lookups are different, false otherwise. It compares only read-only lookups,
   * that created in the code by developer.
   *
   * @param entityId the id of entity
   * @param newLookups the newLookups defined in the code
   * @return true if lookups are different, false otherwise.
   */
  public boolean lookupsDiffer(Long entityId, List<LookupDto> newLookups) {
    List<LookupDto> existingLookups = entityService.getEntityLookups(entityId);
    Map<String, LookupDto> entityLookupsMappings = new HashMap<>();

    for (LookupDto entityLookup : existingLookups) {
      if (entityLookup.isReadOnly()) {
        entityLookupsMappings.put(entityLookup.getLookupName(), entityLookup);
      }
    }

    if (entityLookupsMappings.size() != newLookups.size()) {
      return true;
    }

    for (LookupDto lookup : newLookups) {
      if (!lookupEquals(lookup, entityLookupsMappings.get(lookup.getLookupName()))) {
        return true;
      }
    }

    return false;
  }
コード例 #29
0
  @Override
  public FieldRecord getInstanceValueAsRelatedField(Long entityId, Long fieldId, Long instanceId) {
    validateCredentialsForReading(getEntity(entityId));
    try {
      FieldRecord fieldRecord;
      FieldDto field = entityService.getEntityFieldById(entityId, fieldId);
      MotechDataService service =
          DataServiceHelper.getDataService(
              bundleContext, field.getMetadata(RELATED_CLASS).getValue());

      Object instance = service.findById(instanceId);
      if (instance == null) {
        throw new ObjectNotFoundException(service.getClassType().getName(), instanceId);
      }
      fieldRecord = new FieldRecord(field);
      fieldRecord.setValue(
          parseValueForDisplay(instance, field.getMetadata(Constants.MetadataKeys.RELATED_FIELD)));
      fieldRecord.setDisplayValue(instance.toString());
      return fieldRecord;
    } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
      throw new ObjectReadException(entityId, e);
    }
  }
コード例 #30
0
 private void mockEntity(Class<?> entityClass, long entityId, EntityDto entity) {
   when(entityService.getEntity(entityId)).thenReturn(entity);
   when(entityService.getEntityByClassName(entityClass.getName())).thenReturn(entity);
   when(entity.getClassName()).thenReturn(entityClass.getName());
   when(entity.getId()).thenReturn(entityId);
 }