BrightSide Workbench Full Report + Source Code
ForumAssistants.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.assistant;
20 
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.List;
24 import org.turro.annotation.ExternalAssistant;
25 import org.turro.elephant.entities.db.Post;
26 import org.turro.elephant.entities.db.Topic;
27 import org.turro.entities.Entities;
28 import org.turro.entities.IUniquePath;
29 import org.turro.forum.Forums;
30 import org.turro.plugin.contacts.IContact;
31 
36 @ExternalAssistant
37 public class ForumAssistants implements IAssistant {
38 
39  @Override
40  public void add(Object entity, boolean deep, AssistantSet assistants, Object data) {
41  if(entity instanceof String) {
42  addFromEntityPath((String) entity, deep, assistants, data);
43  } else {
44  addFromEntity(entity, deep, assistants, data);
45  }
46  }
47 
48  @Override
49  public void addFromEntityPath(String entityPath, boolean deep, AssistantSet assistants, Object data) {
50  Object entity = Entities.getController(entityPath).getEntity();
51  addFromEntity(entity, deep, assistants, data);
52  }
53 
54  @Override
55  public void addFromEntity(Object entity, boolean deep, AssistantSet assistants, Object data) {
56  if(entity instanceof Topic) {
57  Topic topic = (Topic) entity;
58  assistants.addSubject(topic.getText());
59  assistants.add(new Assistant(topic.getContact(), topic));
60  Assistants.addAssistants(topic.getEntityPath(), deep, assistants, data);
61  } else if(entity instanceof Post) {
62  Post post = (Post) entity;
63  Topic topic = post.getParentTopic();
64  assistants.addSubject(topic.getText());
65  assistants.add(new Assistant(post.getContact(), post));
66  IUniquePath parent = post.getCurrentParent();
67  while(parent != null) {
68  if(parent instanceof Post) {
69  assistants.add(new Assistant(((Post) parent).getContact(), topic));
70  parent = ((Post) parent).getCurrentParent();
71  } else if(parent instanceof Topic) {
72  assistants.add(new Assistant(((Topic) parent).getContact(), topic));
73  parent = null;
74  }
75  }
76  Assistants.addAssistants(topic.getEntityPath(), deep, assistants, data);
77  }
78  }
79 
80  @Override
81  public void addFromEntityPathData(String entityPath, AssistantSet assistants, Object data) {
82  if((data instanceof String) && ((String) data).contains(AssistantConstants.FROM_FORUM)) {
83  Collection<Topic> topics = Forums.getTopics(entityPath);
84  for(Topic topic : topics) {
85  IContact c = topic.getContact();
86  if(c != null && c.isWebUser()) {
87  assistants.add(new Assistant(c, topic));
88  }
89  }
90  }
91  }
92 
93  @Override
94  public void addFromEntityData(Object entity, AssistantSet assistants, Object data) {
95  if((data instanceof String) && ((String) data).contains(AssistantConstants.FROM_FORUM)) {
96  String entityPath = Entities.getController(entity).getPath();
97  addFromEntityPathData(entityPath, assistants, data);
98  }
99  }
100 
101  @Override
102  public List<String> getParticiped(IContact contact) {
103  return Collections.EMPTY_LIST;
104  }
105 
106 }
static void addAssistants(String role, AssistantSet list, Object data)
Definition: Assistants.java:35
void addFromEntityPath(String entityPath, boolean deep, AssistantSet assistants, Object data)
void addFromEntityPathData(String entityPath, AssistantSet assistants, Object data)
void add(Object entity, boolean deep, AssistantSet assistants, Object data)
void addFromEntityData(Object entity, AssistantSet assistants, Object data)
void addFromEntity(Object entity, boolean deep, AssistantSet assistants, Object data)
List< String > getParticiped(IContact contact)
static IElephantEntity getController(String path)
Definition: Entities.java:78
static List< Topic > getTopics(String entityPath)
Definition: Forums.java:47