Example #1
0
  private void updateExistingProfilesForSegment(Segment segment) {
    long t = System.currentTimeMillis();
    Condition segmentCondition = new Condition();

    segmentCondition.setConditionType(
        definitionsService.getConditionType("profilePropertyCondition"));
    segmentCondition.setParameter("propertyName", "segments");
    segmentCondition.setParameter("comparisonOperator", "equals");
    segmentCondition.setParameter("propertyValue", segment.getItemId());

    if (segment.getMetadata().isEnabled()) {
      List<Profile> previousProfiles =
          persistenceService.query(segmentCondition, null, Profile.class);
      List<Profile> newProfiles =
          persistenceService.query(segment.getCondition(), null, Profile.class);

      List<Profile> add = new ArrayList<>(newProfiles);
      add.removeAll(previousProfiles);
      previousProfiles.removeAll(newProfiles);

      for (Profile profileToAdd : add) {
        profileToAdd.getSegments().add(segment.getItemId());
        persistenceService.update(
            profileToAdd.getItemId(), null, Profile.class, "segments", profileToAdd.getSegments());
        Event profileUpdated =
            new Event("profileUpdated", null, profileToAdd, null, null, profileToAdd, new Date());
        profileUpdated.setPersistent(false);
        eventService.send(profileUpdated);
      }
      for (Profile profileToRemove : previousProfiles) {
        profileToRemove.getSegments().remove(segment.getItemId());
        persistenceService.update(
            profileToRemove.getItemId(),
            null,
            Profile.class,
            "segments",
            profileToRemove.getSegments());
        Event profileUpdated =
            new Event(
                "profileUpdated", null, profileToRemove, null, null, profileToRemove, new Date());
        profileUpdated.setPersistent(false);
        eventService.send(profileUpdated);
      }

    } else {
      List<Profile> previousProfiles =
          persistenceService.query(segmentCondition, null, Profile.class);
      for (Profile profileToRemove : previousProfiles) {
        profileToRemove.getSegments().remove(segment.getItemId());
        persistenceService.update(
            profileToRemove.getItemId(),
            null,
            Profile.class,
            "segments",
            profileToRemove.getSegments());
      }
    }
    logger.info("Profiles updated in {}", System.currentTimeMillis() - t);
  }
 private static void readJsonFile() {
   try (JsonReader jsonReader =
       Json.createReader(
           new FileReader(
               Paths.get(System.getProperty("user.dir"), "target/myData.json").toString()))) {
     JsonStructure jsonStructure = jsonReader.read();
     JsonValue.ValueType valueType = jsonStructure.getValueType();
     if (valueType == JsonValue.ValueType.OBJECT) {
       JsonObject jsonObject = (JsonObject) jsonStructure;
       JsonValue firstName = jsonObject.get("firstName");
       LOGGER.info("firstName=" + firstName);
       JsonValue emailAddresses = jsonObject.get("phoneNumbers");
       if (emailAddresses.getValueType() == JsonValue.ValueType.ARRAY) {
         JsonArray jsonArray = (JsonArray) emailAddresses;
         for (int i = 0; i < jsonArray.size(); i++) {
           JsonValue jsonValue = jsonArray.get(i);
           LOGGER.info("emailAddress(" + i + "): " + jsonValue);
         }
       }
     } else {
       LOGGER.warning("First object is not of type " + JsonValue.ValueType.OBJECT);
     }
   } catch (FileNotFoundException e) {
     LOGGER.severe("Failed to open file: " + e.getMessage());
   }
 }
Example #3
0
  public static void main(String[] args) throws IOException {
    String workingDir = System.getProperty("user.dir");
    Path filePath = FileSystems.getDefault().getPath(workingDir + "/resources/Day12");

    JsonReader reader = Json.createReader(Files.newInputStream(filePath));
    JsonStructure jsonst = reader.read();

    System.out.printf("Value is %d\n", navigateTree(jsonst, null));
    System.out.printf("Value is %d\n", navigateTree(jsonst, "red"));
  }
Example #4
0
  private void updateExistingProfilesForScoring(Scoring scoring) {
    long t = System.currentTimeMillis();
    Condition scoringCondition = new Condition();
    scoringCondition.setConditionType(
        definitionsService.getConditionType("profilePropertyCondition"));
    scoringCondition.setParameter("propertyName", "scores." + scoring.getItemId());
    scoringCondition.setParameter("comparisonOperator", "exists");
    List<Profile> previousProfiles =
        persistenceService.query(scoringCondition, null, Profile.class);

    HashMap<String, Object> scriptParams = new HashMap<>();
    scriptParams.put("scoringId", scoring.getItemId());

    for (Profile profileToRemove : previousProfiles) {
      persistenceService.updateWithScript(
          profileToRemove.getItemId(),
          null,
          Profile.class,
          "if (ctx._source.systemProperties.scoreModifiers == null) { ctx._source.systemProperties.scoreModifiers=[:] } ; if (ctx._source.systemProperties.scoreModifiers.containsKey(scoringId)) { ctx._source.scores[scoringId] = ctx._source.systemProperties.scoreModifiers[scoringId] } else { ctx._source.scores.remove(scoringId) }",
          scriptParams);
    }
    if (scoring.getMetadata().isEnabled()) {
      String script =
          "if (ctx._source.scores == null) { ctx._source.scores=[:] } ; if (ctx._source.scores.containsKey(scoringId)) { ctx._source.scores[scoringId] += scoringValue } else { ctx._source.scores[scoringId] = scoringValue }";
      Map<String, Event> updatedProfiles = new HashMap<>();
      for (ScoringElement element : scoring.getElements()) {
        scriptParams.put("scoringValue", element.getValue());
        for (Profile p : persistenceService.query(element.getCondition(), null, Profile.class)) {
          persistenceService.updateWithScript(
              p.getItemId(), null, Profile.class, script, scriptParams);
          Event profileUpdated = new Event("profileUpdated", null, p, null, null, p, new Date());
          profileUpdated.setPersistent(false);
          updatedProfiles.put(p.getItemId(), profileUpdated);
        }
      }
      Iterator<Map.Entry<String, Event>> entries = updatedProfiles.entrySet().iterator();
      while (entries.hasNext()) {
        eventService.send(entries.next().getValue());
      }
    }
    logger.info("Profiles updated in {}", System.currentTimeMillis() - t);
  }
Example #5
0
  private void updateExistingProfilesForPastEventCondition(
      Condition eventCondition, Condition parentCondition) {
    long t = System.currentTimeMillis();
    List<Condition> l = new ArrayList<Condition>();
    Condition andCondition = new Condition();
    andCondition.setConditionType(definitionsService.getConditionType("booleanCondition"));
    andCondition.setParameter("operator", "and");
    andCondition.setParameter("subConditions", l);

    l.add(eventCondition);

    Integer numberOfDays = (Integer) parentCondition.getParameter("numberOfDays");
    if (numberOfDays != null) {
      Condition numberOfDaysCondition = new Condition();
      numberOfDaysCondition.setConditionType(
          definitionsService.getConditionType("sessionPropertyCondition"));
      numberOfDaysCondition.setParameter("propertyName", "timeStamp");
      numberOfDaysCondition.setParameter("comparisonOperator", "greaterThan");
      numberOfDaysCondition.setParameter("propertyValue", "now-" + numberOfDays + "d");
      l.add(numberOfDaysCondition);
    }
    String propertyKey = (String) parentCondition.getParameter("generatedPropertyKey");
    Map<String, Long> res =
        persistenceService.aggregateQuery(
            andCondition, new TermsAggregate("profileId"), Event.ITEM_TYPE);
    for (Map.Entry<String, Long> entry : res.entrySet()) {
      if (!entry.getKey().startsWith("_")) {
        Map<String, Object> p = new HashMap<>();
        p.put(propertyKey, entry.getValue());
        Map<String, Object> p2 = new HashMap<>();
        p2.put("pastEvents", p);
        try {
          persistenceService.update(entry.getKey(), null, Profile.class, "systemProperties", p2);
        } catch (Exception e) {
          logger.error(e.getMessage(), e);
        }
      }
    }

    logger.info("Profiles past condition updated in {}", System.currentTimeMillis() - t);
  }
 @POST
 public JsonObject saveBook(final JsonObject book) {
   long id = System.nanoTime();
   JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
   JsonObject newBook =
       jsonObjectBuilder
           .add("bookId", id)
           .add("bookName", book.get("bookName"))
           .add("publisher", book.get("publisher"))
           .build();
   BookResource.memoryBase.put(id, newBook);
   return newBook;
 }
Example #7
0
  private void updateExistingProfilesForRemovedScoring(String scoringId) {
    long t = System.currentTimeMillis();
    Condition scoringCondition = new Condition();
    scoringCondition.setConditionType(
        definitionsService.getConditionType("profilePropertyCondition"));
    scoringCondition.setParameter("propertyName", "scores." + scoringId);
    scoringCondition.setParameter("comparisonOperator", "exists");
    List<Profile> previousProfiles =
        persistenceService.query(scoringCondition, null, Profile.class);

    HashMap<String, Object> scriptParams = new HashMap<>();
    scriptParams.put("scoringId", scoringId);

    for (Profile profileToRemove : previousProfiles) {
      persistenceService.updateWithScript(
          profileToRemove.getItemId(),
          null,
          Profile.class,
          "ctx._source.scores.remove(scoringId)",
          scriptParams);
    }
    logger.info("Profiles updated in {}", System.currentTimeMillis() - t);
  }
 private static void createJsonFile() {
   JsonObject model =
       Json.createObjectBuilder()
           .add("firstName", "Martin")
           .add(
               "phoneNumbers",
               Json.createArrayBuilder()
                   .add(Json.createObjectBuilder().add("mobile", "1234 56789"))
                   .add(Json.createObjectBuilder().add("home", "2345 67890")))
           .build();
   try (JsonWriter jsonWriter =
       Json.createWriter(
           new FileWriter(
               Paths.get(System.getProperty("user.dir"), "target/myData.json").toString()))) {
     jsonWriter.write(model);
   } catch (IOException e) {
     LOGGER.severe("Failed to create file: " + e.getMessage());
   }
 }