Пример #1
0
  public Element serviceSpecificExec(Element params, ServiceContext context) throws Exception {
    GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
    DataManager dm = gc.getBean(DataManager.class);
    Dbms dbms = (Dbms) context.getResourceManager().open(Geonet.Res.MAIN_DB);

    String child = Util.getParam(params, Params.CHILD, "n");
    String isTemplate = Util.getParam(params, Params.TEMPLATE, "n");
    String id = "";
    String uuid = "";
    boolean haveAllRights = Boolean.valueOf(Util.getParam(params, Params.FULL_PRIVILEGES, "false"));

    // does the request contain a UUID ?
    try {
      uuid = Util.getParam(params, Params.UUID);
      // lookup ID by UUID
      id = dm.getMetadataId(dbms, uuid);
    } catch (BadInputEx x) {
      try {
        id = Util.getParam(params, Params.ID);
        uuid = dm.getMetadataUuid(dbms, id);
      }
      // request does not contain ID
      catch (BadInputEx xx) {
        // give up
        throw new Exception("Request must contain a UUID or an ID");
      }
    }

    String groupOwner = Util.getParam(params, Params.GROUP);

    // TODO : Check user can create a metadata in that group
    UserSession user = context.getUserSession();
    if (!user.getProfile().equals(Geonet.Profile.ADMINISTRATOR)) {
      String selectGroupIdQuery =
          "SELECT groupId FROM UserGroups WHERE profile='Editor' AND userId=? AND groupId=?";
      @SuppressWarnings("unchecked")
      java.util.List<Element> list =
          dbms.select(
                  selectGroupIdQuery,
                  Integer.valueOf(user.getUserId()),
                  Integer.valueOf(groupOwner))
              .getChildren();
      if (list.size() == 0) {
        throw new ServiceNotAllowedEx(
            "Service not allowed. User needs to be Editor in group with id " + groupOwner);
      }
    }

    // --- query the data manager

    String newId =
        dm.createMetadata(
            context,
            dbms,
            id,
            groupOwner,
            context.getSerialFactory(),
            gc.getSiteId(),
            context.getUserSession().getUserIdAsInt(),
            (child.equals("n") ? null : uuid),
            isTemplate,
            haveAllRights);

    Element response = new Element(Jeeves.Elem.RESPONSE);
    response.addContent(new Element(Geonet.Elem.JUSTCREATED).setText("true"));

    String sessionTabProperty =
        useEditTab ? Geonet.Session.METADATA_EDITING_TAB : Geonet.Session.METADATA_SHOW;

    // Set current tab for new editing session if defined.
    Element elCurrTab = params.getChild(Params.CURRTAB);
    if (elCurrTab != null) {
      context.getUserSession().setProperty(sessionTabProperty, elCurrTab.getText());
    }
    response.addContent(new Element(Geonet.Elem.ID).setText(newId));
    return response;
  }