BrightSide Workbench Full Report + Source Code
ProfileCtrl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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 
19 package org.turro.contacts.profile;
20 
21 import java.io.IOException;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import org.turro.string.Strings;
25 import org.turro.action.Actions;
26 import org.turro.action.Contacts;
27 import org.turro.annotation.ElephantPlugin;
28 import org.turro.auth.Authentication;
29 import org.turro.collections.KeyValueMap;
30 import org.turro.contacts.BusinessRelation;
31 import org.turro.contacts.Contact;
32 import org.turro.contacts.db.ContactsPU;
33 import org.turro.elephant.context.ElephantContext;
34 import org.turro.elephant.context.IConstructor;
35 import org.turro.elephant.direct.AbstractDirectEntityCtrl;
36 import org.turro.elephant.direct.DirectContent;
37 import org.turro.marker.ElephantMarker;
38 import org.turro.plugin.contacts.IContact;
39 
44 //@ElephantPlugin(label="profile-ctrl")
45 //@DirectContent(identifier="profile-action")
46 @Deprecated
47 public class ProfileCtrl extends AbstractDirectEntityCtrl {
48 
49  private Contact contact;
50 
51  public ProfileCtrl() {
52  super("widgets/profile", "profile");
53  setNeedsUser(true);
54  setTemplate("profile");
55  }
56 
57  public void setContact(Contact contact) {
58  if(contact == null) {
59  this.contact = (Contact) Contacts.getContactById(Authentication.getIContact().getId()).getContact();
60  } else {
61  this.contact = contact;
62  }
63  }
64 
65  public String editLink(Object entity, String element) {
66  KeyValueMap kvm = new KeyValueMap();
67  kvm.put("type", "edit");
68  kvm.put("contact", contact.getId());
69  kvm.put("element", element);
70  if(entity instanceof ProfileRelation) {
71  kvm.put("relation", ((ProfileRelation) entity).getRelation().getId());
72  }
73  return createRightNowURL(kvm);
74  }
75 
76  public String addCompanyLink() {
77  KeyValueMap kvm = new KeyValueMap();
78  kvm.put("type", "add");
79  kvm.put("contact", contact.getId());
80  kvm.put("element", "company");
81  return createRightNowURL(kvm);
82  }
83 
84  public String addCenterLink() {
85  KeyValueMap kvm = new KeyValueMap();
86  kvm.put("type", "add");
87  kvm.put("contact", contact.getId());
88  kvm.put("element", "center");
89  return createRightNowURL(kvm);
90  }
91 
92  public String publicLink(Object entity, String element) {
93  KeyValueMap kvm = new KeyValueMap();
94  kvm.put("type", "public");
95  kvm.put("contact", contact.getId());
96  kvm.put("element", element);
97  if(entity instanceof ProfileRelation) {
98  kvm.put("relation", ((ProfileRelation) entity).getRelation().getId());
99  }
100  return createRightNowURL(kvm);
101  }
102 
103  public String usersLink(Object entity, String element) {
104  KeyValueMap kvm = new KeyValueMap();
105  kvm.put("type", "users");
106  kvm.put("contact", contact.getId());
107  kvm.put("element", element);
108  if(entity instanceof ProfileRelation) {
109  kvm.put("relation", ((ProfileRelation) entity).getRelation().getId());
110  }
111  return createRightNowURL(kvm);
112  }
113 
114  public String followingLink(Object entity, String element) {
115  KeyValueMap kvm = new KeyValueMap();
116  kvm.put("type", "following");
117  kvm.put("contact", contact.getId());
118  kvm.put("element", element);
119  if(entity instanceof ProfileRelation) {
120  kvm.put("relation", ((ProfileRelation) entity).getRelation().getId());
121  }
122  return createRightNowURL(kvm);
123  }
124 
125  public String privateLink(Object entity, String element) {
126  KeyValueMap kvm = new KeyValueMap();
127  kvm.put("type", "private");
128  kvm.put("contact", contact.getId());
129  kvm.put("element", element);
130  if(entity instanceof ProfileRelation) {
131  kvm.put("relation", ((ProfileRelation) entity).getRelation().getId());
132  }
133  return createRightNowURL(kvm);
134  }
135 
136  @Override
137  protected void prepareCleanMarker(ElephantMarker marker, KeyValueMap map) {
138  marker.putAll(map);
139  marker.put("profile", new Profile((Contact) Contacts.getContactById(map.get("contact")).getContact(), true));
140  }
141 
142  /* IEntityCtrl */
143 
144  @Override
145  protected void prepareMarker(ElephantMarker marker) {
146  addContainerId(marker, true);
147  marker.put("profile", new Profile(contact, true));
148  }
149 
150  /* IDirectContent */
151 
152  @Override
153  protected String getIdentifier() {
154  return ProfileCtrl.class.getAnnotation(DirectContent.class).identifier();
155  }
156 
157  @Override
158  protected void doExecute(IConstructor constructor, KeyValueMap map) {
159  String type = map.get("type");
160  IContact current = Contacts.getContactById(map.get("contact"));
161  if(current != null) {
162  if("edit".equals(type)) {
163  try {
164  if(map.containsKey("relation")) {
165  BusinessRelation relation = getRelation(current, map.get("relation"));
166  switch(relation.getBusiness().getType()) {
167  case CONTACT_COMPANY:
168  constructor.redirect("/user/profile/editrb?" + Actions.createRightNowAction(map));
169  return;
170  case CONTACT_LEARNINGCENTER:
171  constructor.redirect("/user/profile/editrc?" + Actions.createRightNowAction(map));
172  return;
173  }
174  } else if("skills".equals(map.get("element"))) {
175  map.put("entityPath", ContactsPU.getObjectPath(current.getContact()));
176  constructor.redirect("/user/profile/editsk?" + Actions.createRightNowAction(map));
177  return;
178  } else {
179  constructor.redirect("/user/profile/edit?" + Actions.createRightNowAction(map));
180  return;
181  }
182  } catch (IOException ex) {
183  Logger.getLogger(ProfileCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
184  }
185  } else if("add".equals(type)) {
186  try {
187  switch(map.get("element")) {
188  case "company":
189  constructor.redirect("/user/profile/editrb?" + Actions.createRightNowAction(map));
190  return;
191  case "center":
192  constructor.redirect("/user/profile/editrc?" + Actions.createRightNowAction(map));
193  return;
194  }
195  } catch (IOException ex) {
196  Logger.getLogger(ProfileCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
197  }
198  } else {
199  ProfilePolicies pp = new ProfilePolicies((Contact) current.getContact());
201  BusinessRelation relation = getRelationFrom((Contact) current.getContact(), map.get("relation"));
202  if("public".equals(type)) {
203  policy = PublishPolicy.PUBLISH_ALL;
204  } else if("users".equals(type)) {
205  policy = PublishPolicy.PUBLISH_USERS;
206  } else if("following".equals(type)) {
208  }
209  pp.setPolicy(relation == null ? current.getContact() : relation, map.get("element"), policy);
210  pp.save();
211  }
212  try {
213  constructor.redirect("/user/profile");
214  } catch (IOException ex) {
215  Logger.getLogger(ProfileCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
216  }
217  }
218  }
219 
220  private BusinessRelation getRelationFrom(Contact contact, String relationId) {
221  if(!Strings.isBlank(relationId)) {
222  for(BusinessRelation relation : contact.getBusinessRelations()) {
223  if(relation.getId().equals(relationId)) return relation;
224  }
225  }
226  return null;
227  }
228 
229  private BusinessRelation getRelation(IContact current, String relationId) {
230  Contact c = (Contact) current.getContact();
231  for(BusinessRelation br : c.getBusinessRelations()) {
232  if(br.getId().equals(relationId)) {
233  return br;
234  }
235  }
236  return null;
237  }
238 
239 }
static String createRightNowAction(String values)
Definition: Actions.java:312
static IContact getContactById(String id)
Definition: Contacts.java:72
Set< BusinessRelation > getBusinessRelations()
Definition: Contact.java:379
static String getObjectPath(Object object)
Definition: ContactsPU.java:68
String usersLink(Object entity, String element)
void prepareCleanMarker(ElephantMarker marker, KeyValueMap map)
String editLink(Object entity, String element)
String followingLink(Object entity, String element)
void doExecute(IConstructor constructor, KeyValueMap map)
String publicLink(Object entity, String element)
void prepareMarker(ElephantMarker marker)
String privateLink(Object entity, String element)
void setPolicy(Object obj, String element, PublishPolicy publish)
void addContainerId(ElephantMarker marker, boolean container)
Object put(Object key, Object value)
void setTemplate(String template)