@Override public String toString() { return getName(); } }
@Override public boolean isAutoSave(Presentation p) { p = getPresentation(p.getId()); return p != null && BooleanUtils.isTrue(p.getAutoSave()); }
@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())); } }
protected Map<UUID, Presentation> copyPresentations(User fromUser, User toUser) { Map<UUID, Presentation> presentationMap = new HashMap<>(); try (Transaction tx = persistence.createTransaction()) { EntityManager em = persistence.getEntityManager(); // delete existing Query delete = em.createQuery("delete from sec$Presentation p where p.user.id = ?1"); delete.setParameter(1, toUser.getId()); delete.executeUpdate(); // copy settings TypedQuery<Presentation> selectQuery = em.createQuery( "select p from sec$Presentation p where p.user.id = ?1", Presentation.class); selectQuery.setParameter(1, fromUser.getId()); List<Presentation> presentations = selectQuery.getResultList(); for (Presentation presentation : presentations) { Presentation newPresentation = metadata.create(Presentation.class); newPresentation.setUser(toUser); newPresentation.setComponentId(presentation.getComponentId()); newPresentation.setAutoSave(presentation.getAutoSave()); newPresentation.setName(presentation.getName()); newPresentation.setXml(presentation.getXml()); presentationMap.put(presentation.getId(), newPresentation); em.persist(newPresentation); } tx.commit(); return presentationMap; } }
protected void commit() { Presentations presentations = component.getPresentations(); Document doc = DocumentHelper.createDocument(); doc.setRootElement(doc.addElement("presentation")); component.saveSettings(doc.getRootElement()); String xml = Dom4j.writeDocument(doc, false); presentation.setXml(xml); presentation.setName(nameField.getValue()); presentation.setAutoSave(autoSaveField.getValue()); presentation.setDefault(defaultField.getValue()); User user = sessionSource.getUserSession().getCurrentOrSubstitutedUser(); boolean userOnly = !allowGlobalPresentations || !BooleanUtils.isTrue(globalField.getValue()); presentation.setUser(userOnly ? user : null); if (log.isTraceEnabled()) { log.trace(String.format("XML: %s", Dom4j.writeDocument(doc, true))); } if (isNew) { presentations.add(presentation); } else { presentations.modify(presentation); } presentations.commit(); addCloseListener(e -> { if (isNew) { component.applyPresentation(presentation.getId()); } }); }
autoSaveField.setValue(BooleanUtils.isTrue(presentation.getAutoSave())); root.addComponent(autoSaveField); defaultField.setValue(presentation.getId().equals(component.getDefaultPresentationId())); root.addComponent(defaultField); globalField = new CheckBox(); globalField.setCaption(messages.getMainMessage("PresentationsEditor.global")); globalField.setValue(!isNew && presentation.getUser() == null); root.addComponent(globalField);
@Override public Object getDefaultPresentationId() { if (presentations == null) { return null; } Presentation def = presentations.getDefault(); return def == null ? null : def.getId(); }
@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 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 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; } }
@Override public void actionPerform(Component component) { tableImpl.hidePresentationsPopup(); Presentation presentation = metadata.create(Presentation.class); presentation.setComponentId(ComponentsHelper.getComponentPath(table)); openEditor(presentation); } }
protected boolean validate() { Presentations presentations = component.getPresentations(); //check that name is empty if (StringUtils.isEmpty(nameField.getValue())) { App.getInstance().getWindowManager().showNotification( messages.getMainMessage("PresentationsEditor.error"), messages.getMainMessage("PresentationsEditor.error.nameRequired"), Frame.NotificationType.HUMANIZED); return false; } //check that name is unique final Presentation pres = presentations.getPresentationByName(nameField.getValue()); if (pres != null && !pres.equals(presentation)) { App.getInstance().getWindowManager().showNotification( messages.getMainMessage("PresentationsEditor.error"), messages.getMainMessage("PresentationsEditor.error.nameAlreadyExists"), Frame.NotificationType.HUMANIZED); return false; } return true; }
@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 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 defaultPresentationChanged(Presentations presentations, Object oldPresentationId) { if (presentationsMenuMap != null) { if (oldPresentationId != null) { if (oldPresentationId instanceof Presentation) oldPresentationId = ((Presentation) oldPresentationId).getId(); com.vaadin.ui.MenuBar.MenuItem lastMenuItem = presentationsMenuMap.get(oldPresentationId); if (lastMenuItem != null) removeDefaultItemStyle(lastMenuItem); } Presentation defaultPresentation = presentations.getDefault(); if (defaultPresentation != null) { com.vaadin.ui.MenuBar.MenuItem menuItem = presentationsMenuMap.get(defaultPresentation.getId()); if (menuItem != null) setDefaultItemStyle(menuItem); } } } });
@Override public Presentation getPresentationByName(String name) { for (Presentation p : presentations.values()) { if (name.equalsIgnoreCase(p.getName())) { return p; } } return null; }