2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.api
.service
;
12 import java
.util
.ArrayList
;
13 import java
.util
.List
;
14 import java
.util
.UUID
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
18 import org
.springframework
.stereotype
.Service
;
19 import org
.springframework
.transaction
.annotation
.Transactional
;
21 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
22 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.DefaultPagerImpl
;
23 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
24 import eu
.etaxonomy
.cdm
.model
.agent
.Address
;
25 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
26 import eu
.etaxonomy
.cdm
.model
.agent
.Institution
;
27 import eu
.etaxonomy
.cdm
.model
.agent
.InstitutionalMembership
;
28 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
29 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
30 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
31 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
32 import eu
.etaxonomy
.cdm
.persistence
.dao
.agent
.IAgentDao
;
33 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ICdmGenericDao
;
34 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
35 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.IIdentifiableEntityCacheStrategy
;
36 import eu
.etaxonomy
.cdm
.strategy
.merge
.ConvertMergeStrategy
;
37 import eu
.etaxonomy
.cdm
.strategy
.merge
.DefaultMergeStrategy
;
38 import eu
.etaxonomy
.cdm
.strategy
.merge
.IMergeStrategy
;
39 import eu
.etaxonomy
.cdm
.strategy
.merge
.MergeException
;
40 import eu
.etaxonomy
.cdm
.strategy
.merge
.MergeMode
;
49 @Transactional(readOnly
= true)
50 public class AgentServiceImpl
51 extends IdentifiableServiceBase
<AgentBase
,IAgentDao
>
52 implements IAgentService
{
53 private static final Logger logger
= Logger
.getLogger(AgentServiceImpl
.class);
56 ICdmGenericDao genericDao
;
60 protected void setDao(IAgentDao dao
) {
68 public AgentServiceImpl(){
69 if (logger
.isDebugEnabled()) { logger
.debug("Load AgentService Bean"); }
74 @Transactional(readOnly
= false)
75 public UpdateResult
updateCaches(Class
<?
extends AgentBase
> clazz
, Integer stepSize
, IIdentifiableEntityCacheStrategy
<AgentBase
> cacheStrategy
, IProgressMonitor monitor
) {
77 clazz
= AgentBase
.class;
79 return super.updateCachesImpl(clazz
, stepSize
, cacheStrategy
, monitor
);
83 public List
<Institution
> searchInstitutionByCode(String code
) {
84 return dao
.getInstitutionByCode(code
);
88 public Pager
<InstitutionalMembership
> getInstitutionalMemberships(Person person
, Integer pageSize
, Integer pageNumber
) {
89 long numberOfResults
= dao
.countInstitutionalMemberships(person
);
91 List
<InstitutionalMembership
> results
= new ArrayList
<>();
92 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
93 results
= dao
.getInstitutionalMemberships(person
, pageSize
, pageNumber
);
96 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
100 public Pager
<Person
> getMembers(Team team
, Integer pageSize
, Integer pageNumber
) {
101 long numberOfResults
= dao
.countMembers(team
);
103 List
<Person
> results
= new ArrayList
<>();
104 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
105 results
= dao
.getMembers(team
, pageSize
, pageNumber
);
108 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
112 public Pager
<Address
> getAddresses(AgentBase agent
, Integer pageSize
, Integer pageNumber
) {
113 long numberOfResults
= dao
.countAddresses(agent
);
115 List
<Address
> results
= new ArrayList
<>();
116 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
117 results
= dao
.getAddresses(agent
, pageSize
, pageNumber
);
120 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
124 public List
<UuidAndTitleCache
<Team
>> getTeamUuidAndNomenclaturalTitle() {
125 return dao
.getTeamUuidAndNomenclaturalTitle();
129 public List
<UuidAndTitleCache
<Person
>> getPersonUuidAndTitleCache() {
130 return dao
.getPersonUuidAndTitleCache();
134 public List
<UuidAndTitleCache
<Team
>> getTeamUuidAndTitleCache() {
135 return dao
.getTeamUuidAndTitleCache();
139 public List
<UuidAndTitleCache
<AgentBase
>> getInstitutionUuidAndTitleCache(Integer limit
, String pattern
) {
140 return dao
.getUuidAndAbbrevTitleCache(limit
, pattern
, Institution
.class);
144 @Transactional(readOnly
= false)
145 public DeleteResult
delete(UUID agentUUID
){
146 DeleteResult result
= new DeleteResult();
147 if (agentUUID
== null){
149 result
.addException(new Exception("Can't delete object without UUID."));
152 AgentBase base
= dao
.load(agentUUID
);
153 result
= isDeletable(agentUUID
, null);
156 if (base
instanceof Team
){
157 Team baseTeam
= (Team
) base
;
158 List
<Person
> members
= baseTeam
.getTeamMembers();
159 List
<Person
> temp
= new ArrayList
<Person
>();
160 for (Person member
:members
){
163 for (Person member
: temp
){
164 members
.remove(member
);
170 result
.addDeletedObject(base
);
178 public DeleteResult
delete(AgentBase agent
){
179 return delete(agent
.getUuid());
183 @Transactional(readOnly
= false)
184 public UpdateResult
convertTeam2Person(UUID teamUuid
) throws MergeException
{
185 Team team
= CdmBase
.deproxy(dao
.load(teamUuid
), Team
.class);
186 return convertTeam2Person(team
);
190 public UpdateResult
convertTeam2Person(Team team
) throws MergeException
{
191 UpdateResult result
= new UpdateResult();
192 Person newPerson
= null;
193 team
= CdmBase
.deproxy(team
, Team
.class);
194 if (team
.getTeamMembers().size() > 1){
195 throw new IllegalArgumentException("Team must not have more than 1 member to be convertable into a person");
196 }else if (team
.getTeamMembers().size() == 1){
197 newPerson
= team
.getTeamMembers().get(0);
198 IMergeStrategy strategy
= DefaultMergeStrategy
.NewInstance(TeamOrPersonBase
.class);
199 strategy
.setDefaultCollectionMergeMode(MergeMode
.FIRST
);
200 genericDao
.merge(newPerson
, team
, strategy
);
201 }else if (team
.getTeamMembers().isEmpty()){
202 newPerson
= Person
.NewInstance();
203 genericDao
.save(newPerson
);
204 IMergeStrategy strategy
= DefaultMergeStrategy
.NewInstance(TeamOrPersonBase
.class);
205 strategy
.setDefaultMergeMode(MergeMode
.SECOND
);
206 strategy
.setDefaultCollectionMergeMode(MergeMode
.SECOND
);
207 genericDao
.merge(newPerson
, team
, strategy
);
209 throw new IllegalStateException("Unhandled state of team members collection");
211 result
.setCdmEntity(newPerson
);
216 @Transactional(readOnly
= false)
217 public UpdateResult
convertPerson2Team(UUID personUuid
) throws MergeException
, IllegalArgumentException
{
218 Person person
= CdmBase
.deproxy(dao
.load(personUuid
), Person
.class);
219 return convertPerson2Team(person
);
223 public UpdateResult
convertPerson2Team(Person person
) throws MergeException
, IllegalArgumentException
{
224 UpdateResult result
= new UpdateResult();
225 Team team
= Team
.NewInstance();
226 ConvertMergeStrategy strategy
= ConvertMergeStrategy
.NewInstance(TeamOrPersonBase
.class);
227 strategy
.setDefaultMergeMode(MergeMode
.SECOND
);
228 strategy
.setDefaultCollectionMergeMode(MergeMode
.SECOND
);
229 strategy
.setDeleteSecondObject(true);
232 if (! genericDao
.isMergeable(team
, person
, strategy
)){
233 throw new MergeException("Person can not be transformed into team.");
237 team
.setProtectedNomenclaturalTitleCache(false);
238 team
.setProtectedTitleCache(true);
239 team
.setTitleCache(person
.getTitleCache(), true);
240 team
=(Team
) this.save(team
);
241 genericDao
.merge(team
, person
, strategy
);
242 //team.addTeamMember(person);
245 // team.setNomenclaturalTitle(person.getNomenclaturalTitle(), true);
246 } catch (Exception e
) {
247 throw new MergeException("Unhandled merge exception", e
);
249 result
.setCdmEntity(team
);
250 result
.addUpdatedObject(team
);
255 * @see eu.etaxonomy.cdm.api.service.IAgentService#getUuidAndAbbrevTitleCache()
258 public List
<UuidAndTitleCache
<AgentBase
>> getUuidAndAbbrevTitleCache(Integer limit
, String pattern
, Class clazz
) {
259 return dao
.getUuidAndAbbrevTitleCache(null, pattern
, clazz
);