示例#1
0
  @Override
  @Transactional
  public <T> Records<T> getRelatedFieldValue(
      Long entityId, Long instanceId, String fieldName, QueryParams queryParams) {
    EntityDto entity = getEntity(entityId);
    validateCredentials(entity);
    String entityName = entity.getName();

    MotechDataService service = getServiceForEntity(entity);
    Object instance = service.findById(instanceId);

    if (instance == null) {
      throw new ObjectNotFoundException(entityName, instanceId);
    }

    try {
      Collection<T> relatedAsColl =
          TypeHelper.asCollection(PropertyUtil.getProperty(instance, fieldName));

      List<T> filtered = InMemoryQueryFilter.filter(relatedAsColl, queryParams);

      int recordCount = relatedAsColl.size();
      int rowCount = (int) Math.ceil(recordCount / (double) queryParams.getPageSize());

      return new Records<>(queryParams.getPage(), rowCount, recordCount, filtered);
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
      throw new ObjectReadException(entityName, e);
    }
  }
示例#2
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());
  }
示例#3
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");
  }
示例#4
0
  @Test
  public void shouldReturnRelatedInstances() {
    mockDataService();
    mockAnotherEntity();
    mockEntity();
    mockSampleFields();
    mockAnotherEntityFields();
    mockTestClassEntity();
    mockTestClassService();
    mockTestClassFields();
    when(serviceForAnotherSample.findById(INSTANCE_ID)).thenReturn(sampleForRelationshipTesting());

    QueryParams queryParams =
        new QueryParams(1, 2, new Order(Constants.Util.ID_FIELD_NAME, Order.Direction.ASC));
    Records<BasicEntityRecord> records =
        instanceService.getRelatedFieldValue(
            ANOTHER_ENTITY_ID, INSTANCE_ID, "testClasses", new RelationshipsUpdate(), queryParams);

    assertNotNull(records);
    assertEquals(Integer.valueOf(1), records.getPage()); // page 1
    assertEquals(Integer.valueOf(2), records.getTotal()); // 2 pages total
    assertEquals(Integer.valueOf(3), records.getRecords()); // 3 records total
    assertEquals(
        asList(1L, 2L),
        extract(records.getRows(), on(BasicEntityRecord.class).getFieldByName("id").getValue()));

    RelationshipsUpdate filter = new RelationshipsUpdate();
    filter.setRemovedIds(Arrays.asList(1L, 2L));
    filter.setAddedIds(Arrays.asList(50L));

    when(testClassMotechDataService.findByIds(filter.getAddedIds()))
        .thenReturn(Arrays.asList(new TestClass(50)));
    records =
        instanceService.getRelatedFieldValue(
            ANOTHER_ENTITY_ID, INSTANCE_ID, "testClasses", filter, queryParams);

    assertNotNull(records);
    assertEquals(Integer.valueOf(1), records.getPage()); // page 1
    assertEquals(Integer.valueOf(1), records.getTotal()); // 1 page total
    assertEquals(Integer.valueOf(2), records.getRecords()); // 2 records total
    // 1L and 2L removed, 50L added
    assertEquals(
        asList(3L, 50L),
        extract(records.getRows(), on(BasicEntityRecord.class).getFieldByName("id").getValue()));
  }
示例#5
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);
    }
  }
示例#6
0
  @Test
  public void shouldUpdateRelatedFields() {
    TestSample test1 = new TestSample("someString", 4);
    TestSample test2 = new TestSample("otherString", 5);
    TestSample test3 = new TestSample("sample", 6);

    RelationshipsUpdate oneToOneUpdate = new RelationshipsUpdate();
    oneToOneUpdate.getAddedIds().add(6L);
    RelationshipsUpdate oneToManyUpdate = buildRelationshipUpdate();

    List<FieldRecord> fieldRecords =
        asList(
            FieldTestHelper.fieldRecord("title", String.class.getName(), "String field", "Default"),
            FieldTestHelper.fieldRecord(
                TypeDto.ONE_TO_MANY_RELATIONSHIP, "testSamples", "Related field", oneToManyUpdate),
            FieldTestHelper.fieldRecord(
                TypeDto.ONE_TO_ONE_RELATIONSHIP,
                "testSample",
                "Other Related field",
                oneToOneUpdate));

    EntityRecord entityRecord = new EntityRecord(null, ANOTHER_ENTITY_ID, fieldRecords);

    mockSampleFields();
    mockDataService();
    mockEntity();

    EntityDto entityWithRelatedField = mock(EntityDto.class);
    when(entityService.getEntity(ANOTHER_ENTITY_ID)).thenReturn(entityWithRelatedField);
    when(entityWithRelatedField.getClassName()).thenReturn(AnotherSample.class.getName());
    when(entityWithRelatedField.getId()).thenReturn(ENTITY_ID + 1);

    ServiceReference serviceReferenceForClassWithRelatedField = mock(ServiceReference.class);
    MotechDataService serviceForClassWithRelatedField = mock(MotechDataService.class);
    when(bundleContext.getServiceReference(
            ClassName.getInterfaceName(AnotherSample.class.getName())))
        .thenReturn(serviceReferenceForClassWithRelatedField);
    when(bundleContext.getService(serviceReferenceForClassWithRelatedField))
        .thenReturn(serviceForClassWithRelatedField);
    when(motechDataService.findById(4L)).thenReturn(test1);
    when(motechDataService.findById(5L)).thenReturn(test2);
    when(motechDataService.findById(6L)).thenReturn(test3);
    when(motechDataService.findByIds(oneToManyUpdate.getAddedIds()))
        .thenReturn(Arrays.asList(test1, test2));

    when(entityService.getEntityFieldsForUI(ANOTHER_ENTITY_ID))
        .thenReturn(
            asList(
                FieldTestHelper.fieldDto(
                    5L, "title", String.class.getName(), "String field", "Default"),
                FieldTestHelper.fieldDto(
                    6L,
                    "testSamples",
                    TypeDto.ONE_TO_MANY_RELATIONSHIP.getTypeClass(),
                    "Related field",
                    null)));

    ArgumentCaptor<AnotherSample> captor = ArgumentCaptor.forClass(AnotherSample.class);
    instanceService.saveInstance(entityRecord, null);

    verify(serviceForClassWithRelatedField).create(captor.capture());
    AnotherSample capturedValue = captor.getValue();
    assertEquals(capturedValue.getTestSample(), test3);
    assertEquals(capturedValue.getTestSamples().size(), 3);
    assertEquals(capturedValue.getTitle(), "Default");
    assertTrue(capturedValue.getTestSamples().contains(test1));
    assertFalse(capturedValue.getTestSamples().contains(test3));
    assertTrue(capturedValue.getTestSamples().contains(test2));
  }
示例#7
0
 private Object findRelatedObjectById(Object id, MotechDataService service) {
   // We need parse id value to the long type
   return service.findById(TypeHelper.parseNumber(id, Long.class.getName()).longValue());
 }