public Element exec(Element params, ServiceContext context) throws Exception {
    Element response = new Element("response");

    Dbms dbms = (Dbms) context.getResourceManager().open(Geonet.Res.MAIN_DB);

    // --- check access
    GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
    DataManager dm = gc.getDataManager();

    boolean addEdit = false;

    // --- the request should contain an ID or UUID
    String id = Utils.getIdentifierFromParameters(params, context);

    if (id == null) {
      throw new MetadataNotFoundEx("No record has this UUID");
    }

    // --- check download access
    Lib.resource.checkPrivilege(context, id, AccessManager.OPER_DOWNLOAD);

    // --- get metadata
    boolean withValidationErrors = false, keepXlinkAttributes = false;
    Element elMd =
        gc.getDataManager()
            .getMetadata(context, id, addEdit, withValidationErrors, keepXlinkAttributes);

    if (elMd == null) throw new MetadataNotFoundEx("Metadata not found - deleted?");

    response.addContent(new Element("id").setText(id));

    // --- transform record into brief version
    String briefXslt = appPath + "xsl/metadata-brief.xsl";
    Element elBrief = Xml.transform(elMd, briefXslt);

    XPath xp;
    List elems;

    // --- process links to a file (have name field not blank)
    // --- if they are a reference to a downloadable local file then get size
    // --- and date modified, if not then set local to false
    xp = XPath.newInstance("link[starts-with(@protocol,'WWW:DOWNLOAD') and @name!='']");
    elems = xp.selectNodes(elBrief);
    response = processDownloadLinks(context, id, dm.getSiteURL(), elems, response);

    // --- now process web links so that they can be displayed as well
    xp = XPath.newInstance("link[starts-with(@protocol,'WWW:LINK')]");
    elems = xp.selectNodes(elBrief);
    response = processWebLinks(elems, response);

    return response;
  }
Beispiel #2
0
 public Harvester(Logger log, ServiceContext context, Dbms dbms, Z3950Params params) {
   GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
   this.context = context;
   this.log = log;
   this.searchMan = gc.getSearchmanager();
   this.dataMan = gc.getDataManager();
   this.settingMan = gc.getSettingManager();
   this.context = context;
   this.dbms = dbms;
   this.params = params;
 }
Beispiel #3
0
  public Aligner(Logger log, ServiceContext sc, Dbms dbms, CswServer server, CswParams params)
      throws OperationAbortedEx {
    this.log = log;
    this.context = sc;
    this.dbms = dbms;
    this.params = params;

    GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
    dataMan = gc.getDataManager();
    result = new HarvestResult();

    // --- setup get-record-by-id request

    request = new GetRecordByIdRequest(sc);
    request.setElementSetName(ElementSetName.FULL);

    CswOperation oper = server.getOperation(CswServer.GET_RECORD_BY_ID);

    // Use the preferred HTTP method and check one exist.
    if (oper.getUrl != null && Harvester.PREFERRED_HTTP_METHOD.equals("GET")) {
      request.setUrl(oper.getUrl);
      request.setMethod(CatalogRequest.Method.GET);
    } else if (oper.postUrl != null && Harvester.PREFERRED_HTTP_METHOD.equals("POST")) {
      request.setUrl(oper.postUrl);
      request.setMethod(CatalogRequest.Method.POST);
    } else {
      if (oper.getUrl != null) {
        request.setUrl(oper.getUrl);
        request.setMethod(CatalogRequest.Method.GET);
      } else if (oper.postUrl != null) {
        request.setUrl(oper.postUrl);
        request.setMethod(CatalogRequest.Method.POST);
      } else {
        throw new OperationAbortedEx("No GET or POST DCP available in this service.");
      }
    }

    if (oper.preferredOutputSchema != null) {
      request.setOutputSchema(oper.preferredOutputSchema);
    }

    if (oper.preferredServerVersion != null) {
      request.setServerVersion(oper.preferredServerVersion);
    }

    if (params.useAccount) {
      request.setCredentials(params.username, params.password);
    }
  }
  public Element serviceSpecificExec(Element params, ServiceContext context) throws Exception {
    String parentUuid = Util.getParam(params, "parentUuid");
    String childrenIds = Util.getParam(params, "childrenIds");

    // Transform params element into Map<String, String> for xsl transformation
    @SuppressWarnings("unchecked")
    List<Element> lstParams = params.getChildren();
    Map<String, String> parameters = new HashMap<String, String>();
    for (Element param : lstParams) {
      parameters.put(param.getName(), param.getTextTrim());
    }

    // Handle children IDs.
    String[] children = childrenIds.split(",");

    // Update children
    GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
    DataManager dm = gc.getDataManager();

    Set<String> untreatedChildren = dm.updateChildren(context, parentUuid, children, parameters);

    Element response = new Element(Jeeves.Elem.RESPONSE);
    int treatedChildren = children.length;
    StringBuilder untreatedReport = new StringBuilder();
    if (untreatedChildren.size() != 0) {
      treatedChildren = children.length - untreatedChildren.size();
      untreatedReport.setLength(0);
      untreatedReport.append(untreatedChildren.size()).append(" child/children not updated");
      for (String id : untreatedChildren) untreatedReport.append(", ").append(id);
    }

    String report =
        treatedChildren
            + " child/children updated for metadata "
            + parentUuid
            + ". "
            + untreatedReport;

    return response.setText(report);
  }
  public Aligner(
      Logger log,
      ServiceContext context,
      Dbms dbms,
      XmlRequest req,
      GeonetParams params,
      Element remoteInfo) {
    this.log = log;
    this.context = context;
    this.dbms = dbms;
    this.request = req;
    this.params = params;

    GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
    dataMan = gc.getDataManager();
    result = new GeonetResult();

    // --- save remote categories and groups into hashmaps for a fast access

    List list = remoteInfo.getChild("groups").getChildren("group");
    setupLocEntity(list, hmRemoteGroups);
  }
Beispiel #6
0
  public Element serviceSpecificExec(Element params, ServiceContext context) throws Exception {

    GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
    DataManager dataMan = gc.getDataManager();

    Dbms dbms = (Dbms) context.getResourceManager().open(Geonet.Res.MAIN_DB);

    UserSession session = context.getUserSession();

    String id = Utils.getIdentifierFromParameters(params, context);

    // --- validate metadata from session
    Element errorReport =
        new AjaxEditUtils(context)
            .validateMetadataEmbedded(session, dbms, id, context.getLanguage());

    // --- update element and return status
    Element elResp = new Element(Jeeves.Elem.RESPONSE);
    elResp.addContent(new Element(Geonet.Elem.ID).setText(id));
    elResp.addContent(new Element("schema").setText(dataMan.getMetadataSchema(dbms, id)));
    elResp.addContent(errorReport);

    return elResp;
  }
  /**
   * @param request
   * @param context
   * @return
   * @throws Exception
   */
  private int deleteTransaction(Element request, ServiceContext context) throws Exception {
    int deleted = 0;

    GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
    DataManager dataMan = gc.getDataManager();

    if (context.getUserSession().getUserId() == null)
      throw new NoApplicableCodeEx("User not authenticated.");

    // first, search the record in the database to get the record id
    Element constr = request.getChild("Constraint", Csw.NAMESPACE_CSW);
    List<Element> results = getResultsFromConstraints(context, constr);

    // second, delete the metadata in the dbms using the id
    Iterator<Element> i = results.iterator();
    if (!i.hasNext()) return deleted;

    Dbms dbms = (Dbms) context.getResourceManager().open(Geonet.Res.MAIN_DB);

    // delete all matching records
    while (i.hasNext()) {
      Element result = i.next();
      String uuid = result.getChildText("identifier", Csw.NAMESPACE_DC);
      String id = dataMan.getMetadataId(dbms, uuid);

      if (id == null) return deleted;

      if (!dataMan.getAccessManager().canEdit(context, id))
        throw new NoApplicableCodeEx("User not allowed to delete metadata : " + id);

      dataMan.deleteMetadata(context, dbms, id);
      deleted++;
    }

    return deleted;
  }
Beispiel #8
0
  public Element serviceSpecificExec(Element params, ServiceContext context) throws Exception {
    AjaxEditUtils ajaxEditUtils = new AjaxEditUtils(context);
    ajaxEditUtils.preprocessUpdate(params, context);

    GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
    DataManager dataMan = gc.getDataManager();
    UserSession session = context.getUserSession();

    Dbms dbms = (Dbms) context.getResourceManager().open(Geonet.Res.MAIN_DB);

    String id = Utils.getIdentifierFromParameters(params, context);
    String version = Util.getParam(params, Params.VERSION);
    String isTemplate = Util.getParam(params, Params.TEMPLATE, "n");
    String showValidationErrors = Util.getParam(params, Params.SHOWVALIDATIONERRORS, "false");
    String title = params.getChildText(Params.TITLE);
    String data = params.getChildText(Params.DATA);
    String minor = Util.getParam(params, Params.MINOREDIT, "false");

    boolean finished = config.getValue(Params.FINISHED, "no").equals("yes");
    boolean forget = config.getValue(Params.FORGET, "no").equals("yes");

    if (!forget) {
      int iLocalId = Integer.parseInt(id);
      dataMan.setTemplateExt(dbms, iLocalId, isTemplate, title);

      // --- use StatusActionsFactory and StatusActions class to possibly
      // --- change status as a result of this edit (use onEdit method)
      StatusActionsFactory saf = new StatusActionsFactory(gc.getStatusActionsClass());
      StatusActions sa = saf.createStatusActions(context, dbms);
      saf.onEdit(sa, iLocalId, minor.equals("true"));

      if (data != null) {
        Element md = Xml.loadString(data, false);

        String changeDate = null;
        boolean validate = showValidationErrors.equals("true");
        boolean updateDateStamp = !minor.equals("true");
        boolean ufo = true;
        boolean index = true;
        if (!dataMan.updateMetadata(
            context,
            dbms,
            id,
            md,
            validate,
            ufo,
            index,
            context.getLanguage(),
            changeDate,
            updateDateStamp)) {
          throw new ConcurrentUpdateEx(id);
        }
      } else {
        ajaxEditUtils.updateContent(params, false, true);
      }
    }

    // -----------------------------------------------------------------------
    // --- update element and return status

    Element elResp = new Element(Jeeves.Elem.RESPONSE);
    elResp.addContent(new Element(Geonet.Elem.ID).setText(id));
    elResp.addContent(new Element(Geonet.Elem.SHOWVALIDATIONERRORS).setText(showValidationErrors));
    boolean justCreated = Util.getParam(params, Params.JUST_CREATED, null) != null;
    if (justCreated) {
      elResp.addContent(new Element(Geonet.Elem.JUSTCREATED).setText("true"));
    }
    elResp.addContent(new Element(Params.MINOREDIT).setText(minor));

    // --- if finished then remove the XML from the session
    if (finished) {
      ajaxEditUtils.removeMetadataEmbedded(session, id);
    }

    return elResp;
  }
Beispiel #9
0
  public Element exec(Element params, ServiceContext context) throws Exception {
    UserSession session = context.getUserSession();

    boolean witholdWithheldElements = Util.getParam(params, "hide_withheld", false);
    if (witholdWithheldElements) {
      XmlSerializer.getThreadLocal(true).setForceHideWithheld(witholdWithheldElements);
    }

    // -----------------------------------------------------------------------
    // --- handle current tab

    Element elCurrTab = params.getChild(Params.CURRTAB);

    if (elCurrTab != null) session.setProperty(Geonet.Session.METADATA_SHOW, elCurrTab.getText());

    // -----------------------------------------------------------------------
    // --- check access

    GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
    DataManager dm = gc.getDataManager();

    String id = Utils.getIdentifierFromParameters(params, context);

    if (!skipPopularity) { // skipPopularity could be a URL param as well
      String skip = Util.getParam(params, "skipPopularity", "n");
      skipPopularity = skip.equals("y");
    }

    if (id == null) throw new MetadataNotFoundEx("Metadata not found.");

    Lib.resource.checkPrivilege(context, id, AccessManager.OPER_VIEW);

    // -----------------------------------------------------------------------
    // --- get metadata

    Element elMd;
    boolean addEditing = false;
    if (!skipInfo) {
      boolean withValidationErrors = false, keepXlinkAttributes = false;
      elMd = dm.getMetadata(context, id, addEditing, withValidationErrors, keepXlinkAttributes);
    } else {
      elMd = dm.getMetadataNoInfo(context, id);
    }

    if (elMd == null) throw new MetadataNotFoundEx(id);

    if (addRefs) { // metadata.show for GeoNetwork needs geonet:element
      elMd = dm.enumerateTree(elMd);
    }

    //
    // setting schemaLocation
    // TODO currently it's only set for ISO metadata - this should all move
    // to
    // the updatefixedinfo.xsl for each schema

    // do not set schemaLocation if it is already there
    if (elMd.getAttribute("schemaLocation", Csw.NAMESPACE_XSI) == null) {
      Namespace gmdNs = elMd.getNamespace("gmd");
      // document has ISO root element and ISO namespace
      if (gmdNs != null && gmdNs.getURI().equals("http://www.isotc211.org/2005/gmd")) {
        String schemaLocation;
        // if document has srv namespace then add srv schemaLocation
        if (elMd.getNamespace("srv") != null) {
          schemaLocation =
              " http://www.isotc211.org/2005/srv http://schemas.opengis.net/iso/19139/20060504/srv/srv.xsd";
        }
        // otherwise add gmd schemaLocation
        // (but not both! as that is invalid, the schemas describe
        // partially the same schema types)
        else {
          schemaLocation =
              "http://www.isotc211.org/2005/gmd http://www.isotc211.org/2005/gmd/gmd.xsd";
        }
        Attribute schemaLocationA =
            new Attribute("schemaLocation", schemaLocation, Csw.NAMESPACE_XSI);
        elMd.setAttribute(schemaLocationA);
      }
    }

    // --- increase metadata popularity
    if (!skipPopularity) dm.increasePopularity(context, id);

    return elMd;
  }
  public Element exec(Element params, ServiceContext context) throws Exception {
    GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
    DataManager dm = gc.getDataManager();
    AccessManager am = gc.getAccessManager();

    Dbms dbms = (Dbms) context.getResourceManager().open(Geonet.Res.MAIN_DB);

    String id = Utils.getIdentifierFromParameters(params, context);

    // -----------------------------------------------------------------------
    // --- check access

    MdInfo info = dm.getMetadataInfo(dbms, id);

    if (info == null) throw new MetadataNotFoundEx(id);

    Element ownerId = new Element("ownerid").setText(info.owner);
    Element hasOwner = new Element("owner");
    if (am.isOwner(context, id)) hasOwner.setText("true");
    else hasOwner.setText("false");

    // --- get all operations

    Element elOper = Lib.local.retrieve(dbms, "Operations").setName(Geonet.Elem.OPERATIONS);

    // -----------------------------------------------------------------------
    // --- retrieve groups operations

    Set<String> userGroups =
        am.getUserGroups(dbms, context.getUserSession(), context.getIpAddress());

    Element elGroup = Lib.local.retrieve(dbms, "Groups");

    List list = elGroup.getChildren();

    for (int i = 0; i < list.size(); i++) {
      Element el = (Element) list.get(i);

      el.setName(Geonet.Elem.GROUP);

      // --- get all operations that this group can do on given metadata

      String sGrpId = el.getChildText("id");

      el.setAttribute("userGroup", userGroups.contains(sGrpId) ? "true" : "false");

      String query = "SELECT operationId FROM OperationAllowed WHERE metadataId=? AND groupId=?";

      List listAllow = dbms.select(query, id, sGrpId).getChildren();

      // --- now extend the group list adding proper operations

      List listOper = elOper.getChildren();

      for (int j = 0; j < listOper.size(); j++) {
        String operId = ((Element) listOper.get(j)).getChildText("id");
        Element elGrpOper =
            new Element(Geonet.Elem.OPER).addContent(new Element(Geonet.Elem.ID).setText(operId));
        boolean bFound = false;

        for (int k = 0; k < listAllow.size(); k++) {
          Element elAllow = (Element) listAllow.get(k);

          if (operId.equals(elAllow.getChildText("operationid"))) {
            bFound = true;
            break;
          }
        }
        if (bFound) elGrpOper.addContent(new Element(Geonet.Elem.ON));

        el.addContent(elGrpOper);
      }
    }

    // -----------------------------------------------------------------------
    // --- put all together

    Element elRes =
        new Element(Jeeves.Elem.RESPONSE)
            .addContent(new Element(Geonet.Elem.ID).setText(id))
            .addContent(elOper)
            .addContent(elGroup)
            .addContent(ownerId)
            .addContent(hasOwner);

    return elRes;
  }
  public Element execute(Element request, ServiceContext context) throws CatalogException {
    checkService(request);
    checkVersion(request);

    // num counter
    int totalInserted = 0;
    int totalUpdated = 0;
    int totalDeleted = 0;

    // Response element
    Element response = new Element(getName() + "Response", Csw.NAMESPACE_CSW);

    List<String> strFileIds = new ArrayList<String>();

    // process the transaction from the first to the last
    List<Element> childList = request.getChildren();

    GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
    DataManager dataMan = gc.getDataManager();

    Set<String> toIndex = new HashSet<String>();

    try {
      // process the childlist
      for (Element transRequest : childList) {
        String transactionType = transRequest.getName().toLowerCase();
        if (transactionType.equals("insert")
            || transactionType.equals("update")
            || transactionType.equals("delete")) {
          List<Element> mdList = transRequest.getChildren();

          // insert to database, and get the number of inserted successful
          if (transactionType.equals("insert")) {
            Iterator<Element> inIt = mdList.iterator();
            while (inIt.hasNext()) {
              Element metadata = (Element) inIt.next().clone();
              boolean insertSuccess = insertTransaction(metadata, strFileIds, context, toIndex);
              if (insertSuccess) {
                totalInserted++;
              }
            }
          }
          // Update
          else if (transactionType.equals("update")) {
            Iterator<Element> inIt = mdList.iterator();
            Element metadata = null;

            while (inIt.hasNext()) {
              Element reqElem = (Element) inIt.next();
              if (reqElem.getNamespace() != Csw.NAMESPACE_CSW) {
                metadata = (Element) reqElem.clone();
              }
            }

            totalUpdated = updateTransaction(transRequest, metadata, context, toIndex);
          }
          // Delete
          else {
            totalDeleted = deleteTransaction(transRequest, context);
          }
        }
      }
    } catch (Exception e) {
      Log.error(Geonet.CSW, "Cannot process transaction");
      Log.error(Geonet.CSW, " (C) StackTrace\n" + Util.getStackTrace(e));

      throw new NoApplicableCodeEx("Cannot process transaction: " + e.getMessage());
    } finally {
      try {
        dataMan.indexInThreadPool(context, new ArrayList<String>(toIndex), null);
      } catch (SQLException e) {
        Log.error(Geonet.CSW, "cannot index");
        Log.error(Geonet.CSW, " (C) StackTrace\n" + Util.getStackTrace(e));
      }
      getResponseResult(request, response, strFileIds, totalInserted, totalUpdated, totalDeleted);
    }

    return response;
  }
  /**
   * @param request
   * @param xml
   * @param context
   * @param toIndex
   * @return
   * @throws Exception
   */
  private int updateTransaction(
      Element request, Element xml, ServiceContext context, Set<String> toIndex) throws Exception {
    GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
    DataManager dataMan = gc.getDataManager();

    if (context.getUserSession().getUserId() == null)
      throw new NoApplicableCodeEx("User not authenticated.");

    int totalUpdated = 0;

    // Update full metadata
    if (xml != null) {

      // Retrieve schema and the related Namespaces
      String schemaId = gc.getSchemamanager().autodetectSchema(xml);

      if (schemaId == null) {
        throw new NoApplicableCodeEx("Can't identify metadata schema");
      }

      // Retrieve the metadata identifier
      Dbms dbms = (Dbms) context.getResourceManager().open(Geonet.Res.MAIN_DB);

      String uuid = gc.getDataManager().extractUUID(schemaId, xml);

      if (uuid.length() == 0) {
        throw new NoApplicableCodeEx("Metadata identifier not provided");
      }

      // Update metadata record
      String id = dataMan.getMetadataId(dbms, uuid);

      if (id == null) return totalUpdated;

      if (!gc.getAccessManager().canEdit(context, id))
        throw new NoApplicableCodeEx("User not allowed to update this metadata(" + id + ").");

      String changeDate = null;

      boolean validate = false;
      boolean ufo = false;
      boolean index = false;
      String language = context.getLanguage();
      dataMan.updateMetadata(
          context, dbms, id, xml, validate, ufo, index, language, changeDate, false);

      dbms.commit();
      toIndex.add(id);

      totalUpdated++;

      return totalUpdated;

      // Update properties
    } else {
      // first, search the record in the database to get the record id
      Element constr = (Element) request.getChild("Constraint", Csw.NAMESPACE_CSW).clone();
      List<Element> results = getResultsFromConstraints(context, constr);

      List<Element> recordProperties =
          (List<Element>) request.getChildren("RecordProperty", Csw.NAMESPACE_CSW);

      Iterator<Element> it = results.iterator();
      if (!it.hasNext()) return totalUpdated;

      Dbms dbms = (Dbms) context.getResourceManager().open(Geonet.Res.MAIN_DB);

      Set updatedMd = new HashSet<String>();
      // Process all records selected
      while (it.hasNext()) {
        Element result = it.next();
        String uuid = result.getChildText("identifier", Csw.NAMESPACE_DC);
        String id = dataMan.getMetadataId(dbms, uuid);
        String changeDate = null;

        if (id == null) continue;

        if (!dataMan.getAccessManager().canEdit(context, id))
          throw new NoApplicableCodeEx("User not allowed to update this metadata(" + id + ").");

        Element metadata = dataMan.getMetadata(context, id, false, false, true);
        metadata.removeChild("info", Edit.NAMESPACE);

        // Retrieve the schema and Namespaces of metadata to update
        String schemaId = gc.getDataManager().autodetectSchema(metadata);

        if (schemaId == null) {
          throw new NoApplicableCodeEx("Can't identify metadata schema");
        }

        Map mapNs = retrieveNamepacesForSchema(gc.getDataManager().getSchema(schemaId));

        boolean metadataChanged = false;

        // Process properties to update
        for (Element recordProperty : recordProperties) {
          Element propertyNameEl = recordProperty.getChild("Name", Csw.NAMESPACE_CSW);
          Element propertyValueEl = recordProperty.getChild("Value", Csw.NAMESPACE_CSW);

          String propertyName = propertyNameEl.getText();

          String propertyValue = propertyValueEl.getText();

          // Get XPath for queriable name, i provided in propertyName.
          // Otherwise assume propertyName contains full XPath to property to update
          String xpathProperty = FieldMapper.mapXPath(propertyName, schemaId);
          if (xpathProperty == null) {
            xpathProperty = propertyName;
          }

          Log.info(Geonet.CSW, "Xpath of property: " + xpathProperty);
          XPath xpath = new JDOMXPath(xpathProperty);
          xpath.setNamespaceContext(new SimpleNamespaceContext(mapNs));

          Object propEl = xpath.selectSingleNode(metadata);
          Log.info(Geonet.CSW, "XPath found in metadata: " + (propEl != null));

          // If a property is not found in metadata, just ignore it.
          if (propEl != null) {
            if (propEl instanceof Element) {
              ((Element) propEl).setText(propertyValue);
              metadataChanged = true;

            } else if (propEl instanceof Attribute) {
              ((Attribute) propEl).setValue(propertyValue);
              metadataChanged = true;
            }
          }
        } // for(Element recordProperty : recordProperties)

        // Update the metadata with changes
        if (metadataChanged) {
          boolean validate = false;
          boolean ufo = false;
          boolean index = false;
          String language = context.getLanguage();
          dataMan.updateMetadata(
              context, dbms, id, metadata, validate, ufo, index, language, changeDate, false);

          updatedMd.add(id);

          totalUpdated++;
        }
      }
      dbms.commit();
      toIndex.addAll(updatedMd);

      return totalUpdated;
    }
  }
  /**
   * @param xml
   * @param fileIds
   * @param context
   * @param toIndex
   * @return
   * @throws Exception
   */
  private boolean insertTransaction(
      Element xml, List<String> fileIds, ServiceContext context, Set<String> toIndex)
      throws Exception {
    GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
    DataManager dataMan = gc.getDataManager();

    String schema = dataMan.autodetectSchema(xml);

    //		String category   = Util.getParam(request, Params.CATEGORY);
    String category = null, source = null, createDate = null, changeDate = null;

    String uuid;
    uuid = dataMan.extractUUID(schema, xml);
    if (uuid.length() == 0) uuid = UUID.randomUUID().toString();

    // -----------------------------------------------------------------------
    // --- insert metadata into the system

    UserSession us = context.getUserSession();

    if (us.getUserId() == null) throw new NoApplicableCodeEx("User not authenticated.");

    String profile = us.getProfile();

    // Only editors and above are allowed to insert metadata
    if (!profile.equals(Geonet.Profile.EDITOR)
        && !profile.equals(Geonet.Profile.REVIEWER)
        && !profile.equals(Geonet.Profile.USER_ADMIN)
        && !profile.equals(Geonet.Profile.ADMINISTRATOR))
      throw new NoApplicableCodeEx("User not allowed to insert metadata.");

    int userId = us.getUserIdAsInt();

    AccessManager am = gc.getAccessManager();
    Dbms dbms = (Dbms) context.getResourceManager().open(Geonet.Res.MAIN_DB);

    // Set default group: user first group
    Set<String> userGroups = am.getVisibleGroups(dbms, userId);
    String group;
    if (userGroups.isEmpty()) {
      group = null;
    } else {
      group = (String) userGroups.iterator().next();
    }
    //
    // insert metadata
    //
    String docType = null, title = null, isTemplate = null;
    boolean ufo = true, indexImmediate = false;
    String id =
        dataMan.insertMetadata(
            context,
            dbms,
            schema,
            xml,
            context.getSerialFactory().getSerial(dbms, "Metadata"),
            uuid,
            userId,
            group,
            source,
            isTemplate,
            docType,
            title,
            category,
            createDate,
            changeDate,
            ufo,
            indexImmediate);

    if (id == null) return false;

    // Set metadata as public if setting enabled
    SettingManager sm = gc.getSettingManager();
    boolean metadataPublic = sm.getValueAsBool("system/csw/metadataPublic", false);

    if (metadataPublic) {
      dataMan.setOperation(context, dbms, id, "1", AccessManager.OPER_VIEW);
    }

    dataMan.indexMetadata(dbms, id);

    fileIds.add(uuid);

    dbms.commit();
    toIndex.add(id);
    return true;
  }