Ejemplo n.º 1
0
 public Segment getSegmentDefinition(String segmentId) {
   Segment definition = persistenceService.load(segmentId, Segment.class);
   if (definition != null) {
     ParserHelper.resolveConditionType(definitionsService, definition.getCondition());
   }
   return definition;
 }
Ejemplo n.º 2
0
 private List<Segment> getAllSegmentDefinitions() {
   List<Segment> allItems = persistenceService.getAllItems(Segment.class);
   for (Segment segment : allItems) {
     ParserHelper.resolveConditionType(definitionsService, segment.getCondition());
   }
   return allItems;
 }
Ejemplo n.º 3
0
 private Set<Segment> getScoringDependentSegments(String scoringId) {
   Set<Segment> impactedSegments = new HashSet<>(this.allSegments.size());
   for (Segment segment : this.allSegments) {
     if (checkScoringDeletionImpact(segment.getCondition(), scoringId)) {
       impactedSegments.add(segment);
     }
   }
   return impactedSegments;
 }
Ejemplo n.º 4
0
 public DependentMetadata getScoringDependentMetadata(String scoringId) {
   List<Metadata> segments = new LinkedList<>();
   List<Metadata> scorings = new LinkedList<>();
   for (Segment definition : getScoringDependentSegments(scoringId)) {
     segments.add(definition.getMetadata());
   }
   for (Scoring definition : getScoringDependentScorings(scoringId)) {
     scorings.add(definition.getMetadata());
   }
   return new DependentMetadata(segments, scorings);
 }
Ejemplo n.º 5
0
  public void setSegmentDefinition(Segment segment) {
    ParserHelper.resolveConditionType(definitionsService, segment.getCondition());
    if (segment.getMetadata().isEnabled() && !segment.getMetadata().isMissingPlugins()) {
      updateAutoGeneratedRules(segment.getMetadata(), segment.getCondition());
    }
    // make sure we update the name and description metadata that might not match, so first we
    // remove the entry from the map
    persistenceService.save(segment);

    updateExistingProfilesForSegment(segment);
  }
Ejemplo n.º 6
0
 public PartialList<Metadata> getSegmentMetadatas(
     String scope, int offset, int size, String sortBy) {
   PartialList<Segment> segments =
       persistenceService.query("metadata.scope", scope, sortBy, Segment.class, offset, size);
   List<Metadata> details = new LinkedList<>();
   for (Segment definition : segments.getList()) {
     details.add(definition.getMetadata());
   }
   return new PartialList<>(
       details, segments.getOffset(), segments.getPageSize(), segments.getTotalSize());
 }
Ejemplo n.º 7
0
  public List<Metadata> getSegmentMetadatasForProfile(Profile profile) {
    List<Metadata> metadatas = new ArrayList<>();

    List<Segment> allSegments = this.allSegments;
    for (Segment segment : allSegments) {
      if (persistenceService.testMatch(segment.getCondition(), profile)) {
        metadatas.add(segment.getMetadata());
      }
    }

    return metadatas;
  }
Ejemplo n.º 8
0
  public DependentMetadata removeScoringDefinition(String scoringId, boolean validate) {
    Set<Segment> impactedSegments = getScoringDependentSegments(scoringId);
    Set<Scoring> impactedScorings = getScoringDependentScorings(scoringId);
    if (!validate || (impactedSegments.isEmpty() && impactedScorings.isEmpty())) {
      // update profiles
      updateExistingProfilesForRemovedScoring(scoringId);

      // update impacted segments
      for (Segment segment : impactedSegments) {
        Condition updatedCondition =
            updateScoringDependentCondition(segment.getCondition(), scoringId);
        segment.setCondition(updatedCondition);
        if (updatedCondition == null) {
          clearAutoGeneratedRules(
              persistenceService.query(
                  "linkedItems", segment.getMetadata().getId(), null, Rule.class),
              segment.getMetadata().getId());
          segment.getMetadata().setEnabled(false);
        }
        setSegmentDefinition(segment);
      }

      // update impacted scorings
      for (Scoring scoring : impactedScorings) {
        List<ScoringElement> updatedScoringElements = new ArrayList<>();
        for (ScoringElement scoringElement : scoring.getElements()) {
          Condition updatedCondition =
              updateScoringDependentCondition(scoringElement.getCondition(), scoringId);
          if (updatedCondition != null) {
            scoringElement.setCondition(updatedCondition);
            updatedScoringElements.add(scoringElement);
          }
        }
        scoring.setElements(updatedScoringElements);
        if (updatedScoringElements.isEmpty()) {
          clearAutoGeneratedRules(
              persistenceService.query(
                  "linkedItems", scoring.getMetadata().getId(), null, Rule.class),
              scoring.getMetadata().getId());
          scoring.getMetadata().setEnabled(false);
        }
        setScoringDefinition(scoring);
      }

      persistenceService.remove(scoringId, Scoring.class);
      List<Rule> previousRules =
          persistenceService.query("linkedItems", scoringId, null, Rule.class);
      clearAutoGeneratedRules(previousRules, scoringId);
    }

    List<Metadata> segments = new LinkedList<>();
    List<Metadata> scorings = new LinkedList<>();
    for (Segment definition : impactedSegments) {
      segments.add(definition.getMetadata());
    }
    for (Scoring definition : impactedScorings) {
      scorings.add(definition.getMetadata());
    }
    return new DependentMetadata(segments, scorings);
  }
Ejemplo n.º 9
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);
  }
Ejemplo n.º 10
0
  public SegmentsAndScores getSegmentsAndScoresForProfile(Profile profile) {
    Set<String> segments = new HashSet<String>();
    Map<String, Integer> scores = new HashMap<String, Integer>();

    List<Segment> allSegments = this.allSegments;
    for (Segment segment : allSegments) {
      if (persistenceService.testMatch(segment.getCondition(), profile)) {
        segments.add(segment.getMetadata().getId());
      }
    }

    List<Scoring> allScoring = this.allScoring;
    Map<String, Integer> scoreModifiers =
        (Map<String, Integer>) profile.getSystemProperties().get("scoreModifiers");
    for (Scoring scoring : allScoring) {
      if (scoring.getMetadata().isEnabled()) {
        int score = 0;
        for (ScoringElement scoringElement : scoring.getElements()) {
          if (persistenceService.testMatch(scoringElement.getCondition(), profile)) {
            score += scoringElement.getValue();
          }
        }
        String scoringId = scoring.getMetadata().getId();
        if (scoreModifiers != null
            && scoreModifiers.containsKey(scoringId)
            && scoreModifiers.get(scoringId) != null) {
          score += scoreModifiers.get(scoringId);
        }
        if (score > 0) {
          scores.put(scoringId, score);
        }
      }
    }

    return new SegmentsAndScores(segments, scores);
  }
Ejemplo n.º 11
0
  private void loadPredefinedSegments(BundleContext bundleContext) {
    Enumeration<URL> predefinedSegmentEntries =
        bundleContext.getBundle().findEntries("META-INF/cxs/segments", "*.json", true);
    if (predefinedSegmentEntries == null) {
      return;
    }
    while (predefinedSegmentEntries.hasMoreElements()) {
      URL predefinedSegmentURL = predefinedSegmentEntries.nextElement();
      logger.debug("Found predefined segment at " + predefinedSegmentURL + ", loading... ");

      try {
        Segment segment =
            CustomObjectMapper.getObjectMapper().readValue(predefinedSegmentURL, Segment.class);
        if (segment.getMetadata().getScope() == null) {
          segment.getMetadata().setScope("systemscope");
        }
        if (getSegmentDefinition(segment.getMetadata().getId()) == null) {
          setSegmentDefinition(segment);
        }
      } catch (IOException e) {
        logger.error("Error while loading segment definition " + predefinedSegmentURL, e);
      }
    }
  }
Ejemplo n.º 12
0
  public DependentMetadata removeSegmentDefinition(String segmentId, boolean validate) {
    Set<Segment> impactedSegments = getSegmentDependentSegments(segmentId);
    Set<Scoring> impactedScorings = getSegmentDependentScorings(segmentId);
    if (!validate || (impactedSegments.isEmpty() && impactedScorings.isEmpty())) {
      // update profiles
      Condition segmentCondition = new Condition();
      segmentCondition.setConditionType(
          definitionsService.getConditionType("profilePropertyCondition"));
      segmentCondition.setParameter("propertyName", "segments");
      segmentCondition.setParameter("comparisonOperator", "equals");
      segmentCondition.setParameter("propertyValue", segmentId);

      List<Profile> previousProfiles =
          persistenceService.query(segmentCondition, null, Profile.class);
      for (Profile profileToRemove : previousProfiles) {
        profileToRemove.getSegments().remove(segmentId);
        persistenceService.update(
            profileToRemove.getItemId(),
            null,
            Profile.class,
            "segments",
            profileToRemove.getSegments());
      }

      // update impacted segments
      for (Segment segment : impactedSegments) {
        Condition updatedCondition =
            updateSegmentDependentCondition(segment.getCondition(), segmentId);
        segment.setCondition(updatedCondition);
        if (updatedCondition == null) {
          clearAutoGeneratedRules(
              persistenceService.query(
                  "linkedItems", segment.getMetadata().getId(), null, Rule.class),
              segment.getMetadata().getId());
          segment.getMetadata().setEnabled(false);
        }
        setSegmentDefinition(segment);
      }

      // update impacted scorings
      for (Scoring scoring : impactedScorings) {
        List<ScoringElement> updatedScoringElements = new ArrayList<>();
        for (ScoringElement scoringElement : scoring.getElements()) {
          Condition updatedCondition =
              updateSegmentDependentCondition(scoringElement.getCondition(), segmentId);
          if (updatedCondition != null) {
            scoringElement.setCondition(updatedCondition);
            updatedScoringElements.add(scoringElement);
          }
        }
        scoring.setElements(updatedScoringElements);
        if (updatedScoringElements.isEmpty()) {
          clearAutoGeneratedRules(
              persistenceService.query(
                  "linkedItems", scoring.getMetadata().getId(), null, Rule.class),
              scoring.getMetadata().getId());
          scoring.getMetadata().setEnabled(false);
        }
        setScoringDefinition(scoring);
      }

      persistenceService.remove(segmentId, Segment.class);
      List<Rule> previousRules =
          persistenceService.query("linkedItems", segmentId, null, Rule.class);
      clearAutoGeneratedRules(previousRules, segmentId);
    }

    List<Metadata> segments = new LinkedList<>();
    List<Metadata> scorings = new LinkedList<>();
    for (Segment definition : impactedSegments) {
      segments.add(definition.getMetadata());
    }
    for (Scoring definition : impactedScorings) {
      scorings.add(definition.getMetadata());
    }
    return new DependentMetadata(segments, scorings);
  }