Пример #1
0
  /**
   * Filter the metadata if process parameter is set and corresponding XSL transformation exists.
   *
   * @param ri
   * @param md
   * @return
   */
  private Element processMetadata(RecordInfo ri, Element md) {
    // process metadata
    if (!params.xslfilter.equals("")) {
      MetadataSchema metadataSchema = dataMan.getSchema(ri.schema);

      String filePath = metadataSchema.getSchemaDir() + "/process/" + processName + ".xsl";
      File xslProcessing = new File(filePath);
      if (!xslProcessing.exists()) {
        log.info(
            "     processing instruction not found for "
                + ri.schema
                + " schema. metadata not filtered.");
      } else {
        Element processedMetadata = null;
        try {
          processedMetadata = Xml.transform(md, filePath, processParams);
          if (log.isDebugEnabled()) log.debug("     metadata filtered.");
          md = processedMetadata;
        } catch (Exception e) {
          log.warning("     processing error (" + params.xslfilter + "): " + e.getMessage());
        }
      }
    }
    return md;
  }
  /** Process the links to downloadable files */
  private Element processDownloadLinks(
      ServiceContext context, String id, String siteURL, List elems, Element response) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    for (Iterator<Object> iter = elems.iterator(); iter.hasNext(); ) {
      Object ob = iter.next();
      if (ob instanceof Element) {
        Element elem = (Element) ob;
        elem = (Element) elem.clone();

        // --- get file name and href
        String fname = elem.getAttributeValue("name");
        String href = elem.getAttributeValue("href");

        if (href != null) {
          boolean local = href.startsWith(siteURL);
          boolean found = false;
          long size = 0;
          String dateModified = "";

          String linkPieces[] = href.split("\\&");

          // local file (possibly)
          if (linkPieces.length > 0) {

            if (local) {
              String access = "private";
              for (int i = 0; i < linkPieces.length; i++) {
                String lp = linkPieces[i];
                if (lp.startsWith("access=")) {
                  access = lp.substring(lp.indexOf('=') + 1);
                }
              }

              File dir = new File(Lib.resource.getDir(context, access, id));
              File file = new File(dir, fname);
              if (file.exists()) {
                size = file.length();
                Date date = new Date(file.lastModified());
                dateModified = sdf.format(date);
                found = true;
              }
            }
          }
          elem.setAttribute("size", size + "");
          elem.setAttribute("datemodified", dateModified);
          elem.setAttribute("found", found + "");
          elem.setAttribute("local", local + "");
          elem.setAttribute("download", "true");
          response.addContent(elem);
        } else {
          context.info("Unknown download link: " + Xml.getString(elem));
        }
      }
    }

    return response;
  }
  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;
  }
Пример #4
0
  private Element doSearch(XmlRequest request, Search s) throws OperationAbortedEx {
    request.setAddress(params.getServletPath() + "/srv/en/" + Geonet.Service.XML_SEARCH);

    try {
      log.info("Searching on : " + params.name);
      Element response = request.execute(s.createRequest());
      if (log.isDebugEnabled()) log.debug("Search results:\n" + Xml.getString(response));

      return response;
    } catch (Exception e) {
      log.warning("Raised exception when searching : " + e);
      throw new OperationAbortedEx("Raised exception when searching", e);
    }
  }
Пример #5
0
  /**
   * Does CSW GetRecordById request. If validation is requested and the metadata does not validate,
   * null is returned.
   *
   * @param uuid uuid of metadata to request
   * @return metadata the metadata
   */
  private Element retrieveMetadata(String uuid) {
    request.clearIds();
    request.addId(uuid);

    try {
      if (log.isDebugEnabled())
        log.debug("Getting record from : " + request.getHost() + " (uuid:" + uuid + ")");
      Element response = request.execute();
      if (log.isDebugEnabled()) log.debug("Record got:\n" + Xml.getString(response));

      @SuppressWarnings("unchecked")
      List<Element> list = response.getChildren();

      // --- maybe the metadata has been removed

      if (list.size() == 0) return null;

      response = list.get(0);
      response = (Element) response.detach();

      // validate it here if requested
      if (params.validate) {
        if (!dataMan.validate(response)) {
          log.info("Ignoring invalid metadata with uuid " + uuid);
          result.doesNotValidate++;
          return null;
        }
      }

      if (params.rejectDuplicateResource) {
        if (foundDuplicateForResource(uuid, response)) {
          return null;
        }
      }

      return response;
    } catch (Exception e) {
      log.warning("Raised exception while getting record : " + e);
      e.printStackTrace();
      result.unretrievable++;

      // --- we don't raise any exception here. Just try to go on
      return null;
    }
  }
Пример #6
0
  public Z3950ServerResults harvest() throws Exception {
    Set<String> newUuids = new HashSet<String>();

    int groupSize = 10;

    log.info("Retrieving remote metadata information:" + params.uuid);

    Z3950ServerResults serverResults = new Z3950ServerResults();

    // --- Clean all before harvest : Remove/Add mechanism
    localUuids = new UUIDMapper(dbms, params.uuid);

    // --- remove old metadata
    for (String uuid : localUuids.getUUIDs()) {
      String id = localUuids.getID(uuid);
      if (log.isDebugEnabled()) log.debug("  - Removing old metadata before update with id: " + id);
      dataMan.deleteMetadataGroup(context, dbms, id);
      serverResults.locallyRemoved++;
    }

    if (serverResults.locallyRemoved > 0) dbms.commit();

    // --- Search remote node
    MetaSearcher s = searchMan.newSearcher(SearchManager.Z3950, Geonet.File.SEARCH_Z3950_CLIENT);

    ServiceConfig config = new ServiceConfig();

    Element request = new Element("request");

    // --- Z39.50 servers from harvest params
    for (String id : params.getRepositories()) {
      request.addContent(new Element(Geonet.SearchResult.SERVERS).setText(id));
    }

    // --- Z39.50 query from harvest params
    request.addContent(new Element(Geonet.SearchResult.ZQUERY).setText(params.query));

    // --- don't get html presentations (get them later)
    request.addContent(new Element(Geonet.SearchResult.SERVERHTML).setText("off"));

    // --- set timeout to be 100 seconds
    request.addContent(new Element(Geonet.SearchResult.TIMEOUT).setText("100"));
    // --- set hitsPerPage
    request.addContent(new Element(Geonet.SearchResult.HITS_PER_PAGE).setText(groupSize + ""));

    // --- do the search
    s.search(context, request, config);

    if (s.getSize() == 0) {
      log.error("Search failed or returned 0 results, trying again");
      s.search(context, request, config);
      if (s.getSize() == 0) {
        throw new Exception("Bad luck, Search failed or returned 0 results");
      }
    }

    if (log.isDebugEnabled()) log.debug("Search returned " + s.getSize() + " hits");

    // -- process the hits in groups of groupSize
    int numberOfHits = Math.min(Integer.parseInt(params.maximumHits), s.getSize());
    // -- add from and to placeholders to request

    request.addContent(new Element("from"));
    request.addContent(new Element("to"));

    Element categories = Lib.local.retrieve(dbms, "Categories");
    if (log.isDebugEnabled()) log.debug("categories " + Xml.getString(categories));

    Element repositories = new Info().getZRepositories(context, settingMan);
    if (log.isDebugEnabled()) log.debug("repos " + Xml.getString(repositories));

    // -- build a map of collection code versus repository name for
    // -- assigning the categories
    Map<String, String> codes = new HashMap<String, String>();
    Map<String, String> catCodes = new HashMap<String, String>();

    // -- add new category for each repository
    boolean addcateg = false;
    for (String repo : params.getRepositories()) {
      Element repoElem = Xml.selectElement(repositories, "record[id='" + repo + "']");
      if (repoElem != null) {
        Element repoId = repoElem.getChild("id");
        String repoName = repoElem.getChildText("name");
        codes.put(
            repoId.getAttributeValue("serverCode") + ":" + repoId.getAttributeValue("code"),
            repoName);
        // create a result holder for this repository
        serverResults.getServerResult(repoName);

        // sanitize the name of the category
        String categName = repoName.replaceAll("[^\\w]", "");
        categName = categName.toLowerCase();
        catCodes.put(
            repoId.getAttributeValue("serverCode") + ":" + repoId.getAttributeValue("code"),
            categName);

        if (Xml.selectElement(categories, "record[name='" + categName + "']") == null) {
          int newId = context.getSerialFactory().getSerial(dbms, "Categories");
          dbms.execute("INSERT INTO Categories(id, name) VALUES (?, ?)", newId, categName);
          Lib.local.insert(dbms, "Categories", newId, repoName);
          addcateg = true;
        }
      }
    }

    if (addcateg) dbms.commit();

    // --- return only maximum hits as directed by the harvest params
    int nrGroups = (numberOfHits / groupSize) + 1;
    for (int i = 1; i <= nrGroups; i++) {
      int lower = ((i - 1) * groupSize) + 1;
      int upper = Math.min((i * groupSize), numberOfHits);
      request.getChild("from").setText("" + lower);
      request.getChild("to").setText("" + upper);

      // --- Loading results
      List<Document> list = s.presentDocuments(context, request, config);

      // --- Loading categories and groups
      localCateg = new CategoryMapper(dbms);
      localGroups = new GroupMapper(dbms);

      if (log.isDebugEnabled())
        log.debug(
            "There are "
                + (list.size() - 1)
                + " children in the results ("
                + lower
                + " to "
                + upper
                + ")");

      boolean transformIt = false;
      String thisXslt = context.getAppPath() + Geonet.Path.IMPORT_STYLESHEETS + "/";
      if (!params.importXslt.equals("none")) {
        thisXslt = thisXslt + params.importXslt;
        transformIt = true;
      }

      // --- For each record....
      for (Document doc : list) {
        Element md = doc.getRootElement();
        String eName = md.getQualifiedName();
        if (eName.equals("summary")) continue;

        // -- Remove existing geonet:info children as for example
        // -- GeoNetwork Z39.50 server return when full mode
        // -- an extra element with server info not needed
        // -- once harvested
        String colCode = "";
        Element info = md.getChild(Edit.RootChild.INFO, Edit.NAMESPACE);
        if (info != null) {
          String serverCode = info.getChildText("server");
          int colPos = serverCode.indexOf(':');
          if (colPos != -1) {
            colCode = serverCode.substring(0, colPos) + ":" + info.getChildText("collection");
          }
        }
        md.removeChildren(Edit.RootChild.INFO, Edit.NAMESPACE);
        String repoName = codes.get(colCode);
        if (log.isDebugEnabled()) log.debug("Processing record from server " + repoName);
        HarvestResult result = serverResults.getServerResult(repoName);
        result.totalMetadata++;

        if (eName.equals("error")) {
          log.error("JZKit could not retrieve record - returned " + Xml.getString(md));
          result.unretrievable++;
          continue;
        }

        // transform using importxslt if not none
        if (transformIt) {
          try {
            if (log.isDebugEnabled()) log.debug("Before transform: " + Xml.getString(md));
            md = Xml.transform(md, thisXslt);
            if (log.isDebugEnabled()) log.debug("After transform: " + Xml.getString(md));
          } catch (Exception e) {
            System.out.println("Cannot transform XML, ignoring. Error was: " + e.getMessage());
            result.badFormat++;
            continue; // skip this one
          }
        }

        // detect schema, extract uuid and add
        String schema = dataMan.autodetectSchema(md, null);
        if (schema == null) {
          log.warning("Skipping metadata with unknown schema.");
          result.unknownSchema++;
          continue;
        }

        String uuid = null;
        try {
          uuid = dataMan.extractUUID(schema, md);
        } catch (Exception e) {
          log.error("Unable to extract UUID: " + e.getMessage());
          e.printStackTrace();
        }

        if (uuid == null || uuid.equals("")) {
          log.warning("Skipping metadata due to failure extracting uuid (uuid null or empty).");
          result.unretrievable++;
          continue;
        }

        log.info("  - Adding metadata with " + uuid);

        // --- generate a new metadata id

        int id = context.getSerialFactory().getSerial(dbms, "Metadata");
        // TODO end confusion about datatypes
        String id$ = Integer.toString(id);

        String docType = "";
        if (!transformIt && (doc.getDocType() != null)) {
          docType = Xml.getString(doc.getDocType());
        }

        // --- check for duplicate uuid - violates constraints on metadata table
        // --- if we attempt insert
        boolean alreadyAdded = !newUuids.add(uuid);
        boolean alreadyInDb = (dataMan.getMetadataId(dbms, uuid) != null);
        if (alreadyAdded || alreadyInDb) {
          log.error("Uuid " + uuid + " already exists in this set/database - cannot insert");
          result.couldNotInsert++;
          continue;
        }

        //
        // insert metadata
        //
        try {
          String groupOwner = "1", isTemplate = "n", title = null;
          int owner = 1;
          String category = null, createDate = new ISODate().toString(), changeDate = createDate;
          boolean ufo = false, indexImmediate = false;
          dataMan.insertMetadata(
              context,
              dbms,
              schema,
              md,
              id,
              uuid,
              owner,
              groupOwner,
              params.uuid,
              isTemplate,
              docType,
              title,
              category,
              createDate,
              changeDate,
              ufo,
              indexImmediate);

        } catch (Exception e) {
          log.error("Unable to insert metadata " + e.getMessage());
          e.printStackTrace();
          result.couldNotInsert++;
          continue;
        }

        addPrivileges(id$, params.getPrivileges(), localGroups, dataMan, context, dbms, log);
        addCategories(
            id$,
            params.getCategories(),
            localCateg,
            dataMan,
            dbms,
            context,
            log,
            catCodes.get(colCode));

        dataMan.setTemplateExt(dbms, id, "n", null);
        dataMan.setHarvestedExt(dbms, id, params.uuid, params.name);

        // validate it here if requested
        if (params.validate) {
          Document docVal;
          if (!transformIt && (doc.getDocType() != null)) {
            docVal = new Document(md, (DocType) doc.getDocType().detach());
          } else {
            docVal = new Document(md);
          }

          if (!dataMan.doValidate(dbms, schema, id$, docVal, context.getLanguage())) {
            result.doesNotValidate++;
          }
        }

        dataMan.indexMetadata(dbms, id$);

        result.addedMetadata++;
      }
    }

    dbms.commit();
    return serverResults;
  }
Пример #7
0
  /**
   * Apply a list of changes to the metadata record in current editing session.
   *
   * <p>The changes are a list of KVP. A key contains at least the element identifier from the
   * meta-document. A key starting with an "X" should contain an XML fragment for the value. The
   * following KVP combinations are allowed:
   *
   * <ul>
   *   <li>ElementId=ElementValue
   *   <li>ElementId_AttributeName=AttributeValue
   *   <li>ElementId_AttributeNamespacePrefixCOLONAttributeName=AttributeValue
   *   <li>XElementId=ElementValue
   *   <li>XElementId_ElementName=ElementValue
   * </ul>
   *
   * ElementName MUST contain "{@value #COLON_SEPARATOR}" instead of ":" for prefixed elements.
   *
   * <p>When using X key, value could contains many XML fragments (eg. &lt;gmd:keywords
   * .../&gt;{@value #XML_FRAGMENT_SEPARATOR}&lt;gmd:keywords .../&gt;) separated by {@link
   * #XML_FRAGMENT_SEPARATOR}. All those fragments are inserted to the last element of this type in
   * its parent if ElementName is set. If not, the element with ElementId is replaced.
   *
   * <p>
   *
   * @param dbms
   * @param id Metadata internal identifier.
   * @param changes List of changes to apply.
   * @return The update metadata record
   * @throws Exception
   */
  protected Element applyChangesEmbedded(Dbms dbms, String id, Hashtable changes) throws Exception {
    String schema = dataManager.getMetadataSchema(dbms, id);
    EditLib editLib = dataManager.getEditLib();

    // --- get metadata from session
    Element md = getMetadataFromSession(session, id);

    // Store XML fragments to be handled after other elements update
    Map<String, String> xmlInputs = new HashMap<String, String>();

    // --- update elements
    for (Enumeration e = changes.keys(); e.hasMoreElements(); ) {
      String ref = ((String) e.nextElement()).trim();
      String value = ((String) changes.get(ref)).trim();
      String attribute = null;

      // Avoid empty key
      if (ref.equals("")) {
        continue;
      }

      // Catch element starting with a X to replace XML fragments
      if (ref.startsWith("X")) {
        ref = ref.substring(1);
        xmlInputs.put(ref, value);
        continue;
      }

      if (updatedLocalizedTextElement(md, ref, value, editLib)) {
        continue;
      }

      int at = ref.indexOf('_');
      if (at != -1) {
        attribute = ref.substring(at + 1);
        ref = ref.substring(0, at);
      }

      Element el = editLib.findElement(md, ref);
      if (el == null) {
        Log.error(Geonet.EDITOR, MSG_ELEMENT_NOT_FOUND_AT_REF + ref);
        continue;
      }

      // Process attribute
      if (attribute != null) {
        Pair<Namespace, String> attInfo =
            parseAttributeName(attribute, COLON_SEPARATOR, id, md, dbms, editLib);
        String localname = attInfo.two();
        Namespace attrNS = attInfo.one();
        if (el.getAttribute(localname, attrNS) != null) {
          el.setAttribute(new Attribute(localname, value, attrNS));
        }
      } else {
        // Process element value
        List content = el.getContent();

        for (int i = 0; i < content.size(); i++) {
          if (content.get(i) instanceof Text) {
            el.removeContent((Text) content.get(i));
            i--;
          }
        }
        el.addContent(value);
      }
    }

    // Deals with XML fragments to insert or update
    if (!xmlInputs.isEmpty()) {

      // Loop over each XML fragments to insert or replace
      for (String ref : xmlInputs.keySet()) {
        String value = xmlInputs.get(ref);
        String name = null;
        int addIndex = ref.indexOf('_');
        if (addIndex != -1) {
          name = ref.substring(addIndex + 1);
          ref = ref.substring(0, addIndex);
        }

        // Get element to fill
        Element el = editLib.findElement(md, ref);
        if (el == null) {
          Log.error(Geonet.EDITOR, MSG_ELEMENT_NOT_FOUND_AT_REF + ref);
          continue;
        }

        if (value != null && !value.equals("")) {
          String[] fragments = value.split(XML_FRAGMENT_SEPARATOR);
          for (String fragment : fragments) {
            if (name != null) {
              if (Log.isDebugEnabled(Geonet.EDITOR))
                Log.debug(
                    Geonet.EDITOR,
                    "Add XML fragment; " + fragment + " to element with ref: " + ref);
              name = name.replace(COLON_SEPARATOR, ":");
              editLib.addFragment(schema, el, name, fragment);
            } else {
              if (Log.isDebugEnabled(Geonet.EDITOR))
                Log.debug(
                    Geonet.EDITOR,
                    "Add XML fragment; "
                        + fragment
                        + " to element with ref: "
                        + ref
                        + " replacing content.");

              // clean before update
              el.removeContent();
              fragment = addNamespaceToFragment(fragment);

              // Add content
              el.addContent(Xml.loadString(fragment, false));
            }
          }
        }
      }
    }

    // --- remove editing info
    editLib.removeEditingInfo(md);
    editLib.contractElements(md);

    return (Element) md.detach();
  }
Пример #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.getBean(DataManager.class);
    UserSession session = context.getUserSession();

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

    String id = Utils.getIdentifierFromParameters(params, context);
    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;
  }
Пример #9
0
 public String toString() {
   return Xml.getString(getQuery());
 }
Пример #10
0
 public ProfileLib() throws JDOMException, IOException {
   profiles = Xml.loadFile(Config.getConfig().getWebapp() + "/xml/user-profiles.xml");
 }
Пример #11
0
  /**
   * TODO javadoc.
   *
   * @param dbms
   * @param id
   * @param changes
   * @param currVersion
   * @return
   * @throws Exception
   */
  private Element applyChanges(Dbms dbms, String id, Hashtable changes, String currVersion)
      throws Exception {
    Lib.resource.checkEditPrivilege(context, id);
    Element md = xmlSerializer.select(dbms, "Metadata", id, context);

    // --- check if the metadata has been deleted
    if (md == null) {
      return null;
    }

    EditLib editLib = dataManager.getEditLib();

    String schema = dataManager.getMetadataSchema(dbms, id);
    editLib.expandElements(schema, md);
    editLib.enumerateTree(md);

    // --- check if the metadata has been modified from last time
    if (currVersion != null && !editLib.getVersion(id).equals(currVersion)) {
      return null;
    }

    // --- update elements
    for (Enumeration e = changes.keys(); e.hasMoreElements(); ) {
      String ref = ((String) e.nextElement()).trim();
      String val = ((String) changes.get(ref)).trim();
      String attr = null;

      if (updatedLocalizedTextElement(md, ref, val, editLib)) {
        continue;
      }

      int at = ref.indexOf('_');
      if (at != -1) {
        attr = ref.substring(at + 1);
        ref = ref.substring(0, at);
      }
      boolean xmlContent = false;
      if (ref.startsWith("X")) {
        ref = ref.substring(1);
        xmlContent = true;
      }
      Element el = editLib.findElement(md, ref);
      if (el == null) throw new IllegalStateException("Element not found at ref = " + ref);

      if (attr != null) {
        // The following work-around decodes any attribute name that has a COLON in it
        // The : is replaced by the word COLON in the xslt so that it can be processed
        // by the XML Serializer when an update is submitted - a better solution is
        // to modify the argument handler in Jeeves to store arguments with their name
        // as a value rather than as the element itself
        Integer indexColon = attr.indexOf("COLON");
        if (indexColon != -1) {
          String prefix = attr.substring(0, indexColon);
          String localname = attr.substring(indexColon + 5);
          String namespace =
              editLib.getNamespace(prefix + ":" + localname, md, dataManager.getSchema(schema));
          Namespace attrNS = Namespace.getNamespace(prefix, namespace);
          if (el.getAttribute(localname, attrNS) != null) {
            el.setAttribute(new Attribute(localname, val, attrNS));
          }
          // End of work-around
        } else {
          if (el.getAttribute(attr) != null) el.setAttribute(new Attribute(attr, val));
        }
      } else if (xmlContent) {
        if (Log.isDebugEnabled(Geonet.EDITOR)) Log.debug(Geonet.EDITOR, "replacing XML content");
        el.removeContent();
        val = addNamespaceToFragment(val);
        el.addContent(Xml.loadString(val, false));
      } else {
        List content = el.getContent();
        for (int i = 0; i < content.size(); i++) {
          if (content.get(i) instanceof Text) {
            el.removeContent((Text) content.get(i));
            i--;
          }
        }
        el.addContent(val);
      }
    }
    // --- remove editing info added by previous call
    editLib.removeEditingInfo(md);

    editLib.contractElements(md);
    return md;
  }
Пример #12
0
  public InformationFragment[] getFragment(
      int startingFragment, int count, RecordFormatSpecification spec) throws IRResultSetException {
    if (Log.isDebugEnabled(Geonet.Z3950_SERVER))
      Log.debug(
          Geonet.Z3950_SERVER,
          "Request for fragment start:" + startingFragment + ", count:" + count);

    InformationFragment fragment[] = new InformationFragment[count];

    ExplicitRecordFormatSpecification rec_spec =
        new ExplicitRecordFormatSpecification("xml", null, "f");

    try {
      // build fragment data
      int from = startingFragment;
      int to = startingFragment + count - 1;

      Element request = new Element("request");
      request.addContent(new Element("from").setText(from + ""));
      request.addContent(new Element("to").setText(to + ""));
      ServiceConfig config = new ServiceConfig();

      if (Log.isDebugEnabled(Geonet.Z3950_SERVER))
        Log.debug(Geonet.Z3950_SERVER, "Search request:\n" + Xml.getString(request));
      // get result set
      Element result = this.metasearcher.present(this.srvxtx, request, config);

      if (Log.isDebugEnabled(Geonet.Z3950_SERVER))
        Log.debug(Geonet.Z3950_SERVER, "Search result:\n" + Xml.getString(result));

      // remove summary
      result.removeChildren("summary");
      @SuppressWarnings("unchecked")
      List<Element> list = result.getChildren();

      if (Log.isDebugEnabled(Geonet.Z3950_SERVER))
        Log.debug(Geonet.Z3950_SERVER, "Set name asked:" + spec);

      // save other records to fragment
      for (int i = 0; i < count; i++) {
        Element md = list.get(0);
        md.detach();

        if (Log.isDebugEnabled(Geonet.Z3950_SERVER))
          Log.debug(Geonet.Z3950_SERVER, "Returning fragment:\n" + Xml.getString(md));

        // add metadata

        // fragment[i] = new DOMTree("geonetwork", "geonetwork", null,   getRecord(md),rec_spec );
        // fragment[i].setHitNo(startingFragment+i);

        DOMOutputter outputter = new DOMOutputter();
        Document doc = new Document(md);
        org.w3c.dom.Document doc2 = outputter.output(doc);

        fragment[i] =
            new InformationFragmentImpl(
                startingFragment + i, "geonetwork", "geonetwork", null, doc2, rec_spec);
        // fragment[i] = new
        // InformationFragmentImpl(startingFragment+i,"geonetwork","geonetwork",null,doc,rec_spec);

        // System.err.println(fragment[i]);

      }
      this.srvxtx.getResourceManager().close();
      if (Log.isDebugEnabled(Geonet.Z3950_SERVER))
        Log.debug(Geonet.Z3950_SERVER, "Fragment returned");
    } catch (Throwable e) {
      try {
        this.srvxtx.getResourceManager().abort();
      } catch (Exception e2) {
        e2.printStackTrace();
      }

      if (Log.isDebugEnabled(Geonet.Z3950_SERVER))
        Log.debug(Geonet.Z3950_SERVER, "Exception: " + e.getClass().getName() + " " + e);
      // e.printStackTrace();
    }

    return fragment;
  }