@Override public void setCurrent(Presentation p) { checkLoad(); if (p == null) { Object old = current; current = null; fireCurrentPresentationChanged(old); } else if (presentations.containsKey(p.getId())) { Object old = current; current = p; fireCurrentPresentationChanged(old); } else { throw new IllegalStateException(String.format("Invalid presentation: %s", p.getId())); } }
@Override public void commit() { if (!needToUpdate.isEmpty() || !needToRemove.isEmpty()) { DataService ds = AppBeans.get(DataService.NAME); CommitContext ctx = new CommitContext( Collections.unmodifiableSet(needToUpdate), Collections.unmodifiableSet(needToRemove) ); Set<Entity> commitResult = ds.commit(ctx); commited(commitResult); clearCommitList(); firePresentationsSetChanged(); } }
@Override public String getCaption(Object id) { Presentation p = getPresentation(id); if (p != null) { return p.getName(); } return null; }
@Override public void add(Presentation p) { checkLoad(); presentations.put(p.getId(), p); if (PersistenceHelper.isNew(p)) { needToUpdate.add(p); if (BooleanUtils.isTrue(p.getDefault())) { def = p; } } firePresentationsSetChanged(); }
@Override public void setDefault(Presentation p) { checkLoad(); if (p == null) { Object old = def; def = null; fireDefaultPresentationChanged(old); } else if (presentations.containsKey(p.getId())) { Object old = def; if (def != null) { def.setDefault(false); } p.setDefault(true); def = p; fireDefaultPresentationChanged(old); } else { throw new IllegalStateException(String.format("Invalid presentation: %s", p.getId())); } }
@Override public void modify(Presentation p) { checkLoad(); if (presentations.containsKey(p.getId())) { needToUpdate.add(p); if (BooleanUtils.isTrue(p.getDefault())) { setDefault(p); } else if (def != null && def.getId().equals(p.getId())) { setDefault(null); } } else { throw new IllegalStateException(String.format("Invalid presentation: %s", p.getId())); } }
@Override public void setSettings(Presentation p, Element e) { p = getPresentation(p.getId()); if (p != null) { p.setXml(Dom4j.writeDocument(e.getDocument(), false)); modify(p); } }
@Override public Presentation getCurrent() { checkLoad(); return current; }
public void commited(Set<Entity> entities) { for (Entity entity : entities) { if (entity.equals(def)) setDefault((Presentation) entity); else if (entity.equals(current)) current = (Presentation) entity; if (presentations.containsKey(entity.getId())) { presentations.put(entity.getId(), (Presentation) entity); } } }
@Override public void loadPresentations() { if (isUsePresentations()) { presentations = new PresentationsImpl(this); setTablePresentations(new TablePresentations(this)); } else { throw new UnsupportedOperationException("Component doesn't use presentations"); } }
@Override public void remove(Presentation p) { checkLoad(); if (presentations.remove(p.getId()) != null) { if (PersistenceHelper.isNew(p)) { needToUpdate.remove(p); } else { needToUpdate.remove(p); needToRemove.add(p); } if (p.equals(def)) { def = null; } if (p.equals(current)) { current = null; } firePresentationsSetChanged(); } }
@Override public Presentation getPresentation(Object id) { checkLoad(); return presentations.get(id); }
@Override public boolean isGlobal(Presentation p) { p = getPresentation(p.getId()); return p != null && !PersistenceHelper.isNew(p) && p.getUser() == null; }
@Override public Collection<Object> getPresentationIds() { checkLoad(); return Collections.unmodifiableCollection(presentations.keySet()); }
@Override public boolean isAutoSave(Presentation p) { p = getPresentation(p.getId()); return p != null && BooleanUtils.isTrue(p.getAutoSave()); }
@Override public Element getSettings(Presentation p) { p = getPresentation(p.getId()); if (p != null) { Document doc; if (!StringUtils.isEmpty(p.getXml())) { doc = Dom4j.readDocument(p.getXml()); } else { doc = DocumentHelper.createDocument(); doc.setRootElement(doc.addElement("presentation")); } return doc.getRootElement(); } else { return null; } }