Java tutorial
/* * Copyright (c) 2013 Haulmont Technology Ltd. All Rights Reserved. * Haulmont Technology proprietary and confidential. * Use is subject to license terms. */ package com.haulmont.ext.web.ui.Call; import com.haulmont.cuba.core.app.DataService; import com.haulmont.cuba.core.entity.Entity; import com.haulmont.cuba.core.global.*; import com.haulmont.cuba.gui.ServiceLocator; import com.haulmont.cuba.gui.WindowManager; import com.haulmont.cuba.gui.components.*; import com.haulmont.cuba.gui.data.CollectionDatasource; import com.haulmont.cuba.gui.data.Datasource; import com.haulmont.cuba.gui.data.DsContext; import com.haulmont.cuba.gui.data.impl.CollectionDatasourceImpl; import com.haulmont.cuba.gui.data.impl.CollectionDsListenerAdapter; import com.haulmont.cuba.gui.data.impl.CollectionPropertyDatasourceImpl; import com.haulmont.cuba.security.entity.Role; import com.haulmont.cuba.security.entity.User; import com.haulmont.cuba.web.App; import com.haulmont.cuba.web.gui.components.WebButton; import com.haulmont.cuba.web.gui.components.WebVBoxLayout; import com.haulmont.docflow.core.app.CardService; import com.haulmont.docflow.core.app.NumerationService; import com.haulmont.docflow.core.entity.Doc; import com.haulmont.docflow.core.entity.DocKind; import com.haulmont.docflow.core.entity.NumeratorType; import com.haulmont.docflow.web.DocflowAppWindow; import com.haulmont.ext.core.entity.Call; import com.haulmont.ext.web.ui.Call.CallAccessData; import com.haulmont.taskman.web.ui.common.CardProjectsFrame; import com.haulmont.taskman.web.ui.common.CardRelationsFrame; import com.haulmont.taskman.web.ui.common.CardSecurityFrame; import com.haulmont.taskman.web.ui.common.CardTreeFrame; import com.haulmont.workflow.core.app.WfUtils; import com.haulmont.workflow.core.entity.*; import com.haulmont.workflow.web.ui.base.AbstractCardEditor; import com.haulmont.workflow.web.ui.base.CardProcFrame; import com.haulmont.workflow.web.ui.base.CardRolesFrame; import com.haulmont.workflow.web.ui.base.action.ActionsFrame; import org.apache.commons.lang.BooleanUtils; import org.apache.commons.lang.StringUtils; import javax.inject.Inject; import java.util.*; /** * Created by mahdi on 12/11/13. */ public class CallEditor extends AbstractCardEditor { protected CallAccessData accessData; protected boolean removedComment = false; protected boolean hierarchyTabInited = false; protected Boolean isTabComment; protected boolean historyTabInited = false; protected boolean versionsTabInitialized = false; @Inject protected CollectionDatasource<CardProc, UUID> cardProcDs; @Inject protected Datasource cardDs; @Inject protected PickerField parentCard; @Inject protected WebVBoxLayout startProc; @Inject protected CardProcFrame cardProcFrame; @Inject protected WebVBoxLayout buttonStart; @Inject protected CollectionPropertyDatasourceImpl cardProjectsDs; @Inject protected DataService cuba_DataService; @Inject protected NumerationService docflow_NumerationService; @Inject protected TextField telephoneNumber, name, callDescription; @Inject protected ActionsField extClient; @Inject protected LookupField priority; @Inject protected DateField finishDatePlan; public CallEditor(IFrame frame) { super(frame); } @Override protected boolean isCommentVisible() { return false; } @Override public void init(Map<String, Object> params) { super.init(params); cardDs = getDsContext().get("CardDs"); //? ? ? ? ?? telephoneNumber = getComponent("telephoneNumber"); name = getComponent("name"); extClient = getComponent("extClient"); priority = getComponent("priority"); finishDatePlan = getComponent("finishDatePlan"); callDescription = getComponent("callDescription"); addListener(new CloseListener() { public void windowClosed(String actionId) { App.getInstance().getAppWindow().getFoldersPane().reloadAppFolders(); } }); /* // ? , ? ?? // final HierarchicalDatasource commentDs = ((IFrame) getComponent("cardCommentFrame")).getDsContext().get("commentDs"); if (commentDs != null) { /* ??, ? ? ? ?, ??? ? */ /* commentDs.addListener(new CollectionDsListenerAdapter() { public void collectionChanged(CollectionDatasource ds, Operation operation) { for (Tabsheet.Tab tab : ((Tabsheet) getComponent("tabsheet")).getTabs()) { if ("cardCommentTab".equals(tab.getName()) && !removedComment) { int count = commentDs.size(); if (count > 0) { tab.setCaption(getMessage("cardCommentTab") + " (" + count + ")"); } else { tab.setCaption(getMessage("cardCommentTab")); } } } } }); // ? commentDs.refresh(); } */ // ??, ? //? //? ? ?, //??? ? // attachmentsDs.addListener(new CollectionDsListenerAdapter() { @Override public void collectionChanged(CollectionDatasource ds, Operation operation) { for (Tabsheet.Tab tab : ((Tabsheet) getComponent("tabsheet")).getTabs()) { if (tab.getName() != null && tab.getName().equals("attachmentsTab")) { if (ds.getItemIds().size() > 0) tab.setCaption(getMessage("attachments") + " (" + ds.getItemIds().size() + ")"); else tab.setCaption(getMessage("attachments")); } } } @Override public void itemChanged(Datasource ds, Entity prevItem, Entity item) { for (Action action : attachmentsTable.getActions()) { if (((Card) getItem()).getJbpmProcessId() != null && !"actions.Create".equals(action.getId()) && !"actions.Copy".equals(action.getId())) action.setEnabled(item instanceof CardAttachment); } } }); // ??, ? //? //? , //??? ? // cardProjectsDs.addListener(new CollectionDsListenerAdapter() { public void collectionChanged(CollectionDatasource ds, Operation operation) { Tabsheet tabsheet = getComponent("tabsheet"); for (Tabsheet.Tab tab : tabsheet.getTabs()) { if ("cardProjectsTab".equals(tab.getName())) { if (cardProjectsDs.size() > 0) tab.setCaption(getMessage("cardProjectsTab") + " (" + cardProjectsDs.size() + ")"); else tab.setCaption(getMessage("cardProjectsTab")); } } } }); // ??, ? // ? "?" // ?? ? /*parentCard.removeAction(parentCard.getAction(PickerField.LookupAction.NAME)); // ? parentCard.addAction(new PickerField.LookupAction(parentCard) { public void actionPerform(Component component) { if (DocTypeSelector.isNeeded("read")) { //? //(, , , ) final DocTypeSelector docTypeSelector = new DocTypeSelector("select", "read"); docTypeSelector.addListener(new com.vaadin.ui.Window.CloseListener() { public void windowClose(com.vaadin.ui.Window.CloseEvent e) { openCardLookup(docTypeSelector.getEntityName()); } }); docTypeSelector.show(); } else { openCardLookup((getItem()).getMetaClass().getName()); } } }); */ // ? // ? //?? // parentCard.addAction(new PickerField.OpenAction(parentCard)); getDsContext().addListener(new DsContext.CommitListener() { // ??, ? //? ?? public void beforeCommit(CommitContext<Entity> context) { // ?? Doc doc = (Doc) getItem(); //? ?? ?? if ((PersistenceHelper.isNew(doc)) //? // ? && StringUtils.isBlank(doc.getNumber()) //? ? ?? //?? - // ? && NumeratorType.ON_COMMIT.equals(doc.getDocKind().getNumeratorType())) { // NumerationService ns = ServiceLocator.lookup(NumerationService.NAME); // ?? String num = ns.getNextNumber(doc); if (num != null) //? ? //?? doc.setNumber(num); } CardSecurityFrame securityFrame = getComponent("securityFrame"); if (securityFrame != null) { context.getCommitInstances().addAll(securityFrame.getToCommit()); } } @Override public void afterCommit(CommitContext<Entity> context, Set<Entity> result) { Set<Entity> rolesToCommit = new HashSet<Entity>(); for (Entity entry : result) { if (entry instanceof CardRole) { rolesToCommit.add(entry); } } CardRolesFrame frame = (CardRolesFrame) getComponent("cardProcFrame.cardRolesFrame"); if (frame != null) { frame.getActorActionsFieldsMap().clear(); CollectionDatasourceImpl tmpCardRolesDs = frame.getDsContext().get("tmpCardRolesDs"); if (tmpCardRolesDs != null) tmpCardRolesDs.committed(rolesToCommit); } String template = TemplateHelper.processTemplate(getMessage("docEditor.caption"), Collections.<String, Object>singletonMap("item", getItem())); App.getInstance().getWindowManager().setCurrentWindowCaption((Window) getFrame(), template, null); } }); //? lazy- initLazyTabs(); isTabComment = BooleanUtils.isTrue((Boolean) params.get("isTabComment")); adjustForVersion(params); } private void callSetEditable(boolean key) { telephoneNumber.setEditable(key); name.setEditable(key); extClient.setEditable(key); priority.setEditable(key); finishDatePlan.setEditable(key); callDescription.setEditable(key); } @Override public void setItem(Entity item) { super.setItem(item); // ?? Call call = (Call) getItem(); User user = UserSessionProvider.getUserSession().getCurrentOrSubstitutedUser(); if (!WfUtils.isCardInState(call, null)) callSetEditable(false); LoadContext qtx = new LoadContext(CardRole.class); qtx.setQueryString("select cr from wf$CardRole cr where cr.card.id =:card").addParameter("card", call); qtx.setView("card-edit"); List<CardRole> cardRole = ServiceLocator.getDataService().loadList(qtx); for (CardRole cR : cardRole) { if ((cR.getCode().equals("receiver") && cR.getUser().getLogin().equals(user.getLogin()) && WfUtils.isCardInState(call, "Zvonok_naznachen")) || (cR.getCode().equals("office_manager") && cR.getUser().getLogin().equals(user.getLogin()) && (WfUtils.isCardInState(call, "Zvonok_otklonen_ispolnitelem") || WfUtils.isCardInState(call, "Zvonok_prinyat")))) { callSetEditable(true); } } if (WfUtils.isCardInState(call, "Obrabotan")) callSetEditable(false); initAttachments(call); // ? if (PersistenceHelper.isNew(call)) { //? ?? ? call.setCreator(UserSessionProvider.getUserSession().getUser()); //? ?? ? call.setSubstitutedCreator(UserSessionProvider.getUserSession().getCurrentOrSubstitutedUser()); //? : LoadContext ctx = new LoadContext(DocKind.class); ctx.setQueryString("select dk from df$DocKind dk where dk.docType = " + "(select dt from df$DocType dt where dt.name = :typeName)") .addParameter("typeName", "ext$Call"); ctx.setView("edit"); DocKind docKind = cuba_DataService.load(ctx); call.setDocKind(docKind); //? call.setDateTime(TimeProvider.currentTimestamp()); //? , // " ?" if (NumeratorType.ON_CREATE.equals(call.getDocKind().getNumeratorType())) { // // ?? String num = docflow_NumerationService.getNextNumber(call); if (num != null) //? ? //?? call.setNumber(num); } //? "?" Card parentCard = ((Doc) item).getParentCard(); if (parentCard != null) { call.setParentCard(parentCard); } } if (WfUtils.isCardInState(call, "Obrabotan")) { telephoneNumber.setEditable(false); name.setEditable(false); extClient.setEditable(false); priority.setEditable(false); finishDatePlan.setEditable(false); } // ? deleteNotifications(call); // ((DocflowAppWindow) App.getInstance().getAppWindow()).reloadAppFolders(Collections.singletonList(call)); // ??, ? ? Proc process = call.getProc(); boolean isActiveProc = false; /* AbstractWfAccessData */ accessData = getContext().getParamValue("accessData"); if (!(accessData == null || accessData.getStartCardProcessEnabled())) isActiveProc = true; else if (process != null) for (UUID uuid : cardProcDs.getItemIds()) { CardProc cpt = cardProcDs.getItem(uuid); if (cpt.getProc().getId().equals(process.getId()) && cpt.getActive()) { isActiveProc = true; } } //? ?? ? if (!isActiveProc) { // ? , // ?? ? ?? startProc.setVisible(true); CollectionDatasource<Proc, UUID> procDs = cardProcFrame.getDsContext().get("procDs"); if (procDs.getState() == Datasource.State.VALID) { for (UUID uuid : procDs.getItemIds()) { final Proc proc = procDs.getItem(uuid); //? ? ?? Button button = new WebButton(); button.setWidth("200px"); button.setAction(new AbstractAction("start" + proc.getJbpmProcessKey()) { public void actionPerform(Component component) { startProcess(proc); } @Override public String getCaption() { return proc.getName(); } }); buttonStart.add(button); } } } if (isTabComment) ((Tabsheet) getComponent("tabsheet")).setTab("cardCommentTab"); } //? ? ? //?? private void startProcess(final Proc proc) { showOptionDialog(MessageProvider.getMessage(cardProcFrame.getClass(), "runProc.title"), String.format(MessageProvider.getMessage(cardProcFrame.getClass(), "runProc.msg"), proc.getName()), IFrame.MessageType.CONFIRMATION, new Action[] { new DialogAction(DialogAction.Type.YES) { @Override public void actionPerform(Component component) { CardProc cp = null; for (UUID uuid : cardProcDs.getItemIds()) { CardProc cpt = cardProcDs.getItem(uuid); if (cpt.getProc().getId().equals(proc.getId())) { cp = cpt; } } if (cp == null) { cp = new CardProc(); cp.setCard((Call) getItem()); cp.setProc(proc); cp.setActive(false); cp.setSortOrder(cardProcFrame.calculateSortOrder()); cardProcDs.addItem(cp); cardProcFrame.getCardRolesFrame().procChanged(cp.getProc()); cardProcFrame.getCardRolesFrame().initDefaultActors(cp.getProc()); } cardProcFrame.startProcess(cp); } }, new DialogAction(DialogAction.Type.NO) }); } @Override protected void initActionsFrame(Card card, ActionsFrame actionsFrame) { actionsFrame.initActions(card, isCommentVisible()); } private void openCardLookup(String entityName) { if (entityName == null) { return; } openLookup(entityName + ".browse", new Lookup.Handler() { public void handleLookup(Collection items) { if (items != null && items.size() > 0) { ((Card) getItem()).setParentCard((Card) items.iterator().next()); } } }, WindowManager.OpenType.THIS_TAB, Collections.<String, Object>singletonMap("exclItem", getItem())); } //? lazy- private void initLazyTabs() { Tabsheet tabsheet = getComponent("tabsheet"); accessData = getContext().getParamValue("accessData"); tabsheet.addListener(new Tabsheet.TabChangeListener() { public void tabChanged(Tabsheet.Tab newTab) { if ("cardProjectsTab".equals(newTab.getName())) { CardProjectsFrame cardProjectsFrame = getComponent("cardProjectsFrame"); cardProjectsFrame.init(); cardProjectsFrame.getComponent("add").setVisible(true); cardProjectsFrame.getComponent("delete").setVisible(true); if (!accessData.getNotVersion()) { cardProjectsFrame.setEditable(false); } } else if ("history".equals(newTab.getName())) { initHistoryTab(); } else if ("hierarchy".equals(newTab.getName())) { initHierarchyTab(); } else if ("securityTab".equals(newTab.getName())) { CardSecurityFrame securityFrame = getComponent("securityFrame"); securityFrame.init(); securityFrame.refreshDs(); securityFrame.setEditable(accessData.getNotVersion()); } } }); } private void initHistoryTab() { if (historyTabInited) { return; } Tabsheet tabsheet = getComponent("historyTabsheet"); tabsheet.addListener(new Tabsheet.TabChangeListener() { public void tabChanged(Tabsheet.Tab newTab) { if ("versionsTab".equals(newTab.getName())) { initVersionsTab(); } } }); historyTabInited = true; } protected void initVersionsTab() { if (versionsTabInitialized) return; Table versionsTable = getComponent("versionsTable"); OpenVersionAction openVersionAction = new OpenVersionAction(versionsTable); versionsTable.addAction(openVersionAction); versionsTabInitialized = true; } protected class OpenVersionAction extends AbstractAction { private final Table versionsTable; public OpenVersionAction(Table versionsTable) { super("edit"); this.versionsTable = versionsTable; } @Override public String getCaption() { return getMessage("open"); } public void actionPerform(Component component) { Set selected = versionsTable.getSelected(); if (selected.size() != 1) return; Doc docVersion = (Doc) selected.iterator().next(); openEditor(versionsTable.getDatasource().getMetaClass().getName() + ".edit", docVersion, WindowManager.OpenType.THIS_TAB); } } private void initHierarchyTab() { final CardTreeFrame cardTreeFrame = getComponent("cardTreeFrame"); accessData = getContext().getParamValue("accessData"); if (hierarchyTabInited) { cardTreeFrame.refreshDs(); return; } Map<String, Object> params = new HashMap<String, Object>(); params.put("card", getItem()); cardTreeFrame.init(params); cardTreeFrame.refreshDs(); final CollectionDatasource cardRelationsDs = getDsContext().get("cardRelationsDs"); final Tabsheet tabsheet = getComponent("hierarchyTabsheet"); CardService cardService = ServiceLocator.lookup(CardService.NAME); Integer count = cardService.getCardRelationsCount((Card) getItem()); if (count > 0) { tabsheet.getTab("cardRelationsTab").setCaption(getMessage("cardRelationsTab") + " (" + count + ")"); } else { tabsheet.getTab("cardRelationsTab").setCaption(getMessage("cardRelationsTab")); } cardRelationsDs.addListener(new CollectionDsListenerAdapter() { @Override public void collectionChanged(CollectionDatasource ds, Operation operation) { super.collectionChanged(ds, operation); Tabsheet.Tab tab = tabsheet.getTab("cardRelationsTab"); if (tab != null) { int count = cardRelationsDs.size(); if (count > 0) { tab.setCaption(getMessage("cardRelationsTab") + " (" + count + ")"); } else { tab.setCaption(getMessage("cardRelationsTab")); } } } }); tabsheet.addListener(new Tabsheet.TabChangeListener() { public void tabChanged(Tabsheet.Tab newTab) { if ("docTreeTab".equals(newTab.getName())) { cardTreeFrame.refreshDs(); } else if ("cardRelationsTab".equals(newTab.getName())) { CardRelationsFrame cardRelationsFrame = getComponent("cardRelationsFrame"); cardRelationsFrame.init(); cardRelationsFrame.setEditable(accessData.getNotVersion()); } } }); hierarchyTabInited = true; } protected void refresh() { // ? cardDs.refresh(); } protected void adjustForVersion(Map<String, Object> params) { accessData = getContext().getParamValue("accessData"); if (accessData != null) { Tabsheet ts = getComponent("tabsheet"); Component resolutionsPane = getComponent("resolutionsPane"); if (!accessData.getNotVersion()) { ts.removeTab("history"); ts.removeTab("processTab"); resolutionsPane.setVisible(false); setAttachmentButtonsEnabled(false); attachmentsDs.addListener(new CollectionDsListenerAdapter() { @Override public void itemChanged(Datasource ds, Entity prevItem, Entity item) { setAttachmentButtonsEnabled(false); } }); } } } protected void setAttachmentButtonsEnabled(boolean enable) { String[] componentNames = { "createAttachBtn", "editAttach", "removeAttach", "pasteAttach" }; for (String componentName : componentNames) { Component component = cardAttachmentsFrame.getComponent(componentName); if (component != null) component.setEnabled(false); } } }