BrightSide Workbench Full Report + Source Code
Participations.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2023 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.alliance.content.control;
20 
21 import java.util.Date;
22 import org.turro.alliance.db.AlliancePU;
23 import org.turro.alliance.db.entities.RelationId;
24 import org.turro.jpa.Dao;
25 import org.turro.server.db.entities.AxAllianceParticipation;
26 import org.turro.server.db.entities.AxParticipationRequest;
27 import org.turro.util.Cached;
28 
33 public class Participations {
34 
35  public void validate() {
36  dao.get().saveObject(addUp(get()));
37  }
38 
39  public void delete() {
40  dao.get().deleteObject(request);
41  }
42 
43  public void add() {
44  dao.get().saveObject(addUpRequest(getRequest()));
45  }
46 
47  /* Utils */
48 
49  private AxAllianceParticipation get() {
50  RelationId rid = request.getRelation();
51  return dao.get().find(AxAllianceParticipation.class, rid);
52  }
53 
54  private AxAllianceParticipation addUp(AxAllianceParticipation participation) {
55  if(participation == null) {
56  participation = new AxAllianceParticipation();
57  participation.setRelation(request.getRelation());
58  participation.setCreation(new Date());
59  }
60  participation.setCompany(request.getCompany());
61  participation.setCompanyFace(request.getCompanyFace());
62  participation.setFace(request.getFace());
63  participation.setMemberName(request.getMemberName());
64  participation.setName(request.getName());
65  if(request.isAdmin()) participation.setAdmin(true);
66  if(request.isBeneficiary()) participation.setBeneficiary(true);
67  if(request.isConsortium()) participation.setConsortium(true);
68  if(request.isCoordinator()) participation.setCoordinator(true);
69  if(request.isDriver()) participation.setDriver(true);
70  if(request.isFunding()) participation.setFunding(true);
71  if(request.isOfferer()) participation.setOfferer(true);
72  if(request.isResearch()) participation.setResearch(true);
73  if(request.isSupport()) participation.setSupport(true);
74  return participation;
75  }
76 
77  private AxParticipationRequest getRequest() {
78  RelationId rid = request.getRelation();
79  return dao.get().find(AxParticipationRequest.class, rid);
80  }
81 
82  private AxParticipationRequest addUpRequest(AxParticipationRequest participation) {
83  if(participation == null) {
84  participation = new AxParticipationRequest();
85  participation.setRelation(request.getRelation());
86  participation.setCreation(new Date());
87  }
88  participation.setCompany(request.getCompany());
89  participation.setCompanyFace(request.getCompanyFace());
90  participation.setFace(request.getFace());
91  participation.setMemberName(request.getMemberName());
92  participation.setName(request.getName());
93  if(request.isAdmin()) participation.setAdmin(true);
94  if(request.isBeneficiary()) participation.setBeneficiary(true);
95  if(request.isConsortium()) participation.setConsortium(true);
96  if(request.isCoordinator()) participation.setCoordinator(true);
97  if(request.isDriver()) participation.setDriver(true);
98  if(request.isFunding()) participation.setFunding(true);
99  if(request.isOfferer()) participation.setOfferer(true);
100  if(request.isResearch()) participation.setResearch(true);
101  if(request.isSupport()) participation.setSupport(true);
102  return participation;
103  }
104 
105  /* Dao */
106 
107  private final Cached<Dao> dao = Cached.instance(() -> new AlliancePU());
108 
109  /* Factory */
110 
111  public static Participations from(AxParticipationRequest request) {
112  return new Participations(request);
113  }
114 
115  private final AxParticipationRequest request;
116 
117  private Participations(AxParticipationRequest request) {
118  this.request = request;
119  }
120 
121 }
static Participations from(AxParticipationRequest request)