private URI updatePortfolio(String name, DocumentVisibility visibility, PortfolioDocument doc) { doc.getPortfolio().setName(name); doc.setVisibility(visibility); doc = data().getPortfolioMaster().update(doc); data().setPortfolio(doc); URI uri = WebPortfolioResource.uri(data()); return uri; }
// ------------------------------------------------------------------------- @Override public PortfolioDocument correct(final PortfolioDocument document) { ArgumentChecker.notNull(document, "document"); ArgumentChecker.notNull(document.getPortfolio(), "document.portfolio"); ArgumentChecker.notNull(document.getUniqueId(), "document.uniqueId"); URI uri = DataPortfolioResource.uriVersion(getBaseUri(), document.getUniqueId()); return accessRemote(uri).put(PortfolioDocument.class, document); }
@DELETE @Produces(MediaType.APPLICATION_JSON) public Response deleteJSON() { PortfolioDocument doc = data().getPortfolio(); if (doc.isLatest()) { data().getPortfolioMaster().remove(doc.getUniqueId()); } return Response.ok().build(); }
private void setDocumentId( final PortfolioDocument document, final PortfolioDocument clonedDoc, final UniqueId uniqueId) { document.getPortfolio().setUniqueId(uniqueId); clonedDoc.getPortfolio().setUniqueId(uniqueId); document.setUniqueId(uniqueId); clonedDoc.setUniqueId(uniqueId); }
/** * Creates the output root data. * * @return the output root data, not null */ protected FlexiBean createRootData() { FlexiBean out = super.createRootData(); PortfolioDocument doc = data().getPortfolio(); out.put("portfolioDoc", doc); out.put("portfolio", doc.getPortfolio()); out.put("childNodes", doc.getPortfolio().getRootNode().getChildNodes()); out.put("deleted", !doc.isLatest()); out.put("rootNode", doc.getPortfolio().getRootNode()); return out; }
private PortfolioDocument clonePortfolioDocument(PortfolioDocument document) { if (isCloneResults()) { PortfolioDocument clone = JodaBeanUtils.clone(document); ManageablePortfolio portfolioClone = JodaBeanUtils.clone(document.getPortfolio()); portfolioClone.setRootNode(clonePortfolioNode(portfolioClone.getRootNode())); clone.setPortfolio(portfolioClone); return clone; } else { return document; } }
private FlexiBean createPortfolioData() { PortfolioDocument doc = data().getPortfolio(); PositionSearchRequest positionSearch = new PositionSearchRequest(); positionSearch.setPositionObjectIds(doc.getPortfolio().getRootNode().getPositionIds()); PositionSearchResult positionsResult = data().getPositionMaster().search(positionSearch); FlexiBean out = createRootData(); out.put("positionsResult", positionsResult); out.put("positions", positionsResult.getPositions()); return out; }
// ------------------------------------------------------------------------- @DELETE @Produces(MediaType.TEXT_HTML) public Response deleteHTML() { PortfolioDocument doc = data().getPortfolio(); if (doc.isLatest() == false) { return Response.status(Status.FORBIDDEN).entity(getHTML()).build(); } data().getPortfolioMaster().remove(doc.getUniqueId()); URI uri = WebPortfolioResource.uri(data()); return Response.seeOther(uri).build(); }
@Override public void remove(ObjectIdentifiable objectIdentifiable) { ArgumentChecker.notNull(objectIdentifiable, "objectIdentifiable"); PortfolioDocument storedDocument = _store.remove(objectIdentifiable.getObjectId()); if (storedDocument == null) { throw new DataNotFoundException("Portfolio not found " + objectIdentifiable); } removeNodes(storedDocument.getPortfolio().getRootNode()); _changeManager.entityChanged( ChangeType.REMOVED, objectIdentifiable.getObjectId(), null, null, Instant.now()); updateCaches(storedDocument, null); }
@PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.APPLICATION_JSON) public Response putJSON(@FormParam("name") String name, @FormParam("hidden") Boolean isHidden) { PortfolioDocument doc = data().getPortfolio(); if (doc.isLatest() == false) { return Response.status(Status.FORBIDDEN).entity(getHTML()).build(); } name = StringUtils.trimToNull(name); DocumentVisibility visibility = BooleanUtils.isTrue(isHidden) ? DocumentVisibility.HIDDEN : DocumentVisibility.VISIBLE; updatePortfolio(name, visibility, doc); return Response.ok().build(); }
// ------------------------------------------------------------------------- @Override public PortfolioDocument add(final PortfolioDocument document) { ArgumentChecker.notNull(document, "document"); ArgumentChecker.notNull(document.getPortfolio(), "document.portfolio"); ArgumentChecker.notNull(document.getPortfolio().getRootNode(), "document.portfolio.rootNode"); URI uri = DataPortfoliosResource.uri(getBaseUri(), null); return accessRemote(uri).post(PortfolioDocument.class, document); }
// ------------------------------------------------------------------------- @PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.TEXT_HTML) public Response putHTML(@FormParam("name") String name, @FormParam("hidden") Boolean isHidden) { PortfolioDocument doc = data().getPortfolio(); if (doc.isLatest() == false) { return Response.status(Status.FORBIDDEN).entity(getHTML()).build(); } name = StringUtils.trimToNull(name); DocumentVisibility visibility = BooleanUtils.isTrue(isHidden) ? DocumentVisibility.HIDDEN : DocumentVisibility.VISIBLE; if (name == null) { FlexiBean out = createRootData(); out.put("err_nameMissing", true); String html = getFreemarker().build("portfolios/portfolio-update.ftl", out); return Response.ok(html).build(); } URI uri = updatePortfolio(name, visibility, doc); return Response.seeOther(uri).build(); }
// ------------------------------------------------------------------------- @Override protected void updateCaches( ObjectIdentifiable replacedObject, PortfolioDocument updatedDocument) { PortfolioDocument replacedPortfolio = (PortfolioDocument) replacedObject; if (replacedPortfolio != null) { Set<PortfolioDocument> docsWithName = _portfoliosByName.get(replacedPortfolio.getValue().getName()); if (docsWithName != null) { docsWithName.remove(replacedPortfolio); } } if (updatedDocument != null) { Set<PortfolioDocument> docsWithName = _portfoliosByName.get(updatedDocument.getValue().getName()); if (docsWithName == null) { docsWithName = new HashSet<PortfolioDocument>(); _portfoliosByName.put(updatedDocument.getValue().getName(), docsWithName); } docsWithName.add(updatedDocument); } }
@Override public PortfolioDocument add(PortfolioDocument document) { ArgumentChecker.notNull(document, "document"); ArgumentChecker.notNull(document.getPortfolio(), "document.portfolio"); final ObjectId objectId = _objectIdSupplier.get(); final UniqueId uniqueId = objectId.atVersion(""); final Instant now = Instant.now(); final PortfolioDocument clonedDoc = clonePortfolioDocument(document); setDocumentId(document, clonedDoc, uniqueId); setVersionTimes(document, clonedDoc, now, null, now, null); _store.put(objectId, clonedDoc); storeNodes( clonedDoc.getPortfolio().getRootNode(), document.getPortfolio().getRootNode(), uniqueId, null); _changeManager.entityChanged( ChangeType.ADDED, objectId, document.getVersionFromInstant(), document.getVersionToInstant(), now); updateCaches(null, clonedDoc); return document; }
/** * Stores the portfolio. * * @param portfolio the portfolio, not null */ private void storePortfolio(final ManageablePortfolio portfolio) { final PortfolioMaster portfolioMaster = getToolContext().getPortfolioMaster(); final PortfolioSearchRequest req = new PortfolioSearchRequest(); req.setName(portfolio.getName()); final PortfolioSearchResult result = portfolioMaster.search(req); switch (result.getDocuments().size()) { case 0: s_logger.info("Creating new portfolio"); portfolioMaster.add(new PortfolioDocument(portfolio)); break; case 1: final UniqueId previousId = result.getDocuments().get(0).getUniqueId(); s_logger.info("Updating portfolio {}", previousId); portfolio.setUniqueId(previousId); final PortfolioDocument document = new PortfolioDocument(portfolio); document.setUniqueId(previousId); portfolioMaster.update(document); break; default: throw new OpenGammaRuntimeException("Multiple portfolios matching " + req); } }
private void setVersionTimes( PortfolioDocument document, final PortfolioDocument clonedDoc, final Instant versionFromInstant, final Instant versionToInstant, final Instant correctionFromInstant, final Instant correctionToInstant) { clonedDoc.setVersionFromInstant(versionFromInstant); document.setVersionFromInstant(versionFromInstant); clonedDoc.setVersionToInstant(versionToInstant); document.setVersionToInstant(versionToInstant); clonedDoc.setCorrectionFromInstant(correctionFromInstant); document.setCorrectionFromInstant(correctionFromInstant); clonedDoc.setCorrectionToInstant(correctionToInstant); document.setCorrectionToInstant(correctionToInstant); }
// ------------------------------------------------------------------------- @Override protected void validateDocument(PortfolioDocument document) { ArgumentChecker.notNull(document, "document"); ArgumentChecker.notNull(document.getPortfolio(), "document.portfolio"); }
@Override public PortfolioDocument update(PortfolioDocument document) { ArgumentChecker.notNull(document, "document"); ArgumentChecker.notNull(document.getUniqueId(), "document.uniqueId"); ArgumentChecker.notNull(document.getPortfolio(), "document.portfolio"); final UniqueId uniqueId = document.getUniqueId(); final Instant now = Instant.now(); final PortfolioDocument storedDocument = _store.get(uniqueId.getObjectId()); if (storedDocument == null) { throw new DataNotFoundException("Portfolio not found: " + uniqueId); } final PortfolioDocument clonedDoc = clonePortfolioDocument(document); removeNodes(storedDocument.getPortfolio().getRootNode()); setVersionTimes(document, clonedDoc, now, null, now, null); if (_store.replace(uniqueId.getObjectId(), storedDocument, clonedDoc) == false) { throw new IllegalArgumentException("Concurrent modification"); } storeNodes( clonedDoc.getPortfolio().getRootNode(), document.getPortfolio().getRootNode(), uniqueId, null); _changeManager.entityChanged( ChangeType.CHANGED, document.getObjectId(), document.getVersionFromInstant(), document.getVersionToInstant(), now); updateCaches(storedDocument, clonedDoc); return document; }