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; }
public Element exec(Element params, ServiceContext context) throws Exception { GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME); ThesaurusManager thesaurusMan = gc.getThesaurusManager(); String THESAURUS_DIR = thesaurusMan.getThesauriDirectory(); Element thesauriList = new Element("thesaurusList"); String type = Util.getParam(params, Params.TYPE, init_type); if (type.equals("all-directories")) { listThesauri( thesauriList, THESAURUS_DIR + EXTERNAL_DIR, 1, directoryFilter, Geonet.CodeList.EXTERNAL); listThesauri( thesauriList, THESAURUS_DIR + LOCAL_DIR, 1, directoryFilter, Geonet.CodeList.LOCAL); } else if (type.equals("upload-directories")) { listThesauri( thesauriList, THESAURUS_DIR + EXTERNAL_DIR, 1, directoryFilter, Geonet.CodeList.EXTERNAL); } else if (type.equals("all-thesauri")) { listThesauri( thesauriList, THESAURUS_DIR + EXTERNAL_DIR, 2, thesauriFilter, Geonet.CodeList.EXTERNAL); listThesauri( thesauriList, THESAURUS_DIR + LOCAL_DIR, 2, thesauriFilter, Geonet.CodeList.LOCAL); } else if (type.equals("update-thesauri")) { listThesauri( thesauriList, THESAURUS_DIR + LOCAL_DIR, 3, thesauriFilter, Geonet.CodeList.LOCAL); } else { listThesauri( thesauriList, THESAURUS_DIR + EXTERNAL_DIR, 3, thesauriFilter, Geonet.CodeList.EXTERNAL); listThesauri( thesauriList, THESAURUS_DIR + LOCAL_DIR, 3, thesauriFilter, Geonet.CodeList.LOCAL); } // ----------------------------------------------------------------------- Element elRes = new Element(Jeeves.Elem.RESPONSE).addContent(thesauriList); // ----------------------------------------------------------------------- String selected = Util.getParam(params, Params.SELECTED, "none"); if (!selected.equals("none")) { Element elSelected = new Element("selectedThesaurus"); elSelected.addContent(selected); elRes.addContent(elSelected); } String mode = Util.getParam(params, "mode", "none"); if (!mode.equals("none")) { Element elMode = new Element("mode"); elMode.addContent(mode); elRes.addContent(elMode); } // ----------------------------------------------------------------------- return elRes; }
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; }
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); } }
/** * Get Feature Catalog ID if exists using relation table. * * @param context * @param metadataId Metadata record id to search for feature catalogue for. * @return String Feature catalogue uuid. * @throws Exception */ private static String getFeatureCatalogID(ServiceContext context, int metadataId) throws Exception { GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME); DataManager dm = gc.getBean(DataManager.class); List<MetadataRelation> relations = context.getBean(MetadataRelationRepository.class).findAllById_MetadataId(metadataId); if (relations.isEmpty()) { return ""; } // Assume only one feature catalogue is available for a metadata record. int ftId = relations.get(0).getId().getRelatedId(); String ftUuid = dm.getMetadataUuid("" + ftId); return ftUuid != null ? ftUuid : ""; }
public GNResultSet(GNXMLQuery query, Object userInfo, Observer[] observers, ServiceContext srvctx) throws Exception { super(observers); this.query = query; this.srvxtx = srvctx; try { GeonetContext gc = (GeonetContext) this.srvxtx.getHandlerContext(Geonet.CONTEXT_NAME); SearchManager searchMan = gc.getBean(SearchManager.class); metasearcher = searchMan.newSearcher(SearchManager.LUCENE, Geonet.File.SEARCH_Z3950_SERVER); } catch (Exception e) { if (Log.isDebugEnabled(Geonet.Z3950_SERVER)) Log.debug(Geonet.Z3950_SERVER, "error constructing GNresult set: " + e); e.printStackTrace(); } }
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 Element serviceSpecificExec(Element params, ServiceContext context) throws Exception { GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME); DataManager dataMan = gc.getBean(DataManager.class); AccessManager accessMan = gc.getBean(AccessManager.class); Dbms dbms = (Dbms) context.getResourceManager().open(Geonet.Res.MAIN_DB); String id = Utils.getIdentifierFromParameters(params, context); // ----------------------------------------------------------------------- // --- check access MdInfo info = dataMan.getMetadataInfo(dbms, id); if (info == null) throw new IllegalArgumentException("Metadata not found --> " + id); if (!accessMan.canEdit(context, id)) throw new OperationNotAllowedEx(); // ----------------------------------------------------------------------- // --- backup metadata in 'removed' folder if (info.template != MdInfo.Template.SUBTEMPLATE) backupFile( context, id, info.uuid, MEFLib.doExport(context, info.uuid, "full", false, true, false)); // ----------------------------------------------------------------------- // --- remove the metadata directory including the public and private directories. File pb = new File(Lib.resource.getMetadataDir(context, id)); FileCopyMgr.removeDirectoryOrFile(pb); // ----------------------------------------------------------------------- // --- delete metadata and return status dataMan.deleteMetadata(context, dbms, id); Element elResp = new Element(Jeeves.Elem.RESPONSE); elResp.addContent(new Element(Geonet.Elem.ID).setText(id)); return elResp; }
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); }
public Element exec(Element params, ServiceContext context) throws Exception { GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME); SchemaManager scm = gc.getBean(SchemaManager.class); String schema = Util.getParam(params, Params.SCHEMA); String urlStr, uuid, fname; uuid = ""; URL url = null; // -- try the file name argument then the url then the uuid of a metadata // -- record to which a schema is attached fname = Util.getParam(params, Params.FNAME, ""); if ("".equals(fname)) { urlStr = Util.getParam(params, Params.URL, ""); if ("".equals(urlStr)) { uuid = Util.getParam(params, Params.UUID, ""); if ("".equals(uuid)) { throw new IllegalArgumentException("One of fname, url or uuid must be supplied"); } } else { try { url = new URL(urlStr); } catch (MalformedURLException mu) { throw new OperationAbortedEx("URL " + urlStr + " is malformed: " + mu.getMessage()); } } } // -- test if schema to be updated exists, if not then chuck a fit and exit if (!scm.existsSchema(schema)) { throw new OperationAbortedEx("Schema doesn't exist"); } SchemaUtils su = new SchemaUtils(); return su.updateSchema(context, schema, fname, url, uuid, scm); }
/** * @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; }
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; }
private String getSiteId() { GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME); SettingManager sm = gc.getBean(SettingManager.class); return sm.getValue("system/site/siteId"); }
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 exec(Element params, ServiceContext context) throws Exception { GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME); return gc.getBean(DataManager.class).getKeywords(); }
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; }
/** * @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; }
public GeonetResult align(Set<RecordInfo> records) throws Exception { log.info("Start of alignment for : " + params.name); // ----------------------------------------------------------------------- // --- retrieve all local categories and groups // --- retrieve harvested uuids for given harvesting node localCateg = new CategoryMapper(dbms); localGroups = new GroupMapper(dbms); localUuids = new UUIDMapper(dbms, params.uuid); dbms.commit(); parseXSLFilter(); // ----------------------------------------------------------------------- // --- remove old metadata for (String uuid : localUuids.getUUIDs()) if (!exists(records, uuid)) { String id = localUuids.getID(uuid); if (log.isDebugEnabled()) log.debug(" - Removing old metadata with id:" + id); dataMan.deleteMetadata(context, dbms, id); dbms.commit(); result.locallyRemoved++; } // ----------------------------------------------------------------------- // --- insert/update new metadata for (RecordInfo ri : records) { result.totalMetadata++; if (!dataMan.existsSchema(ri.schema)) { if (log.isDebugEnabled()) log.debug( " - Metadata skipped due to unknown schema. uuid:" + ri.uuid + ", schema:" + ri.schema); result.unknownSchema++; } else { String id = dataMan.getMetadataId(dbms, ri.uuid); // look up value of localrating/enabled GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME); SettingManager settingManager = gc.getSettingManager(); boolean localRating = settingManager.getValueAsBool("system/localrating/enabled", false); if (id == null) { addMetadata(ri, localRating); } else { updateMetadata(ri, id, localRating); } } } log.info("End of alignment for : " + params.name); return result; }
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; }
/** * Uses the notifier update service to handle insertion and updates of metadata * * @param metadata * @param metadataUuid * @throws MetadataNotifierClientException */ public void webUpdate( String serviceUrl, String username, String password, String metadata, String metadataUuid, GeonetContext gc) throws MetadataNotifierClientException { HttpClient client; Protocol.registerProtocol( "https", new Protocol("https", new EasySSLProtocolSocketFactory(), 443)); // Create a method instance. PostMethod method = new PostMethod(serviceUrl); // Provide custom retry handler is necessary method .getParams() .setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); NameValuePair[] data = { new NameValuePair("action", "update"), new NameValuePair("uuid", metadataUuid), new NameValuePair("XMLFile", metadata) }; method.setRequestBody(data); // RequestEntity requestEntity = new InputStreamRequestEntity(isoDocumentInputStream); // method.setRequestEntity(requestEntity); try { // Create an instance of HttpClient. client = new HttpClient(); if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) { System.out.println("webUpdate: SET USER"); client .getState() .setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); method.setDoAuthentication(true); } System.out.println("settingMan: " + (gc.getSettingManager() != null)); if (gc.getSettingManager() != null) Lib.net.setupProxy(gc.getSettingManager(), client); // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { throw new MetadataNotifierClientException("Method failed: " + method.getStatusLine()); } // Read the response body. // byte[] responseBody = method.getResponseBody(); // Deal with the response. // Use caution: ensure correct character encoding and is not binary data // System.out.println(new String(responseBody)); } catch (HttpException e) { throw new MetadataNotifierClientException(e); } catch (IOException e) { throw new MetadataNotifierClientException(e); } finally { // Release the connection. method.releaseConnection(); client = null; } }
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 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; }
// -------------------------------------------------------------------------- // --- // --- Service // --- // -------------------------------------------------------------------------- @Override public Element serviceSpecificExec(Element params, ServiceContext context) throws Exception { boolean readOnlyMode = super.exec(params, context) == null; if (readOnlyMode) { return null; } String message = ""; GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME); // gets the total popularity count (=100) Dbms dbms = (Dbms) context.getResourceManager().open(Geonet.Res.MAIN_DB); // wont work if there is no metadata List l = dbms.select("select sum(popularity) as sumpop from metadata").getChildren(); if (l.size() != 1) { message = "cannot get popularity count"; return null; } int cnt = Integer.parseInt(((Element) l.get(0)).getChildText("sumpop")); if (Log.isDebugEnabled(Geonet.SEARCH_LOGGER)) Log.debug(Geonet.SEARCH_LOGGER, "query to get popularity by group:\n" + query); dbms = (Dbms) context.getResourceManager().open(Geonet.Res.MAIN_DB); DefaultPieDataset dataset = new DefaultPieDataset(); List resultSet = dbms.select(query).getChildren(); for (int i = 0; i < resultSet.size(); i++) { Element record = (Element) resultSet.get(i); String popularity = (record).getChildText("popu"); Double d = 0.0; if (popularity.length() > 0) { d = (Double.parseDouble(popularity) / cnt) * 100; } dataset.setValue(record.getChildText("source"), d); // System.out.println(record.getChildText("groupname") + ", " + d); } // create a chart... JFreeChart chart = ChartFactory.createPieChart( null, dataset, true, // legend? true, // tooltips? false // URLs? ); // hard coded values for the moment. should come from a configuration file. chart.setBackgroundPaint(Color.decode("#E7EDF5")); String chartFilename = "popubycatalog_" + System.currentTimeMillis() + ".png"; File statFolder = new File( gc.getHandlerConfig().getMandatoryValue(Geonet.Config.RESOURCES_DIR) + File.separator + "images" + File.separator + "statTmp"); if (!statFolder.exists()) { statFolder.mkdirs(); } File f = new File(statFolder, chartFilename); this.imageMap = org.fao.geonet.services.statistics.ChartFactory.writeChartImage( chart, f, this.chartWidth, this.chartHeight, this.createTooltips, "graphPopuByCatalogImageMap"); // will return some info to the XSLT: // dateFrom, dateTo, graphicType, chartUrl, tooltipImageMap, // message, chartWidth, chartHeight Element elResp = new Element(Jeeves.Elem.RESPONSE); Element elchartUrl = new Element("popuByCatalogUrl") .setText(context.getBaseUrl() + "/images/statTmp/" + chartFilename); Element elTooltipImageMap = new Element("tooltipImageMap").addContent(this.createTooltips ? this.imageMap : ""); Element elMessage = new Element("message").setText(message); Element elChartWidth = new Element("chartWidth").setText("" + this.chartWidth); Element elChartHeight = new Element("chartHeight").setText("" + this.chartHeight); elResp.addContent(elchartUrl); elResp.addContent(elTooltipImageMap); elResp.addContent(elMessage); elResp.addContent(elChartWidth); elResp.addContent(elChartHeight); return elResp; }
public static List<Element> handlePropertyName( String[] propertyNames, ServiceContext context, boolean freq, int maxRecords, String cswServiceSpecificConstraint, LuceneConfig luceneConfig) throws Exception { List<Element> domainValuesList = null; if (Log.isDebugEnabled(Geonet.CSW)) Log.debug( Geonet.CSW, "Handling property names '" + Arrays.toString(propertyNames) + "' with max records of " + maxRecords); for (int i = 0; i < propertyNames.length; i++) { if (i == 0) domainValuesList = new ArrayList<Element>(); // Initialize list of values element. Element listOfValues = null; // Generate DomainValues element Element domainValues = new Element("DomainValues", Csw.NAMESPACE_CSW); // FIXME what should be the type ??? domainValues.setAttribute("type", "csw:Record"); String property = propertyNames[i].trim(); // Set propertyName in any case. Element pn = new Element("PropertyName", Csw.NAMESPACE_CSW); domainValues.addContent(pn.setText(property)); GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME); SearchManager sm = gc.getSearchmanager(); IndexAndTaxonomy indexAndTaxonomy = sm.getNewIndexReader(null); try { GeonetworkMultiReader reader = indexAndTaxonomy.indexReader; BooleanQuery groupsQuery = (BooleanQuery) CatalogSearcher.getGroupsQuery(context); BooleanQuery query = null; // Apply CSW service specific constraint if (StringUtils.isNotEmpty(cswServiceSpecificConstraint)) { Query constraintQuery = CatalogSearcher.getCswServiceSpecificConstraintQuery( cswServiceSpecificConstraint, luceneConfig); query = new BooleanQuery(); BooleanClause.Occur occur = LuceneUtils.convertRequiredAndProhibitedToOccur(true, false); query.add(groupsQuery, occur); query.add(constraintQuery, occur); } else { query = groupsQuery; } List<Pair<String, Boolean>> sortFields = Collections.singletonList(Pair.read(Geonet.SearchResult.SortBy.RELEVANCE, true)); Sort sort = LuceneSearcher.makeSort(sortFields, context.getLanguage(), false); CachingWrapperFilter filter = null; Pair<TopDocs, Element> searchResults = LuceneSearcher.doSearchAndMakeSummary( maxRecords, 0, maxRecords, context.getLanguage(), null, reader, query, filter, sort, null, false, false, false, false // Scoring is useless for GetDomain operation ); TopDocs hits = searchResults.one(); try { // Get mapped lucene field in CSW configuration String indexField = CatalogConfiguration.getFieldMapping().get(property.toLowerCase()); if (indexField != null) property = indexField; // check if params asked is in the index using getFieldNames ? FieldInfos fi = new SlowCompositeReaderWrapper(reader).getFieldInfos(); if (fi.fieldInfo(property) == null) continue; boolean isRange = false; if (CatalogConfiguration.getGetRecordsRangeFields().contains(property)) isRange = true; if (isRange) listOfValues = new Element("RangeOfValues", Csw.NAMESPACE_CSW); else listOfValues = new Element("ListOfValues", Csw.NAMESPACE_CSW); Set<String> fields = new HashSet<String>(); fields.add(property); fields.add("_isTemplate"); // parse each document in the index String[] fieldValues; SortedSet<String> sortedValues = new TreeSet<String>(); HashMap<String, Integer> duplicateValues = new HashMap<String, Integer>(); for (int j = 0; j < hits.scoreDocs.length; j++) { DocumentStoredFieldVisitor selector = new DocumentStoredFieldVisitor(fields); reader.document(hits.scoreDocs[j].doc, selector); Document doc = selector.getDocument(); // Skip templates and subTemplates String[] isTemplate = doc.getValues("_isTemplate"); if (isTemplate[0] != null && !isTemplate[0].equals("n")) continue; // Get doc values for specified property fieldValues = doc.getValues(property); if (fieldValues == null) continue; addtoSortedSet(sortedValues, fieldValues, duplicateValues); } SummaryComparator valuesComparator = new SummaryComparator(SortOption.FREQUENCY, Type.STRING, context.getLanguage(), null); TreeSet<Map.Entry<String, Integer>> sortedValuesFrequency = new TreeSet<Map.Entry<String, Integer>>(valuesComparator); sortedValuesFrequency.addAll(duplicateValues.entrySet()); if (freq) return createValuesByFrequency(sortedValuesFrequency); else listOfValues.addContent(createValuesElement(sortedValues, isRange)); } finally { // any children means that the catalog was unable to determine // anything about the specified parameter if (listOfValues != null && listOfValues.getChildren().size() != 0) domainValues.addContent(listOfValues); // Add current DomainValues to the list domainValuesList.add(domainValues); } } finally { sm.releaseIndexReader(indexAndTaxonomy); } } return domainValuesList; }