BrightSide Workbench Full Report + Source Code
ParticipationWalker.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.assistant;
20 
21 import java.util.function.Consumer;
22 import org.turro.elephant.db.ElephantPU;
23 import org.turro.elephant.db.WhereClause;
24 import org.turro.entities.Entities;
25 import org.turro.entities.ITreeEntity;
26 import org.turro.jpa.Dao;
27 import org.turro.participation.ParticipationReason;
28 import org.turro.plugin.contacts.IContact;
29 
34 public class ParticipationWalker {
35 
36  /* Walk up */
37 
38  public void walkUpTree(ITreeEntity entity, Consumer<ITreeEntity> execute) {
39  if(entity != null) {
40  execute.accept(entity);
41  walkUpTree(entity.getEntityParent(), execute);
42  }
43  }
44 
45  /* Walk down */
46 
47  public <T> void walkDownTree(ITreeEntity entity, Consumer<ITreeEntity> execute) {
48  if(entity != null) {
49  execute.accept(entity);
50  for(ITreeEntity e : entity.getEntityChildren()) {
51  walkDownTree(e, execute);
52  }
53  }
54  }
55 
56  /* Utilities */
57 
58  public void deleteExceptFor(Object entity, IContact contact, ParticipationReason reason) {
59  WhereClause wc = new WhereClause();
60  wc.addClause("delete from EntityParticipation");
61  wc.addClause("where entityPath = :entityPath");
62  wc.addNamedValue("entityPath", Entities.getController(entity).getPath());
63  wc.addClause("and participatorPath <> :participatorPath");
64  wc.addNamedValue("participatorPath", Entities.getController(contact.getContact()).getPath());
65  wc.addClause("and reason = :reason");
66  wc.addNamedValue("reason", reason);
67  getDao().executeUpdate(wc);
68  }
69 
70  public void createFor(Object entity, IContact contact, ParticipationReason reason) {
71  new ParticipationInfo(entity, contact, reason).check();
72  }
73 
74  /* Dao */
75 
76  private Dao _dao;
77 
78  private Dao getDao() {
79  if(_dao == null) {
80  _dao = new ElephantPU();
81  }
82  return _dao;
83  }
84 
85 }
void walkUpTree(ITreeEntity entity, Consumer< ITreeEntity > execute)
void createFor(Object entity, IContact contact, ParticipationReason reason)
void deleteExceptFor(Object entity, IContact contact, ParticipationReason reason)
void addNamedValue(String name, Object value)
static IElephantEntity getController(String path)
Definition: Entities.java:78
int executeUpdate(String query)
Definition: Dao.java:463
Collection< ITreeEntity > getEntityChildren()