2 * Copyright (C) 2014 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.
9 package eu
.etaxonomy
.cdm
.api
.service
.molecular
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Collection
;
13 import java
.util
.HashMap
;
14 import java
.util
.List
;
16 import java
.util
.Map
.Entry
;
17 import java
.util
.UUID
;
19 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
20 import org
.springframework
.stereotype
.Service
;
21 import org
.springframework
.transaction
.annotation
.Transactional
;
23 import eu
.etaxonomy
.cdm
.api
.service
.AnnotatableServiceBase
;
24 import eu
.etaxonomy
.cdm
.api
.service
.DeleteResult
;
25 import eu
.etaxonomy
.cdm
.api
.service
.UpdateResult
;
26 import eu
.etaxonomy
.cdm
.api
.service
.UpdateResult
.Status
;
27 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
28 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
29 import eu
.etaxonomy
.cdm
.model
.molecular
.DnaSample
;
30 import eu
.etaxonomy
.cdm
.model
.molecular
.Sequence
;
31 import eu
.etaxonomy
.cdm
.model
.molecular
.SingleRead
;
32 import eu
.etaxonomy
.cdm
.model
.molecular
.SingleReadAlignment
;
33 import eu
.etaxonomy
.cdm
.persistence
.dao
.molecular
.ISequenceDao
;
34 import eu
.etaxonomy
.cdm
.persistence
.dao
.molecular
.ISingleReadDao
;
41 @Transactional(readOnly
= true)
42 public class SequenceServiceImpl
extends AnnotatableServiceBase
<Sequence
, ISequenceDao
> implements ISequenceService
{
45 private ISingleReadDao singleReadDao
;
49 protected void setDao(ISequenceDao dao
) {
54 public UpdateResult
moveSingleRead(Sequence from
, Sequence to
, SingleRead singleRead
) {
55 UpdateResult result
= new UpdateResult();
56 from
.removeSingleRead(singleRead
);
58 to
.addSingleRead(singleRead
);
60 result
.setStatus(Status
.OK
);
61 result
.addUpdatedObject(from
);
62 result
.addUpdatedObject(to
);
67 @Transactional(readOnly
= false)
68 public UpdateResult
moveSingleRead(UUID fromUuid
, UUID toUuid
, UUID singleReadUuid
) {
69 SingleRead singleRead
= null;
70 Sequence from
= CdmBase
.deproxy(dao
.load(fromUuid
), Sequence
.class);
71 Sequence to
= CdmBase
.deproxy(dao
.load(toUuid
), Sequence
.class);
72 for(SingleRead sr
: from
.getSingleReads()) {
73 if(sr
.getUuid().equals(singleReadUuid
)) {
78 return moveSingleRead(from
, to
, singleRead
);
82 public Map
<SingleRead
, Collection
<Sequence
>> getSingleReadSequencesMap() {
83 Map
<SingleRead
, Collection
<Sequence
>> singleReadToSequences
= new HashMap
<SingleRead
, Collection
<Sequence
>>();
84 for(Sequence sequence
:list(Sequence
.class, null, null, null, null)){
85 for(SingleRead singleRead
:sequence
.getSingleReads()){
86 Collection
<Sequence
> sequences
= singleReadToSequences
.get(singleRead
);
88 sequences
= new ArrayList
<Sequence
>();
90 sequences
.add(sequence
);
91 singleReadToSequences
.put(singleRead
, sequences
);
94 return singleReadToSequences
;
98 public DeleteResult
delete(Sequence sequence
) {
99 DeleteResult deleteResult
= new DeleteResult();
100 //remove from dnaSample
101 DnaSample dnaSample
= sequence
.getDnaSample();
103 dnaSample
.removeSequence(sequence
);
104 deleteResult
.addUpdatedObject(dnaSample
);
107 for (SingleReadAlignment singleReadAlignment
: sequence
.getSingleReadAlignments()) {
108 deleteSingleRead(singleReadAlignment
.getSingleRead(), sequence
);
110 dao
.delete(sequence
);
111 deleteResult
.addDeletedObject(sequence
);
116 public DeleteResult
deleteSingleRead(SingleRead singleRead
, Sequence sequence
){
117 DeleteResult deleteResult
= new DeleteResult();
118 singleRead
= HibernateProxyHelper
.deproxy(singleRead
, SingleRead
.class);
119 //delete from amplification result
120 if(singleRead
.getAmplificationResult()!=null){
121 deleteResult
.addUpdatedObject(singleRead
.getAmplificationResult());
122 singleRead
.getAmplificationResult().removeSingleRead(singleRead
);
124 //delete from sequence
125 sequence
.removeSingleRead(singleRead
);
126 deleteResult
.addUpdatedObject(sequence
);
128 //check if used in other sequences
129 List
<SingleRead
> toDelete
= new ArrayList
<SingleRead
>();
130 Map
<SingleRead
, Collection
<Sequence
>> singleReadSequencesMap
= getSingleReadSequencesMap();
131 if(singleReadSequencesMap
.containsKey(singleRead
)){
132 for (Entry
<SingleRead
, Collection
<Sequence
>> entry
: singleReadSequencesMap
.entrySet()) {
133 if(entry
.getValue().isEmpty()){
134 toDelete
.add(singleRead
);
137 for (SingleRead singleReadToDelete
: toDelete
) {
138 singleReadDao
.delete(singleReadToDelete
);
139 deleteResult
.addDeletedObject(singleReadToDelete
);
143 singleReadDao
.delete(singleRead
);
144 deleteResult
.addDeletedObject(singleRead
);
146 deleteResult
.setStatus(Status
.OK
);
151 @Transactional(readOnly
= false)
152 public DeleteResult
deleteSingleRead(UUID singleReadUuid
, UUID sequenceUuid
){
153 SingleRead singleRead
= null;
154 Sequence sequence
= CdmBase
.deproxy(load(sequenceUuid
), Sequence
.class);
155 for(SingleRead sr
: sequence
.getSingleReads()) {
156 if(sr
.getUuid().equals(singleReadUuid
)) {
161 return deleteSingleRead(singleRead
, sequence
);