001package org.kuali.kpme.edo.reviewlayerdef.service;
002
003import java.util.Collection;
004import java.util.HashMap;
005import java.util.LinkedList;
006import java.util.List;
007import java.util.Map;
008import java.util.Set;
009import java.util.TreeMap;
010
011import org.apache.commons.collections.CollectionUtils;
012import org.kuali.kpme.edo.api.reviewlayerdef.EdoReviewLayerDefinition;
013import org.kuali.kpme.edo.api.reviewlayerdef.EdoSuppReviewLayerDefinition;
014import org.kuali.kpme.edo.api.vote.EdoVoteRecord;
015import org.kuali.kpme.edo.reviewlayerdef.EdoReviewLayerDefinitionBo;
016import org.kuali.kpme.edo.reviewlayerdef.EdoSuppReviewLayerDefinitionBo;
017import org.kuali.kpme.edo.reviewlayerdef.dao.EdoReviewLayerDefinitionDao;
018import org.kuali.kpme.edo.service.EdoServiceLocator;
019import org.kuali.kpme.edo.util.EdoConstants;
020import org.kuali.rice.core.api.mo.ModelObjectUtils;
021
022public class EdoReviewLayerDefinitionServiceImpl implements EdoReviewLayerDefinitionService {
023
024    private EdoReviewLayerDefinitionDao edoReviewLayerDefinitionDao;
025
026    public EdoReviewLayerDefinition getReviewLayerDefinitionById(String edoReviewLayerDefinitionId) {
027        EdoReviewLayerDefinitionBo edoReviewLayerDefinitionBo = edoReviewLayerDefinitionDao.getReviewLayerDefinitionById(edoReviewLayerDefinitionId);
028        
029        if ( edoReviewLayerDefinitionBo == null){
030                
031                return null;
032        }
033        
034        EdoReviewLayerDefinition.Builder builder = EdoReviewLayerDefinition.Builder.create(edoReviewLayerDefinitionBo);
035        
036        return builder.build();
037    }
038
039    public EdoReviewLayerDefinition getReviewLayerDefinition(String workflowId, String nodeName) {
040        EdoReviewLayerDefinitionBo edoReviewLayerDefinitionBo = edoReviewLayerDefinitionDao.getReviewLayerDefinition(workflowId, nodeName);
041        
042        if ( edoReviewLayerDefinitionBo == null){
043                return null;
044        }
045        
046        EdoReviewLayerDefinition.Builder builder = EdoReviewLayerDefinition.Builder.create(edoReviewLayerDefinitionBo);
047        
048        return builder.build();
049    }
050
051    public Collection<EdoReviewLayerDefinition> getReviewLayerDefinitions() {
052        
053        return ModelObjectUtils.transform(edoReviewLayerDefinitionDao.getReviewLayerDefinitions(), EdoReviewLayerDefinitionBo.toImmutable); 
054    }
055    
056    public List<EdoReviewLayerDefinition> getReviewLayerDefinitionsByWorkflowId(String workflowId) {
057        
058        return ModelObjectUtils.transform(edoReviewLayerDefinitionDao.getReviewLayerDefinitionsByWorkflowId(workflowId), EdoReviewLayerDefinitionBo.toImmutable); 
059    }
060    
061    public Collection<EdoReviewLayerDefinition> getReviewLayerDefinitions(String workflowId) {
062        
063        return ModelObjectUtils.transform(edoReviewLayerDefinitionDao.getReviewLayerDefinitions(workflowId), EdoReviewLayerDefinitionBo.toImmutable);
064    }
065 
066    // for lookuphelper
067    public List<EdoReviewLayerDefinition> getReviewLayerDefinitions(String nodeName, String voteType, String reviewLetter) {
068        
069        return ModelObjectUtils.transform(edoReviewLayerDefinitionDao.getReviewLayerDefinitions(nodeName, voteType, reviewLetter), EdoReviewLayerDefinitionBo.toImmutable);
070    }
071    
072    public Collection<EdoReviewLayerDefinition> getReviewLayerDefinitions(String workflowId, Set<String> nodeNames) {
073        
074        return ModelObjectUtils.transform(edoReviewLayerDefinitionDao.getReviewLayerDefinitions(workflowId, nodeNames), EdoReviewLayerDefinitionBo.toImmutable);
075    }
076    
077    public Collection<EdoReviewLayerDefinition> getReviewLayerDefinitionsToMax(String maxRouteLevel) {
078        
079        return ModelObjectUtils.transform(edoReviewLayerDefinitionDao.getReviewLayerDefinitionsToMax(maxRouteLevel), EdoReviewLayerDefinitionBo.toImmutable);
080    }
081    
082    @Override 
083    public List<EdoReviewLayerDefinition> getRouteLevelsWithReviewLayers() {
084        
085        return ModelObjectUtils.transform(edoReviewLayerDefinitionDao.getRouteLevelsWithReviewLayers(), EdoReviewLayerDefinitionBo.toImmutable);
086    }
087
088    public Collection<EdoReviewLayerDefinitionBo> getReviewLayerDefinitions(Set<String> nodeNames) {
089        return this.edoReviewLayerDefinitionDao.getReviewLayerDefinitions(nodeNames);
090    }
091
092    
093
094    public List<String> getValidReviewLevelNodeNames() {
095        return this.edoReviewLayerDefinitionDao.getValidReviewLevelNodeNames();
096    }
097
098    public List<String> getValidReviewLevelNodeNames(String workflowId) {
099        return this.edoReviewLayerDefinitionDao.getValidReviewLevelNodeNames(workflowId);
100    }
101
102    public Map<String, EdoReviewLayerDefinition> buildReviewLevelMap(Collection<EdoReviewLayerDefinition> reviewLayerDefinitions) {
103        Map<String, EdoReviewLayerDefinition> levelMap = new HashMap<String, EdoReviewLayerDefinition>();
104        
105        if (CollectionUtils.isNotEmpty(reviewLayerDefinitions)) {
106            for (EdoReviewLayerDefinition reviewLayerDefinition : reviewLayerDefinitions) {
107                levelMap.put(reviewLayerDefinition.getReviewLevel(), reviewLayerDefinition);
108            }
109        }
110        return levelMap;
111    }
112
113    public Map<String, String> buildReviewLevelByRouteMap(Collection<EdoReviewLayerDefinition> reviewLayerDefinitions) {
114        Map<String, String> levelMap = new HashMap<String, String>();
115        if (CollectionUtils.isNotEmpty(reviewLayerDefinitions)) {
116            for (EdoReviewLayerDefinition reviewLayerDefinition : reviewLayerDefinitions) {
117                levelMap.put(reviewLayerDefinition.getNodeName(), reviewLayerDefinition.getReviewLevel());
118            }
119        }
120        return levelMap;
121    }
122
123    // KPME-3711 These methods have been moved to EdoSuppReviewLayerDefinition
124    /*
125    public List<String> getAuthorizedSupplementalNodes(String reviewLayerDefinitionId) {
126    
127        return this.edoReviewLayerDefinitionDao.getAuthorizedSupplementalNodes(reviewLayerDefinitionId);        
128    }
129    
130    public EdoSuppReviewLayerDefinition getSuppReviewLayerDefinition(String reviewLayerDefinitionId) {
131        return EdoSuppReviewLayerDefinitionBo.to(edoReviewLayerDefinitionDao.getSuppReviewLayerDefinition(reviewLayerDefinitionId));
132    }
133   
134    public String getSuppLevelQualifierByWorkflowId(String workflowId, String nodeName) {
135        return this.edoReviewLayerDefinitionDao.getSuppLevelQualifierByWorkflowId(workflowId, nodeName);
136    }*/
137
138    public void setEdoReviewLayerDefinitionDao(EdoReviewLayerDefinitionDao edoReviewLayerDefinitionDao) {
139        this.edoReviewLayerDefinitionDao = edoReviewLayerDefinitionDao;
140    }
141
142    public void saveOrUpdate(EdoReviewLayerDefinition reviewLayerDefinition) {
143        
144        EdoReviewLayerDefinitionBo bo = EdoReviewLayerDefinitionBo.from(reviewLayerDefinition);
145        
146        this.edoReviewLayerDefinitionDao.saveOrUpdate(bo);
147    }
148        
149    public List<String> getDistinctWorkflowIds() {
150        return this.edoReviewLayerDefinitionDao.getDistinctWorkflowIds();
151    }
152
153    public String getLevelQualifierByWorkflowId(String workflowId, String nodeName) {
154        return this.edoReviewLayerDefinitionDao.getLevelQualifierByWorkflowId(workflowId, nodeName);
155    }
156
157    public Map<String,String> getReviewLayerDefinitionDescriptionsByWorkflow(String workflowId) {
158        //List<String> levels = new LinkedList<String>();
159        Map<String,String> levelMap = new TreeMap<String, String>();
160
161        List<EdoReviewLayerDefinition> rldList = getReviewLayerDefinitionsByWorkflowId(workflowId);
162
163        for (EdoReviewLayerDefinition rld : rldList) {
164            levelMap.put(rld.getEdoReviewLayerDefinitionId(), rld.getDescription());
165            //levels.add(rld.getDescription());
166        }
167        return levelMap;
168    }
169    
170    public EdoReviewLayerDefinition findFirstNegativeReviewLayerByVote(String edoDossierId) {
171        EdoReviewLayerDefinition rld = null;
172        String workflowId = EdoServiceLocator.getEdoDossierService().getEdoDossierById(edoDossierId).getWorkflowId();
173
174        Collection<EdoReviewLayerDefinition> reviewLayerDefinitions = EdoServiceLocator.getEdoReviewLayerDefinitionService().getReviewLayerDefinitions(workflowId);
175        List<EdoReviewLayerDefinition> voteRecordLayerDefinitions = new LinkedList<EdoReviewLayerDefinition>();
176
177        for (EdoReviewLayerDefinition reviewLayerDefinition : reviewLayerDefinitions) {
178            if (!reviewLayerDefinition.getVoteType().equals(EdoConstants.VOTE_TYPE_NONE)) {
179                voteRecordLayerDefinitions.add(reviewLayerDefinition);
180            }
181        }
182        for (EdoReviewLayerDefinition voteRLD : voteRecordLayerDefinitions) {
183            EdoVoteRecord voteRecord = EdoServiceLocator.getEdoVoteRecordService().getVoteRecordMostCurrentRound(edoDossierId, voteRLD.getEdoReviewLayerDefinitionId());
184            if (EdoServiceLocator.getEdoVoteRecordService().isNegativeVote(voteRecord)) {
185                if (rld == null) {
186                    rld = voteRLD;
187                    continue;
188                }
189                if (voteRLD.getReviewLevel().compareTo(rld.getReviewLevel()) == -1 ) {
190                    rld = voteRLD;
191                }
192            }
193        }
194        return rld;
195    }
196    
197
198    /*
199    public EdoReviewLayerDefinitionBo getReviewLayerDefinition(String workflowId, BigDecimal reviewLayerDefinitionId) {
200        return this.edoReviewLayerDefinitionDao.getReviewLayerDefinition(workflowId, reviewLayerDefinitionId);
201    }*/
202
203    /* no use
204    public EdoReviewLayerDefinition getReviewLayerDefinition(String workflowId, String nodeName, String routeLevel) {
205        EdoReviewLayerDefinitionBo edoReviewLayerDefinitionBo = edoReviewLayerDefinitionDao.getReviewLayerDefinition(workflowId, nodeName, routeLevel);
206        
207        if ( edoReviewLayerDefinitionBo == null){
208                return null;
209        }
210        
211        EdoReviewLayerDefinition.Builder builder = EdoReviewLayerDefinition.Builder.create(edoReviewLayerDefinitionBo);
212        
213        return builder.build();
214    }*/
215
216
217    /* no usages
218    public Map<BigDecimal, EdoReviewLayerDefinitionBo> buildRouteLevelMap(Collection<EdoReviewLayerDefinitionBo> reviewLayerDefinitions) {
219        Map<BigDecimal, EdoReviewLayerDefinitionBo> levelMap = new HashMap<BigDecimal, EdoReviewLayerDefinitionBo>();
220        if (CollectionUtils.isNotEmpty(reviewLayerDefinitions)) {
221            for (EdoReviewLayerDefinitionBo reviewLayerDefinition : reviewLayerDefinitions) {
222                levelMap.put(new BigDecimal(reviewLayerDefinition.getRouteLevel()), reviewLayerDefinition);
223            }
224        }
225        return levelMap;
226    }*/
227
228    /*public Map<String, String> buildNodeMap(Collection<EdoReviewLayerDefinition> reviewLayerDefinitions) {
229        Map<String, String> nodeMap = new HashMap<String, String>();
230        if (CollectionUtils.isNotEmpty(reviewLayerDefinitions)) {
231            for (EdoReviewLayerDefinition reviewLayerDefinition : reviewLayerDefinitions) {
232                nodeMap.put(reviewLayerDefinition.getNodeName(), reviewLayerDefinition.getSuppNodeName());
233            }
234        }
235        return nodeMap;
236    }*/
237    
238    /* public EdoReviewLayerDefinition getReviewLayerDefinitionBySupplementalNode(String suppNodeName) {
239        return this.edoReviewLayerDefinitionDao.getReviewLayerDefinitionBySupplementalNode(suppNodeName);
240        }  */
241
242
243    /* public BigDecimal getMaxRouteLevel() {
244        return this.edoReviewLayerDefinitionDao.getMaxRouteLevel();
245    }  */
246
247    /* public Map<BigDecimal, EdoReviewLayerDefinition> buildIdMap(Collection<EdoReviewLayerDefinition> reviewLayerDefinitions) {
248        Map<BigDecimal, EdoReviewLayerDefinition> idMap = new HashMap<BigDecimal, EdoReviewLayerDefinition>();
249        if (CollectionUtils.isNotEmpty(reviewLayerDefinitions)) {
250            for (EdoReviewLayerDefinition reviewLayerDefinition : reviewLayerDefinitions) {
251                idMap.put(reviewLayerDefinition.getReviewLayerDefinitionId(), reviewLayerDefinition);
252            }
253        }
254        return idMap;
255    }  */
256
257
258    /* public List<String> getValidNodes() {
259        List<EdoReviewLayerDefinition> edoReviewLayerDefinitions = this.edoReviewLayerDefinitionDao.getReviewLayerDefinitions();
260        List<String> validNodes = new LinkedList<String>();
261
262        for (EdoReviewLayerDefinition edoReviewLayerDefinition : edoReviewLayerDefinitions) {
263            validNodes.add(edoReviewLayerDefinition.getNodeName());
264        }
265
266        return validNodes;
267    }  */
268
269    /*no usages of this method found
270    public EdoReviewLayerDefinitionBo getReviewLayerDefinition(BigDecimal routeLevel, List<EdoReviewLayerDefinitionBo> reviewLayerDefinitions) {
271        if (CollectionUtils.isNotEmpty(reviewLayerDefinitions) && routeLevel != null) {
272            for (EdoReviewLayerDefinitionBo reviewLayerDefinition : reviewLayerDefinitions) {
273                if (reviewLayerDefinition.getRouteLevel().equals(routeLevel)) {
274                    return reviewLayerDefinition;
275                }
276            }
277        }
278        return null;
279    }*/
280
281    /*no usages of this method found
282    public EdoReviewLayerDefinitionBo getReviewLayerDefinition(String nodeName, List<EdoReviewLayerDefinitionBo> reviewLayerDefinitions) {
283        if (CollectionUtils.isNotEmpty(reviewLayerDefinitions) && StringUtils.isNotEmpty(nodeName)) {
284            for (EdoReviewLayerDefinitionBo reviewLayerDefinition : reviewLayerDefinitions) {
285                if (reviewLayerDefinition.getNodeName().equals(nodeName)) {
286                    return reviewLayerDefinition;
287                }
288            }
289        }
290        return null;
291    }*/
292
293
294    //public EdoReviewLayerDefinition getReviewLayerDefinition(String nodeName) {
295    //    return this.edoReviewLayerDefinitionDao.getReviewLayerDefinition(nodeName);
296    //}
297
298}