Esempio n. 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);
    }
  }
Esempio n. 2
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);
    }
  }
Esempio n. 3
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);
  }
Esempio n. 4
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);
  }
Esempio n. 5
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);
  }
Esempio n. 6
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);
  }
Esempio n. 7
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);
  }
Esempio n. 8
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;
  }
Esempio n. 9
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());
  }
Esempio n. 10
0
 @Override
 public List<FieldDto> getEntityFields(Long entityId) {
   validateCredentialsForReading(getEntity(entityId));
   return entityService.getEntityFields(entityId);
 }