BrightSide Workbench Full Report + Source Code
ContactProposal.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.proposal;
19 
20 import java.io.Serializable;
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24 import org.turro.string.Strings;
25 import org.turro.contacts.Address;
26 import org.turro.contacts.Comment;
27 import org.turro.contacts.Connector;
28 import org.turro.contacts.Contact;
29 import org.turro.util.CompareUtil;
30 
35 public class ContactProposal implements Serializable, Comparable<ContactProposal> {
36 
37  private Contact proposal, destination;
38  private String reason;
39  private List<GenericAttribute> attributes;
40  private boolean noAction, exists;
41 
42  public ContactProposal(Contact proposal) {
43  this(proposal, null);
44  }
45 
46  public ContactProposal(Contact proposal, String reason) {
47  this.proposal = proposal;
48  this.reason = reason;
49  this.exists = false;
50  addAttributes();
51  }
52 
53  public ContactProposal(Contact proposal, boolean exists) {
54  this.proposal = proposal;
55  this.reason = null;
56  this.exists = exists;
57  addAttributes();
58  }
59 
60  public void reload() {
61  addAttributes();
62  }
63 
65  return destination;
66  }
67 
68  public void setDestination(Contact destination) {
69  if(destination == null && this.destination != null) {
70  for(GenericAttribute ga : attributes) {
71  ga.setActive(true);
72  }
73  }
74  this.destination = destination;
75  }
76 
77  public String getReason() {
78  return reason;
79  }
80 
81  public void setReason(String reason) {
82  this.reason = reason;
83  }
84 
85  public boolean isNoAction() {
86  return noAction;
87  }
88 
89  public void setNoAction(boolean noAction) {
90  this.noAction = noAction;
91  }
92 
93  public String getDescription() {
94  return proposal.getName();
95  }
96 
97  public List<GenericAttribute> getAttributes() {
98  return attributes;
99  }
100 
101  public Contact getProposal() {
102  return proposal;
103  }
104 
105  public boolean isSureNew() {
106  for(GenericAttribute ga : attributes) {
107  if(!ga.getRelated().isEmpty()) {
108  return false;
109  }
110  }
111  return true;
112  }
113 
114  public boolean isProbablyNew() {
115  for(GenericAttribute ga : attributes) {
116  if(!ga.getRelated().isEmpty() && !ga.duplication.equals(DuplicationType.ALLOW_DUPLICATES)) {
117  return false;
118  }
119  }
120  return true;
121  }
122 
123  public boolean isDuplicated() {
124  for(GenericAttribute ga : attributes) {
125  if(!ga.getRelated().isEmpty() && ga.duplication.equals(DuplicationType.NOT_ALLOWED)) {
126  return true;
127  }
128  }
129  return false;
130  }
131 
132  private void addAttributes() {
133  attributes = new ArrayList<>();
134  attributes.add(new NameAttribute(proposal));
135  if(!Strings.isBlank(proposal.getGlobalIdentifier())) {
136  attributes.add(new GlobalIdAttribute(proposal));
137  }
138  for(Connector c : proposal.getConnectors()) {
139  attributes.add(new ConnectorAttribute(c));
140  }
141  for(Address a : proposal.getAddresses()) {
142  attributes.add(new AddressAttribute(a));
143  }
144  for(Comment c : proposal.getComments()) {
145  attributes.add(new CommentAttribute(c));
146  }
147  if(exists) {
148  // clear self
149  for(GenericAttribute ga : attributes) {
150  Iterator<IAttribute> it = ga.getRelated().iterator();
151  while(it.hasNext()) {
152  if(!Strings.isBlank(proposal.getId()) && it.next().getContact().getId().equals(proposal.getId())) {
153  it.remove();
154  }
155  }
156  }
157  }
158  MostProposedSet mps = new MostProposedSet();
159  for(GenericAttribute ga : attributes) {
160  for(IAttribute a : (List<IAttribute>) ga.getRelated()) {
161  mps.addProposition(a.getContact());
162  }
163  }
164  setDestination(mps.getMostProposed());
165  for(GenericAttribute ga : attributes) {
166  ga.checkActive(getDestination());
167  }
168  checkNoAction();
169  }
170 
171  public void clearInactiveAttributes() {
172  Iterator<GenericAttribute> it = attributes.iterator();
173  while(it.hasNext()) {
174  if(!it.next().isActive()) {
175  it.remove();
176  }
177  }
178  }
179 
180  private void checkNoAction() {
181  noAction = (
182  destination != null &&
183  (
184  !proposal.getName().equalsIgnoreCase(destination.getName()) ||
185  (
186  !Strings.isBlank(proposal.getGlobalIdentifier()) &&
187  !Strings.isBlank(destination.getGlobalIdentifier()) &&
188  !proposal.getGlobalIdentifier().equalsIgnoreCase(destination.getGlobalIdentifier())
189  )
190  ));
191  }
192 
193  @Override
194  public int compareTo(ContactProposal o) {
195  int result = CompareUtil.compare(proposal.getName(), o.proposal.getName());
196  if(result == 0) {
197  result = CompareUtil.compare(proposal.getGlobalIdentifier(), o.proposal.getGlobalIdentifier());
198  }
199  if(result == 0) {
200  CompareUtil.compare(this.toString(), o.toString());
201  }
202  return result;
203  }
204 
205 }
Set< Comment > getComments()
Definition: Contact.java:415
Set< Address > getAddresses()
Definition: Contact.java:355
Set< Connector > getConnectors()
Definition: Contact.java:367
ContactProposal(Contact proposal, String reason)
ContactProposal(Contact proposal, boolean exists)