@Test public void testFindCollections() throws IOException, ParserConfigurationException, SAXException, JSONException { URL res = CollectionsDecoratorTest.class.getResource("rels-ext.xml"); Document document = XMLUtils.parseDocument(res.openStream(), true); FedoraAccess fa = EasyMock.createMock(FedoraAccess.class); EasyMock.expect(fa.getRelsExt("uuid:c32e0540-3e38-11e2-8227-5ef3fc9bb22f")).andReturn(document).anyTimes(); EasyMock.replay(fa); CollectionsDecorator coll = new CollectionsDecorator(); coll.fedoraAccess = fa; JSONObject jsonObject = new JSONObject(); jsonObject.put("pid", "uuid:c32e0540-3e38-11e2-8227-5ef3fc9bb22f"); Map<String, Object> runtimeContext = new HashMap<String, Object>(); coll.decorate(jsonObject, runtimeContext); Assert.assertTrue(jsonObject.has("collections")); Assert.assertTrue(jsonObject.getJSONArray("collections").length() ==1); }
@Override public void decorate(JSONObject jsonObject, Map<String, Object> runtimeContext) { try { if (jsonObject.has("pid")) { String pid = jsonObject.getString("pid"); if (!PIDSupport.isComposedPID(pid)) { Document relsExtDoc = RELSEXTDecoratorUtils.getRELSEXTPidDocument(pid, context, this.fedoraAccess); List<Element> collections = CollectionUtils.findCollectionsElements(relsExtDoc); if (!collections.isEmpty()) { JSONArray collectionsJSON = new JSONArray(); for (Element colElm : collections) { String collectionPid = colElm.getAttributeNS(FedoraNamespaces.RDF_NAMESPACE_URI, "resource"); if (StringUtils.isAnyString(collectionPid)) { try { PIDParser pidParser = new PIDParser(collectionPid); if (collectionPid.startsWith(PIDParser.INFO_FEDORA_PREFIX)) { pidParser.disseminationURI(); } else { pidParser.objectPid(); } collectionsJSON.put(pidParser.getObjectPid()); } catch (LexerException e) { LOGGER.severe("cannot parse collection pid " + collectionPid); LOGGER.log(Level.SEVERE, e.getMessage(), e); } } } jsonObject.put("collections", collectionsJSON); } } } } catch (IOException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); throw new GenericApplicationException(e.getMessage()); } catch (JSONException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); throw new GenericApplicationException(e.getMessage()); } }
CollectionsDecorator extends AbstractItemDecorator { @Override public void decorate(JSONObject jsonObject, Map<String, Object> runtimeContext) { try { if (jsonObject.has("pid")) { String pid = jsonObject.getString("pid"); if (!PIDSupport.isComposedPID(pid)) { Document relsExtDoc = RELSEXTDecoratorUtils.getRELSEXTPidDocument(pid, context, this.fedoraAccess); List<Element> collections = CollectionUtils.findCollectionsElements(relsExtDoc); if (!collections.isEmpty()) { JSONArray collectionsJSON = new JSONArray(); for (Element colElm : collections) { String collectionPid = colElm.getAttributeNS(FedoraNamespaces.RDF_NAMESPACE_URI, "resource"); if (StringUtils.isAnyString(collectionPid)) { try { PIDParser pidParser = new PIDParser(collectionPid); if (collectionPid.startsWith(PIDParser.INFO_FEDORA_PREFIX)) { pidParser.disseminationURI(); } else { pidParser.objectPid(); } collectionsJSON.put(pidParser.getObjectPid()); } catch (LexerException e) { LOGGER.severe("cannot parse collection pid " + collectionPid); LOGGER.log(Level.SEVERE, e.getMessage(), e); } } } jsonObject.put("collections", collectionsJSON); } } } } catch (IOException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); throw new GenericApplicationException(e.getMessage()); } catch (JSONException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); throw new GenericApplicationException(e.getMessage()); } } }
CollectionsDecorator extends AbstractItemDecorator { @Override public void decorate(JSONObject jsonObject, Map<String, Object> runtimeContext) { try { if (jsonObject.has("pid")) { String pid = jsonObject.getString("pid"); if (!PIDSupport.isComposedPID(pid)) { Document relsExtDoc = RELSEXTDecoratorUtils.getRELSEXTPidDocument(pid, context, this.fedoraAccess); List<Element> collections = CollectionUtils.findCollectionsElements(relsExtDoc); if (!collections.isEmpty()) { JSONArray collectionsJSON = new JSONArray(); for (Element colElm : collections) { String collectionPid = colElm.getAttributeNS(FedoraNamespaces.RDF_NAMESPACE_URI, "resource"); if (StringUtils.isAnyString(collectionPid)) { try { PIDParser pidParser = new PIDParser(collectionPid); if (collectionPid.startsWith(PIDParser.INFO_FEDORA_PREFIX)) { pidParser.disseminationURI(); } else { pidParser.objectPid(); } collectionsJSON.put(pidParser.getObjectPid()); } catch (LexerException e) { LOGGER.severe("cannot parse collection pid " + collectionPid); LOGGER.log(Level.SEVERE, e.getMessage(), e); } } } jsonObject.put("collections", collectionsJSON); } } } } catch (IOException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); throw new GenericApplicationException(e.getMessage()); } catch (JSONException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); throw new GenericApplicationException(e.getMessage()); } }  }
CollectionsDecorator extends AbstractItemDecorator { @Override public void decorate(JSONObject jsonObject, Map<String, Object> runtimeContext) { try { if (jsonObject.has("pid")) { String pid = jsonObject.getString("pid"); if (!PIDSupport.isComposedPID(pid)) { Document relsExtDoc = RELSEXTDecoratorUtils.getRELSEXTPidDocument(pid, context, this.fedoraAccess); List<Element> collections = CollectionUtils.findCollectionsElements(relsExtDoc); if (!collections.isEmpty()) { JSONArray collectionsJSON = new JSONArray(); for (Element colElm : collections) { String collectionPid = colElm.getAttributeNS(FedoraNamespaces.RDF_NAMESPACE_URI, "resource"); if (StringUtils.isAnyString(collectionPid)) { try { PIDParser pidParser = new PIDParser(collectionPid); if (collectionPid.startsWith(PIDParser.INFO_FEDORA_PREFIX)) { pidParser.disseminationURI(); } else { pidParser.objectPid(); } collectionsJSON.put(pidParser.getObjectPid()); } catch (LexerException e) { LOGGER.severe("cannot parse collection pid " + collectionPid); LOGGER.log(Level.SEVERE, e.getMessage(), e); } } } jsonObject.put("collections", collectionsJSON); } } } } catch (IOException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); throw new GenericApplicationException(e.getMessage()); } catch (JSONException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); throw new GenericApplicationException(e.getMessage()); } }  @Override String getKey(); @Override void decorate(JSONObject jsonObject, Map<String, Object> runtimeContext); @Override boolean apply(JSONObject jsonObject, String context); }
CollectionsDecorator extends AbstractItemDecorator { @Override public void decorate(JSONObject jsonObject, Map<String, Object> runtimeContext) { try { if (jsonObject.has("pid")) { String pid = jsonObject.getString("pid"); if (!PIDSupport.isComposedPID(pid)) { Document relsExtDoc = RELSEXTDecoratorUtils.getRELSEXTPidDocument(pid, context, this.fedoraAccess); List<Element> collections = CollectionUtils.findCollectionsElements(relsExtDoc); if (!collections.isEmpty()) { JSONArray collectionsJSON = new JSONArray(); for (Element colElm : collections) { String collectionPid = colElm.getAttributeNS(FedoraNamespaces.RDF_NAMESPACE_URI, "resource"); if (StringUtils.isAnyString(collectionPid)) { try { PIDParser pidParser = new PIDParser(collectionPid); if (collectionPid.startsWith(PIDParser.INFO_FEDORA_PREFIX)) { pidParser.disseminationURI(); } else { pidParser.objectPid(); } collectionsJSON.put(pidParser.getObjectPid()); } catch (LexerException e) { LOGGER.severe("cannot parse collection pid " + collectionPid); LOGGER.log(Level.SEVERE, e.getMessage(), e); } } } jsonObject.put("collections", collectionsJSON); } } } } catch (IOException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); throw new GenericApplicationException(e.getMessage()); } catch (JSONException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); throw new GenericApplicationException(e.getMessage()); } }  @Override String getKey(); @Override void decorate(JSONObject jsonObject, Map<String, Object> runtimeContext); @Override boolean apply(JSONObject jsonObject, String context); static final Logger LOGGER; static final String COLLECTIONS_DECORATOR_KEY; }