private TemplateResult updateRelation(
      DataAccess access, DataTemplate template, Map<String, String> param) throws BaseException {
    DataSource dataSource = getDataSource(access, param.get(Helper.Keys.SourceId));
    if (dataSource.getState() != DataInformation.DataState.READY)
      throw new NotReadyException("The datasource %s is not yet ready.", dataSource.getId());

    String relationId = param.get(Helper.Keys.RelationId);
    if (relationId == null)
      throw new MissingArgumentException("The argument %s is missing!", Helper.Keys.RelationId);

    String fromId = param.get(Helper.Keys.FromId);
    if (fromId == null)
      throw new MissingArgumentException("The argument %s is missing!", Helper.Keys.FromId);

    String toId = param.get(Helper.Keys.ToId);
    if (toId == null)
      throw new MissingArgumentException("The argument %s is missing!", Helper.Keys.ToId);

    String type = param.get(Helper.Keys.Type);
    if (type == null)
      throw new MissingArgumentException("The argument %s is missing!", Helper.Keys.Type);

    Relation relation = dataSource.updateRelation(relationId, fromId, toId, type, param);
    return template.transform(relation);
  }
  private TemplateResult createItem(
      DataAccess access, DataTemplate template, Map<String, String> param) throws BaseException {
    DataSource dataSource = getDataSource(access, param.get(Helper.Keys.SourceId));
    if (dataSource.getState() != DataInformation.DataState.READY)
      throw new NotReadyException("The datasource %s is not yet ready.", dataSource.getId());

    Item item = dataSource.createItem(param);
    return template.transform(item);
  }
  private TemplateResult updateUser(
      DataAccess access, DataTemplate template, Map<String, String> param) throws BaseException {
    DataSource dataSource = getDataSource(access, param.get(Helper.Keys.SourceId));
    if (dataSource.getState() != DataInformation.DataState.READY)
      throw new NotReadyException("The datasource %s is not yet ready.", dataSource.getId());

    String userId = param.get(Helper.Keys.UserId);

    User user = dataSource.updateUser(userId, param);
    return template.transform(user);
  }
  private TemplateResult getUsers(
      DataAccess access, DataTemplate template, Map<String, String> param) throws BaseException {
    DataSource dataSource = getDataSource(access, param.get(Helper.Keys.SourceId));
    if (dataSource.getState() != DataInformation.DataState.READY)
      throw new NotReadyException("The datasource %s is not yet ready.", dataSource.getId());

    User[] users = dataSource.getUsers();

    users = Helper.applyPaging(users, param);

    return template.transform(users);
  }
  private TemplateResult getInteraction(
      DataAccess access, DataTemplate template, Map<String, String> param) throws BaseException {
    DataSource dataSource = getDataSource(access, param.get(Helper.Keys.SourceId));
    if (dataSource.getState() != DataInformation.DataState.READY)
      throw new NotReadyException("The datasource %s is not yet ready.", dataSource.getId());

    String itemId = param.get(Helper.Keys.ItemId);
    String userId = param.get(Helper.Keys.UserId);

    Interaction[] interaction = dataSource.getInteractions(itemId, userId);

    interaction = Helper.applyPaging(interaction, param);

    return template.transform(interaction);
  }
Example #6
0
  @Override
  public void close() throws IOException {
    if (threads != null) {
      threads.values().forEach(java.lang.Thread::interrupt);
    }

    if (dataSourceBuilderListener != null) {
      this.context.removeServiceListener(dataSourceBuilderListener);
    }

    if (dataSources != null) {
      for (DataSource s : dataSources.values()) {
        s.close();
      }
    }
  }
Example #7
0
  private DataSource connectDataSource(Map<String, String> content) throws BaseException {
    String typeKey = content.get(Helper.Keys.DataBuilderId);
    String dataSourceId = content.get(Helper.Keys.SourceId);

    if (typeKey == null)
      throw new MissingArgumentException(
          String.format("Argument %s is missing.", Helper.Keys.DataBuilderId));
    if (dataSourceId == null)
      throw new MissingArgumentException(
          String.format("Argument %s is missing.", Helper.Keys.SourceId));

    DataSourceBuilder connection = dataSourceBuilderListener.getInstance(typeKey);

    DataSource source = connection.createInstance(dataSourceId, content);
    source.setState(DataInformation.DataState.CONNECTING);

    Thread thread =
        new Thread() {
          public void run() {
            try {
              source.connect();
              source.setState(DataInformation.DataState.READY);
              threads.remove(source.getId());
            } catch (BaseException e) {
              e.printStackTrace();
              threads.remove(source.getId());
              dataSources.remove(source.getId());
            }
          }
        };

    threads.put(dataSourceId, thread);

    thread.start();

    return source;
  }
  private TemplateResult addInteraction(
      DataAccess access, DataTemplate template, Map<String, String> param) throws BaseException {
    DataSource dataSource = getDataSource(access, param.get(Helper.Keys.SourceId));

    if (dataSource.getState() != DataInformation.DataState.READY)
      throw new NotReadyException("The datasource %s is not yet ready.", dataSource.getId());

    String itemId = param.get(Helper.Keys.ItemId);
    String userId = param.get(Helper.Keys.UserId);
    String type = param.get(Helper.Keys.Type);
    String value = param.get(Helper.Keys.Value);
    String timestamp = param.get(Helper.Keys.TimeStamp);

    Date date = null;
    if (timestamp != null && !timestamp.isEmpty()) {
      Long t = Long.parseLong(timestamp);
      if (t != null) {
        date = new Date(t);
      }
    }

    if (date == null) {
      date = new Date();
    }

    if (dataSource.tryGetItem(itemId) == null) {
      Map<String, String> temp = new HashMap<>();
      temp.put(Helper.Keys.ItemId, itemId);
      dataSource.createItem(temp);
    }

    if (dataSource.tryGetUser(userId) == null) {
      Map<String, String> temp = new HashMap<>();
      temp.put(Helper.Keys.UserId, userId);
      dataSource.createUser(temp);
    }

    Message message = dataSource.addInteraction(itemId, userId, date, type, value, param);
    return template.transform(message);
  }
  private TemplateResult getRelations(
      DataAccess access, DataTemplate template, Map<String, String> param) throws BaseException {
    DataSource dataSource = getDataSource(access, param.get(Helper.Keys.SourceId));
    if (dataSource.getState() != DataInformation.DataState.READY)
      throw new NotReadyException("The datasource %s is not yet ready.", dataSource.getId());

    String relationId = param.get(Helper.Keys.RelationId);
    String fromId = param.get(Helper.Keys.FromId);
    String toId = param.get(Helper.Keys.ToId);

    if (relationId != null) {
      Relation relation = dataSource.getRelation(relationId);
      return template.transform(relation);
    } else if (fromId != null || toId != null) {
      Relation[] relations = dataSource.getRelations(fromId, toId);
      return template.transform(relations);
    } else {

      Relation[] relations = dataSource.getRelations();
      return template.transform(relations);
    }
  }