예제 #1
0
 private static Group createGroup(Map<String, String> beanAttr) {
   if (beanAttr == null) return null;
   Group group = new Group();
   if (beanAttr.get("parentGroupId").equals("\\0")) group.setParentGroupId(null);
   else group.setParentGroupId(Integer.valueOf(beanAttr.get("parentGroupId")));
   group.setId(Integer.valueOf(beanAttr.get("id")).intValue());
   group.setName(BeansUtils.eraseEscaping(beanAttr.get("name")));
   group.setDescription(BeansUtils.eraseEscaping(beanAttr.get("description")));
   group.setVoId(Integer.valueOf(beanAttr.get("voId")));
   return group;
 }
예제 #2
0
 public Group mapRow(ResultSet rs, int i) throws SQLException {
   Group g = new Group();
   g.setId(rs.getInt("groups_id"));
   // ParentGroup with ID=0 is not supported
   if (rs.getInt("groups_parent_group_id") != 0)
     g.setParentGroupId(rs.getInt("groups_parent_group_id"));
   else g.setParentGroupId(null);
   g.setName(rs.getString("groups_name"));
   g.setShortName(g.getName().substring(g.getName().lastIndexOf(":") + 1));
   g.setDescription(rs.getString("groups_dsc"));
   g.setVoId(rs.getInt("groups_vo_id"));
   g.setCreatedAt(rs.getString("groups_created_at"));
   g.setCreatedBy(rs.getString("groups_created_by"));
   g.setModifiedAt(rs.getString("groups_modified_at"));
   g.setModifiedBy(rs.getString("groups_modified_by"));
   if (rs.getInt("groups_modified_by_uid") == 0) g.setModifiedByUid(null);
   else g.setModifiedByUid(rs.getInt("groups_modified_by_uid"));
   if (rs.getInt("groups_created_by_uid") == 0) g.setCreatedByUid(null);
   else g.setCreatedByUid(rs.getInt("groups_created_by_uid"));
   return g;
 }
예제 #3
0
  public Group createGroup(PerunSession sess, Vo vo, Group group)
      throws GroupExistsException, InternalErrorException {
    Utils.notNull(group, "group");
    Utils.notNull(group.getName(), "group.getName()");

    // Check if the group already exists
    if (group.getParentGroupId() == null) {
      if (1
          == jdbc.queryForInt(
              "select count('x') from groups where lower(name)=lower(?) and vo_id=? and parent_group_id IS NULL",
              group.getName(),
              vo.getId())) {
        throw new GroupExistsException(
            "Group ["
                + group.getName()
                + "] already exists under VO ["
                + vo.getShortName()
                + "] and has parent Group with id is [NULL]");
      }
    } else {
      if (1
          == jdbc.queryForInt(
              "select count('x') from groups where lower(name)=lower(?) and vo_id=? and parent_group_id=?",
              group.getName(),
              vo.getId(),
              group.getParentGroupId())) {
        throw new GroupExistsException(
            "Group ["
                + group.getName()
                + "] already exists under VO ["
                + vo.getShortName()
                + "] and has parent Group with id ["
                + group.getParentGroupId()
                + "]");
      }
    }

    // Check the group name, it can contain only a-Z0-9_- and space
    if (!group.getShortName().matches("^[- a-zA-Z.0-9_]+$")) {
      throw new InternalErrorException(
          new IllegalArgumentException(
              "Wrong group name, group name can contain only a-Z0-9.-_: and space characters. "
                  + group));
    }

    try {
      // Store the group into the DB
      int newId = Utils.getNewId(jdbc, "groups_id_seq");

      jdbc.update(
          "insert into groups (id, parent_group_id, name, dsc, vo_id, created_by,created_at,modified_by,modified_at,created_by_uid,modified_by_uid) "
              + "values (?,?,?,?,?,?,"
              + Compatibility.getSysdate()
              + ",?,"
              + Compatibility.getSysdate()
              + ",?,?)",
          newId,
          group.getParentGroupId(),
          group.getName(),
          group.getDescription(),
          vo.getId(),
          sess.getPerunPrincipal().getActor(),
          sess.getPerunPrincipal().getActor(),
          sess.getPerunPrincipal().getUserId(),
          sess.getPerunPrincipal().getUserId());
      group.setId(newId);

      group.setVoId(vo.getId());

      return group;
    } catch (RuntimeException err) {
      throw new InternalErrorException(err);
    }
  }