BrightSide Workbench Full Report + Source Code
ProposalListbox.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.contacts.zul.proposal;
19 
20 import java.io.IOException;
21 import java.util.Collection;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import java.util.stream.Collectors;
27 import org.turro.command.Command;
28 import org.turro.command.Context;
29 import org.turro.contacts.Contact;
30 import org.turro.contacts.ContactType;
31 import org.turro.contacts.form.ContactWrapper;
32 import org.turro.contacts.proposal.ContactProposal;
33 import org.turro.contacts.proposal.GenericAttribute;
34 import org.turro.contacts.proposal.ProposalSet;
35 import org.turro.contacts.social.SocialGroupValue;
36 import org.turro.elephant.context.Application;
37 import org.turro.elephant.context.ElephantContext;
38 import org.turro.elephant.util.Messages;
39 import org.turro.security.SocialGroups;
40 import org.zkoss.zul.Listbox;
41 import org.zkoss.zul.Listitem;
42 
47 public class ProposalListbox extends Listbox {
48 
49  private final ProposalSet proposals;
50  private Collection<SocialGroupValue> socialGroups;
51  private ContactType type;
52 
53  public ProposalListbox() {
55  addProposalItems();
56  }
57 
58  public void setSocialGroups(Collection<SocialGroupValue> socialGroups) {
59  this.socialGroups = socialGroups;
60  }
61 
62  public void setType(ContactType type) {
63  this.type = type;
64  }
65 
66  public void importVCard() throws InterruptedException, IOException {
68  proposals.loadFromVCards(app, new Command() {
69  @Override
70  public Object execute(Context context) {
71  addProposalItems();
73  return null;
74  }
75  });
76  }
77 
78  public void processSingle(ContactProposal proposal) throws InterruptedException, Exception {
79  Iterator<Listitem> it = getItems().iterator();
80  boolean done = false;
81  while(it.hasNext()) {
82  Listitem li = it.next();
83  if(done) {
84  li.setSelected(true);
85  break;
86  } else if(li.getValue().equals(proposal)) {
87  ContactProposal cp = (ContactProposal) li.getValue();
88  processProposal(cp);
89  proposals.remove(cp);
90  it.remove();
91  done = true;
92  }
93  }
95  }
96 
97  public void processSelected() throws InterruptedException, Exception {
98  Messages.confirmProcess().show(() -> {
99  Iterator<Listitem> it = getItems().iterator();
100  while(it.hasNext()) {
101  Listitem li = it.next();
102  if(li.isSelected()) {
103  ContactProposal cp = (ContactProposal) li.getValue();
104  if(!cp.isNoAction()) {
105  try {
106  processProposal(cp);
107  proposals.remove(cp);
108  it.remove();
109  } catch (Exception ex) {
110  Logger.getLogger(ProposalListbox.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
111  }
112  }
113  }
114  }
116  });
117  }
118 
119  public void deleteSelected() throws InterruptedException {
120  Messages.confirmDeletion().show(() -> {
121  Iterator<Listitem> it = getItems().iterator();
122  while(it.hasNext()) {
123  Listitem li = it.next();
124  if(li.isSelected()) {
125  proposals.remove((ContactProposal) li.getValue());
126  it.remove();
127  }
128  }
130  });
131  }
132 
133  private void addProposalItems() {
134  getItems().clear();
135  for(ContactProposal cp : proposals) {
136  Listitem li = new Listitem(cp.getDescription(), cp);
137  if(cp.isDuplicated()) {
138  li.setSclass("duplicated");
139  } else if(cp.isProbablyNew()) {
140  li.setSclass("probablynew");
141  }
142  appendChild(li);
143  }
144  }
145 
146  private void processProposal(ContactProposal cp) throws Exception {
147  if(cp != null) {
148  Contact destination = cp.getDestination();
149  if(destination != null) {
150  for(GenericAttribute ga : cp.getAttributes()) {
151  if(ga.isActive()) {
152  ga.addToContact(destination);
153  }
154  }
155  saveDestination(destination);
156  } else {
157  cp.clearInactiveAttributes();
158  saveDestination(cp.getProposal());
159  }
160  }
161  }
162 
163  private void saveDestination(Contact destination) throws Exception {
164  if(type != null) destination.setType(type);
165  SocialGroups.syndicate(List.of(destination),
166  socialGroups.stream().map(sgv -> sgv.getKey()).collect(Collectors.toSet()),
167  null);
168  new ContactWrapper(destination).save();
169  }
170 
171 }
static ProposalSet deserialize(Application app)
static void serialize(Application app, ProposalSet instance)
void loadFromVCards(Application app, final Command command)
void processSingle(ContactProposal proposal)
void setSocialGroups(Collection< SocialGroupValue > socialGroups)
static Messages confirmDeletion()
Definition: Messages.java:87
static Messages confirmProcess()
Definition: Messages.java:95