Beispiel #1
0
  public static void saveGroup(String groupId, String stype, HashMap hashMap) throws Exception {

    EntityCondition whereCondition =
        EntityCondition.makeCondition(
            UtilMisc.toList(
                EntityCondition.makeCondition("id", EntityOperator.EQUALS, groupId),
                EntityCondition.makeCondition("stype", EntityOperator.EQUALS, stype)),
            EntityOperator.AND);

    List<GenericValue> groups =
        getDelegator()
            .findList("SvGroup", whereCondition, null, UtilMisc.toList("idx"), null, false);
    if (!(groups.size() > 0)) {
      GenericValue group = getDelegator().makeValue("SvGroup");
      group.set("id", groupId);
      String[] splits = groupId.split("\\.");
      if (splits.length > 1) {
        group.set("parent", splits[0]);
      }
      group.set("stype", stype);
      group.create();
    }

    whereCondition =
        EntityCondition.makeCondition(
            UtilMisc.toList(
                EntityCondition.makeCondition("groupId", EntityOperator.EQUALS, groupId),
                EntityCondition.makeCondition("stype", EntityOperator.EQUALS, stype)),
            EntityOperator.AND);
    List<GenericValue> groupValue =
        getDelegator()
            .findList("SvGroupValue", whereCondition, null, UtilMisc.toList("idx"), null, false);
    long maxIdx = 0;
    try {
      int index = groupValue.size() - 1;
      GenericValue val = groupValue.get(index);
      maxIdx = val.getInteger("idx");
    } catch (Exception e) {
    }

    for (GenericValue val : groupValue) {
      String key = val.getString("attrName");
      if (key == null) continue;
      boolean bExist = false;
      Enumeration<?> enumKeys = hashMap.keys();
      while (enumKeys.hasMoreElements()) {
        Object obj = enumKeys.nextElement();
        String keyi = null;
        if (obj instanceof StringProperty) {
          keyi = ((StringProperty) obj).getName();
        } else {
          keyi = (String) obj;
        }
        if (key.equals(keyi)) {
          bExist = true;
        }
      }
      if (!bExist) {
        val.remove();
      }
    }

    Enumeration<?> enumeration = hashMap.keys();
    while (enumeration.hasMoreElements()) {
      Object obj = enumeration.nextElement();
      String key = null;
      if (obj instanceof StringProperty) {
        key = ((StringProperty) obj).getName();
      } else {
        key = (String) obj;
      }
      if (key == null) continue;

      String value = null;
      if (obj instanceof StringProperty) {
        value = ((StringProperty) obj).getValue();
      } else if (obj instanceof Array) {

      } else if (obj instanceof String) {
        value = "" + hashMap.get(key);
      }
      value = "".equals(value) ? null : value;
      boolean bExist = false;

      for (GenericValue val : groupValue) {
        if (key.equals(val.getString("attrName"))) {
          if (value == null) {
            val.remove();
          } else {
            val.set("attrValue", value);
            val.store();
          }
          bExist = true;
        }
      }
      if (!bExist && value != null) {
        GenericValue val = getDelegator().makeValue("SvGroupValue");
        val.set("groupId", groupId);
        val.set("stype", stype);
        maxIdx++;
        val.set("idx", maxIdx);
        val.set("attrName", key);
        val.set("attrValue", value);
        val.create();
      }
    }
  }