Example #1
0
  /**
   * Stores this object in the provided Map. In case of an indexed key use _name to get a
   * corresponding List, and _index for the element in the list. In case of a simple key just put
   * the value at the name key
   *
   * <p>No overrides are allowed.
   *
   * @param map the map to store the value in
   * @param value the value to put in the map
   * @throws PathSegmentSyntaxException if the object is unable to be placed
   */
  public void putOnDataMap(MapMap map, String value) throws PathSegmentSyntaxException {
    // If not an indexed key, just store the value in the provided Map
    // with the current _name as key. If already there - error
    if (_index == null) {
      if (map.get(_name) != null)
        throw new PathSegmentSyntaxException("Duplicate references to key: " + _name);

      map.put(_name, value);
      return;
    }

    // Otherwise, get the element at the key _name, which is assumed to be
    // a Map keyed on Integer (as opposed to String for DataMaps), and store
    // the value at the index key in that map.
    ListMap listMap;
    Object entry = map.get(_name);
    if (entry == null) {
      listMap = new ListMap();
      map.put(_name, listMap);
    } else if (entry instanceof ListMap) {
      listMap = (ListMap) entry;
    } else {
      throw new PathSegmentSyntaxException("Conflicting references to key: " + toString());
    }

    // Now put the value on the _index entry of the list if it's not there.
    // If it is - exception
    if (listMap.get(_index) != null)
      throw new PathSegmentSyntaxException("Duplicate references to key: " + _name);

    listMap.put(_index, value);
  }
Example #2
0
  /**
   * Get the value referenced by this path segment in the provided DataMap
   *
   * <p>The method assumes the current key is not a leaf segment of the key path, i.e. it always
   * references a Map value in the current Map.
   *
   * @param map the Map
   * @return a Map of Strings to Maps
   * @throws PathSegmentSyntaxException if the current key is a leaf segment
   */
  public MapMap getNextLevelMap(MapMap map) throws PathSegmentSyntaxException {
    Object object = map.get(_name);

    if (object == null) {
      MapMap nextLevelDataMap = new MapMap();
      // If not an indexed path segment, create a new Map, put it at the
      // current key and return it.
      if (_index == null) {
        map.put(_name, nextLevelDataMap);
      }
      // If an indexed key, create a new ListMap, put it at the current _name
      // and put next level data map at the current _index in the List Map
      else {
        ListMap listMap = new ListMap();
        map.put(_name, listMap);
        listMap.put(_index, nextLevelDataMap);
      }
      return nextLevelDataMap;
    }

    if (_index == null) {
      if (object instanceof MapMap) return (MapMap) object;
      throw new PathSegmentSyntaxException("Conflicting references to key " + toString());
    }

    if (object instanceof ListMap) {
      ListMap list = (ListMap) object;

      Object object2 = list.get(_index);

      if (object2 == null) {
        MapMap nextLevelMap = new MapMap();
        list.put(_index, nextLevelMap);
        return nextLevelMap;
      } else if (object2 instanceof MapMap) {
        return (MapMap) object2;
      } else {
        throw new PathSegmentSyntaxException("Conflicting references to key " + toString());
      }
    }

    throw new PathSegmentSyntaxException("Conflicting references to key " + toString());
  }
Example #3
0
  /**
   * Assembles a list of DimensionalObjects based on the concrete objects in this
   * BaseAnalyticalObject. Explodes the dx dimension into the in|de|dc|ds concrete objects and
   * returns them as separate DimensionalObjects.
   *
   * <p>Merges fixed and relative periods into the pe dimension, where the RelativePeriods object is
   * represented by enums (e.g. LAST_MONTH). Merges fixed and user organisation units into the ou
   * dimension, where user organisation units properties are represented by enums (e.g.
   * USER_ORG_UNIT).
   *
   * <p>This method is useful when serializing the AnalyticalObject.
   *
   * @param dimension the dimension identifier.
   * @return a list of DimensionalObjects.
   */
  protected List<DimensionalObject> getDimensionalObjectList(String dimension) {
    List<DimensionalObject> objects = new ArrayList<>();

    List<String> categoryDims = getCategoryDims();

    if (DATA_X_DIM_ID.equals(dimension)) {
      if (!indicators.isEmpty()) {
        objects.add(
            new BaseDimensionalObject(INDICATOR_DIM_ID, DimensionType.INDICATOR, indicators));
      }

      if (!dataElements.isEmpty()) {
        objects.add(
            new BaseDimensionalObject(DATAELEMENT_DIM_ID, DimensionType.DATAELEMENT, dataElements));
      }

      if (!dataElementOperands.isEmpty()) {
        objects.add(
            new BaseDimensionalObject(
                DATAELEMENT_OPERAND_ID, DimensionType.DATAELEMENT_OPERAND, dataElementOperands));
      }

      if (!dataSets.isEmpty()) {
        objects.add(new BaseDimensionalObject(DATASET_DIM_ID, DimensionType.DATASET, dataSets));
      }
    } else if (PERIOD_DIM_ID.equals(dimension) && (!periods.isEmpty() || hasRelativePeriods())) {
      List<Period> periodList = new ArrayList<>(periods);

      if (hasRelativePeriods()) {
        List<RelativePeriodEnum> list = relatives.getRelativePeriodEnums();

        for (RelativePeriodEnum periodEnum : list) {
          periodList.add(new ConfigurablePeriod(periodEnum.toString()));
        }
      }

      Collections.sort(periodList, new AscendingPeriodComparator());

      objects.add(new BaseDimensionalObject(dimension, DimensionType.PERIOD, periodList));
    } else if (ORGUNIT_DIM_ID.equals(dimension)
        && (!organisationUnits.isEmpty()
            || !transientOrganisationUnits.isEmpty()
            || hasUserOrgUnit())) {
      List<NameableObject> ouList = new ArrayList<>();
      ouList.addAll(organisationUnits);
      ouList.addAll(transientOrganisationUnits);

      if (userOrganisationUnit) {
        ouList.add(new BaseNameableObject(KEY_USER_ORGUNIT, KEY_USER_ORGUNIT, KEY_USER_ORGUNIT));
      }

      if (userOrganisationUnitChildren) {
        ouList.add(
            new BaseNameableObject(
                KEY_USER_ORGUNIT_CHILDREN, KEY_USER_ORGUNIT_CHILDREN, KEY_USER_ORGUNIT_CHILDREN));
      }

      if (userOrganisationUnitGrandChildren) {
        ouList.add(
            new BaseNameableObject(
                KEY_USER_ORGUNIT_GRANDCHILDREN,
                KEY_USER_ORGUNIT_GRANDCHILDREN,
                KEY_USER_ORGUNIT_GRANDCHILDREN));
      }

      if (organisationUnitLevels != null && !organisationUnitLevels.isEmpty()) {
        for (Integer level : organisationUnitLevels) {
          String id = KEY_LEVEL + level;

          ouList.add(new BaseNameableObject(id, id, id));
        }
      }

      if (itemOrganisationUnitGroups != null && !itemOrganisationUnitGroups.isEmpty()) {
        for (OrganisationUnitGroup group : itemOrganisationUnitGroups) {
          String id = KEY_ORGUNIT_GROUP + group.getUid();

          ouList.add(new BaseNameableObject(id, id, id));
        }
      }

      objects.add(new BaseDimensionalObject(dimension, DimensionType.ORGANISATIONUNIT, ouList));
    } else if (CATEGORYOPTIONCOMBO_DIM_ID.equals(dimension)) {
      objects.add(
          new BaseDimensionalObject(
              dimension, DimensionType.CATEGORY_OPTION_COMBO, new ArrayList<BaseNameableObject>()));
    } else if (categoryDims.contains(dimension)) {
      DataElementCategoryDimension categoryDimension =
          categoryDimensions.get(categoryDims.indexOf(dimension));

      objects.add(
          new BaseDimensionalObject(
              dimension, DimensionType.CATEGORY, categoryDimension.getItems()));
    } else if (DATA_COLLAPSED_DIM_ID.contains(dimension)) {
      objects.add(
          new BaseDimensionalObject(
              dimension, DimensionType.DATA_COLLAPSED, new ArrayList<NameableObject>()));
    } else if (STATIC_DIMS.contains(dimension)) {
      objects.add(
          new BaseDimensionalObject(
              dimension, DimensionType.STATIC, new ArrayList<NameableObject>()));
    } else {
      // Data element group set

      ListMap<String, NameableObject> deGroupMap = new ListMap<>();

      for (DataElementGroup group : dataElementGroups) {
        if (group.getGroupSet() != null) {
          deGroupMap.putValue(group.getGroupSet().getDimension(), group);
        }
      }

      if (deGroupMap.containsKey(dimension)) {
        objects.add(
            new BaseDimensionalObject(
                dimension, DimensionType.DATAELEMENT_GROUPSET, deGroupMap.get(dimension)));
      }

      // Organisation unit group set

      ListMap<String, NameableObject> ouGroupMap = new ListMap<>();

      for (OrganisationUnitGroup group : organisationUnitGroups) {
        if (group.getGroupSet() != null) {
          ouGroupMap.putValue(group.getGroupSet().getUid(), group);
        }
      }

      if (ouGroupMap.containsKey(dimension)) {
        objects.add(
            new BaseDimensionalObject(
                dimension, DimensionType.ORGANISATIONUNIT_GROUPSET, ouGroupMap.get(dimension)));
      }

      // Category option group set

      ListMap<String, NameableObject> coGroupMap = new ListMap<>();

      for (CategoryOptionGroup group : categoryOptionGroups) {
        if (group.getGroupSet() != null) {
          coGroupMap.putValue(group.getGroupSet().getUid(), group);
        }
      }

      if (coGroupMap.containsKey(dimension)) {
        objects.add(
            new BaseDimensionalObject(
                dimension, DimensionType.CATEGORYOPTION_GROUPSET, coGroupMap.get(dimension)));
      }

      // Tracked entity attribute

      Map<String, TrackedEntityAttributeDimension> attributes = new HashMap<>();

      for (TrackedEntityAttributeDimension attribute : attributeDimensions) {
        attributes.put(attribute.getUid(), attribute);
      }

      if (attributes.containsKey(dimension)) {
        TrackedEntityAttributeDimension tead = attributes.get(dimension);

        objects.add(
            new BaseDimensionalObject(
                dimension,
                DimensionType.TRACKED_ENTITY_ATTRIBUTE,
                null,
                tead.getDisplayName(),
                tead.getLegendSet(),
                tead.getFilter()));
      }

      // Tracked entity data element

      Map<String, TrackedEntityDataElementDimension> dataElements = new HashMap<>();

      for (TrackedEntityDataElementDimension dataElement : dataElementDimensions) {
        dataElements.put(dataElement.getUid(), dataElement);
      }

      if (dataElements.containsKey(dimension)) {
        TrackedEntityDataElementDimension tedd = dataElements.get(dimension);

        objects.add(
            new BaseDimensionalObject(
                dimension,
                DimensionType.TRACKED_ENTITY_DATAELEMENT,
                null,
                tedd.getDisplayName(),
                tedd.getLegendSet(),
                tedd.getFilter()));
      }
    }

    return objects;
  }
Example #4
0
  /**
   * Assembles a DimensionalObject based on the persisted properties of this AnalyticalObject.
   * Collapses indicators, data elements, data element operands and data sets into the dx dimension.
   *
   * <p>Collapses fixed and relative periods into the pe dimension. Collapses fixed and user
   * organisation units into the ou dimension.
   *
   * @param dimension the dimension identifier.
   * @param date the date used for generating relative periods.
   * @param user the current user.
   * @param dynamicNames whether to use dynamic or static names.
   * @param format the I18nFormat.
   * @return a DimensionalObject.
   */
  protected DimensionalObject getDimensionalObject(
      String dimension,
      Date date,
      User user,
      boolean dynamicNames,
      List<OrganisationUnit> organisationUnitsAtLevel,
      List<OrganisationUnit> organisationUnitsInGroups,
      I18nFormat format) {
    List<NameableObject> items = new ArrayList<>();

    DimensionType type = null;

    List<String> categoryDims = getCategoryDims();

    if (DATA_X_DIM_ID.equals(dimension)) {
      items.addAll(indicators);
      items.addAll(dataElements);
      items.addAll(dataElementOperands);
      items.addAll(dataSets);

      type = DimensionType.DATA_X;
    } else if (PERIOD_DIM_ID.equals(dimension)) {
      setPeriodNames(periods, dynamicNames, format);

      items.addAll(periods);

      if (hasRelativePeriods()) {
        if (rewindRelativePeriods) {
          items.addAll(relatives.getRewindedRelativePeriods(1, date, format, dynamicNames));
        } else {
          items.addAll(relatives.getRelativePeriods(date, format, dynamicNames));
        }
      }

      type = DimensionType.PERIOD;
    } else if (ORGUNIT_DIM_ID.equals(dimension)) {
      items.addAll(organisationUnits);
      items.addAll(transientOrganisationUnits);

      if (userOrganisationUnit && user != null && user.hasOrganisationUnit()) {
        items.add(user.getOrganisationUnit());
      }

      if (userOrganisationUnitChildren && user != null && user.hasOrganisationUnit()) {
        items.addAll(user.getOrganisationUnit().getSortedChildren());
      }

      if (userOrganisationUnitGrandChildren && user != null && user.hasOrganisationUnit()) {
        items.addAll(user.getOrganisationUnit().getSortedGrandChildren());
      }

      if (organisationUnitLevels != null
          && !organisationUnitLevels.isEmpty()
          && organisationUnitsAtLevel != null) {
        items.addAll(organisationUnitsAtLevel); // Must be set externally
      }

      if (itemOrganisationUnitGroups != null
          && !itemOrganisationUnitGroups.isEmpty()
          && organisationUnitsInGroups != null) {
        items.addAll(organisationUnitsInGroups); // Must be set externally
      }

      type = DimensionType.ORGANISATIONUNIT;
    } else if (CATEGORYOPTIONCOMBO_DIM_ID.equals(dimension)) {
      items.addAll(transientCategoryOptionCombos);

      type = DimensionType.CATEGORY_OPTION_COMBO;
    } else if (categoryDims.contains(dimension)) {
      DataElementCategoryDimension categoryDimension =
          categoryDimensions.get(categoryDims.indexOf(dimension));

      items.addAll(categoryDimension.getItems());

      type = DimensionType.CATEGORY;
    } else if (STATIC_DIMS.contains(dimension)) {
      type = DimensionType.STATIC;
    } else {
      // Data element group set

      ListMap<String, NameableObject> deGroupMap = new ListMap<>();

      for (DataElementGroup group : dataElementGroups) {
        if (group.getGroupSet() != null) {
          deGroupMap.putValue(group.getGroupSet().getDimension(), group);
        }
      }

      if (deGroupMap.containsKey(dimension)) {
        items.addAll(deGroupMap.get(dimension));

        type = DimensionType.DATAELEMENT_GROUPSET;
      }

      // Organisation unit group set

      ListMap<String, NameableObject> ouGroupMap = new ListMap<>();

      for (OrganisationUnitGroup group : organisationUnitGroups) {
        if (group.getGroupSet() != null) {
          ouGroupMap.putValue(group.getGroupSet().getUid(), group);
        }
      }

      if (ouGroupMap.containsKey(dimension)) {
        items.addAll(ouGroupMap.get(dimension));

        type = DimensionType.ORGANISATIONUNIT_GROUPSET;
      }

      // Category option group set

      ListMap<String, NameableObject> coGroupMap = new ListMap<>();

      for (CategoryOptionGroup group : categoryOptionGroups) {
        if (group.getGroupSet() != null) {
          coGroupMap.putValue(group.getGroupSet().getUid(), group);
        }
      }

      if (coGroupMap.containsKey(dimension)) {
        items.addAll(coGroupMap.get(dimension));

        type = DimensionType.CATEGORYOPTION_GROUPSET;
      }

      // Tracked entity attribute

      Map<String, TrackedEntityAttributeDimension> attributes = new HashMap<>();

      for (TrackedEntityAttributeDimension attribute : attributeDimensions) {
        attributes.put(attribute.getUid(), attribute);
      }

      if (attributes.containsKey(dimension)) {
        TrackedEntityAttributeDimension tead = attributes.get(dimension);

        return new BaseDimensionalObject(
            dimension,
            DimensionType.TRACKED_ENTITY_ATTRIBUTE,
            null,
            tead.getDisplayName(),
            tead.getLegendSet(),
            tead.getFilter());
      }

      // Tracked entity data element

      Map<String, TrackedEntityDataElementDimension> dataElements = new HashMap<>();

      for (TrackedEntityDataElementDimension dataElement : dataElementDimensions) {
        dataElements.put(dataElement.getUid(), dataElement);
      }

      if (dataElements.containsKey(dimension)) {
        TrackedEntityDataElementDimension tedd = dataElements.get(dimension);

        return new BaseDimensionalObject(
            dimension,
            DimensionType.TRACKED_ENTITY_DATAELEMENT,
            null,
            tedd.getDisplayName(),
            tedd.getLegendSet(),
            tedd.getFilter());
      }
    }

    IdentifiableObjectUtils.removeDuplicates(items);

    return new BaseDimensionalObject(dimension, type, items);
  }