Ejemplo n.º 1
0
  private void processAnnotationScanningResults(
      List<EntityProcessorOutput> entityProcessorOutput,
      Map<String, List<LookupDto>> lookupProcessingResult) {
    Map<String, Long> entityIdMappings = new HashMap<>();

    for (EntityProcessorOutput result : entityProcessorOutput) {
      EntityDto processedEntity = result.getEntityProcessingResult();

      EntityDto entity = entityService.getEntityByClassName(processedEntity.getClassName());

      if (entity == null) {
        entity = entityService.createEntity(processedEntity);
      }
      entityIdMappings.put(entity.getClassName(), entity.getId());

      entityService.updateRestOptions(entity.getId(), result.getRestProcessingResult());
      entityService.updateTracking(entity.getId(), result.getTrackingProcessingResult());
      entityService.addFields(entity, result.getFieldProcessingResult());
      entityService.addFilterableFields(entity, result.getUiFilterableProcessingResult());
      entityService.addDisplayedFields(entity, result.getUiDisplayableProcessingResult());
      entityService.updateSecurityOptions(
          entity.getId(), processedEntity.getSecurityMode(), processedEntity.getSecurityMembers());
      entityService.updateMaxFetchDepth(entity.getId(), processedEntity.getMaxFetchDepth());
      entityService.addNonEditableFields(entity, result.getNonEditableProcessingResult());
    }

    for (Map.Entry<String, List<LookupDto>> entry : lookupProcessingResult.entrySet()) {
      entityService.addLookups(entityIdMappings.get(entry.getKey()), entry.getValue());
    }
  }
Ejemplo n.º 2
0
  private EntityRecord instanceToRecord(
      Object instance, EntityDto entityDto, List<FieldDto> fields, MotechDataService service) {
    if (instance == null) {
      return null;
    }
    try {
      List<FieldRecord> fieldRecords = new ArrayList<>();

      for (FieldDto field : fields) {
        Object value = getProperty(instance, field, service);
        Object displayValue = getDisplayValueForField(field, value);

        value =
            parseValueForDisplay(value, field.getMetadata(Constants.MetadataKeys.RELATED_FIELD));

        FieldRecord fieldRecord = new FieldRecord(field);
        fieldRecord.setValue(value);
        fieldRecord.setDisplayValue(displayValue);
        fieldRecords.add(fieldRecord);
      }

      Number id = (Number) PropertyUtil.safeGetProperty(instance, ID_FIELD_NAME);
      return new EntityRecord(id == null ? null : id.longValue(), entityDto.getId(), fieldRecords);
    } catch (Exception e) {
      throw new ObjectReadException(entityDto.getName(), e);
    }
  }
Ejemplo n.º 3
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);
    }
  }
Ejemplo n.º 4
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());
  }
Ejemplo n.º 5
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);
 }
Ejemplo n.º 6
0
 private void mockTestClassEntity() {
   EntityDto testClassEntity = mock(EntityDto.class);
   when(testClassEntity.getClassName()).thenReturn(TestClass.class.getName());
   when(testClassEntity.getId()).thenReturn(TEST_CLASS_ID);
   mockEntity(TestClass.class, TEST_CLASS_ID, testClassEntity);
 }
Ejemplo n.º 7
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));
  }
Ejemplo n.º 8
0
 @Before
 public void setUp() {
   when(entity.getClassName()).thenReturn(TestSample.class.getName());
   when(entity.getId()).thenReturn(ENTITY_ID);
   when(bundleContext.getBundles()).thenReturn(new Bundle[0]);
 }
Ejemplo n.º 9
0
 @Override
 public List<LookupDto> getAvailableLookups(String entityName) {
   EntityDto entity = getEntityByEntityClassName(entityName);
   AdvancedSettingsDto settingsDto = entityService.getAdvancedSettings(entity.getId(), true);
   return settingsDto.getIndexes();
 }