]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/org/splat/service/KnowledgeElementServiceImpl.java
Salome HOME
The checkin from SALOME operation is updated (ScenarioService.checkin()). Versioning...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / KnowledgeElementServiceImpl.java
1 /*****************************************************************************
2  * Company         OPEN CASCADE
3  * Application     SIMAN
4  * File            $Id$ 
5  * Creation date   06.10.2012
6  * @author         $Author$
7  * @version        $Revision$
8  *****************************************************************************/
9
10 package org.splat.service;
11
12 import java.util.Collection;
13 import java.util.Vector;
14
15 import org.splat.dal.bo.som.KnowledgeElement;
16 import org.splat.dal.bo.som.ProgressState;
17 import org.splat.dal.bo.som.Scenario;
18 import org.splat.dal.dao.som.KnowledgeElementDAO;
19 import org.splat.kernel.InvalidPropertyException;
20 import org.splat.log.AppLogger;
21 import org.splat.service.dto.KnowledgeElementDTO;
22 import org.splat.service.technical.IndexService;
23 import org.splat.som.Step;
24 import org.splat.util.BeanHelper;
25 import org.springframework.transaction.annotation.Transactional;
26
27 /**
28  * Knowledge element service implementation.
29  * 
30  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
31  */
32 public class KnowledgeElementServiceImpl implements KnowledgeElementService {
33
34         /**
35          * The logger for the service.
36          */
37         public final static AppLogger LOG = AppLogger
38                         .getLogger(KnowledgeElementServiceImpl.class);
39
40         /**
41          * Injected index service.
42          */
43         private IndexService _indexService;
44         /**
45          * Injected knowledge element DAO.
46          */
47         private KnowledgeElementDAO _knowledgeElementDAO;
48         /**
49          * Injected study service.
50          */
51         private StudyService _studyService;
52         /**
53          * Injected project element service.
54          */
55         private ProjectElementService _projectElementService;
56
57         /**
58          * {@inheritDoc}
59          * 
60          * @see org.splat.service.KnowledgeElementService#approve(org.splat.dal.bo.som.KnowledgeElement)
61          */
62         @Transactional
63         public boolean approve(final KnowledgeElement knowledgeElement) {
64                 if (knowledgeElement.getProgressState() != ProgressState.inCHECK) {
65                         return false;
66                 }
67                 knowledgeElement.setProgressState(ProgressState.APPROVED);
68                 return update(knowledgeElement);
69         }
70
71         /**
72          * {@inheritDoc}
73          * 
74          * @see org.splat.service.KnowledgeElementService#demote(org.splat.dal.bo.som.KnowledgeElement)
75          */
76         @Transactional
77         public boolean demote(final KnowledgeElement knowledgeElement) {
78                 if (knowledgeElement.getProgressState() != ProgressState.APPROVED
79                                 && knowledgeElement.getProgressState() != ProgressState.inCHECK) {
80                         return false;
81                 }
82                 knowledgeElement.setProgressState(ProgressState.inDRAFT);
83                 return update(knowledgeElement);
84         }
85
86         /**
87          * Update knowledge element in the database and in the lucene index.
88          * 
89          * @param knowledgeElement
90          *            the knowledge element to update
91          * @return true if updating succeeded
92          */
93         protected boolean update(final KnowledgeElement knowledgeElement) {
94                 try {
95                         getKnowledgeElementDAO().update(knowledgeElement);
96                         getIndexService().update(knowledgeElement);
97                         return true;
98                 } catch (Exception error) {
99                         LOG.error("Unable to re-index the knowledge '"
100                                         + knowledgeElement.getIndex() + "', reason:", error);
101                         return false;
102                 }
103         }
104
105         /**
106          * {@inheritDoc}
107          * 
108          * @see org.splat.service.KnowledgeElementService#promote(org.splat.dal.bo.som.KnowledgeElement)
109          */
110         @Transactional
111         public boolean promote(final KnowledgeElement knowledgeElement) {
112                 if (knowledgeElement.getProgressState() != ProgressState.inDRAFT) {
113                         return false;
114                 }
115                 knowledgeElement.setProgressState(ProgressState.inCHECK);
116                 return update(knowledgeElement);
117         }
118
119         /**
120          * {@inheritDoc}
121          * 
122          * @see org.splat.service.KnowledgeElementService#rename(org.splat.dal.bo.som.KnowledgeElement, java.lang.String)
123          */
124         @Transactional
125         public void rename(final KnowledgeElementDTO kelmDTO, final String title)
126                         throws InvalidPropertyException {
127                 if (title.length() == 0) {
128                         throw new InvalidPropertyException("name");
129                 }
130                 KnowledgeElement kelm = getKnowledgeElementDAO().get(kelmDTO.getIndex());
131                 kelm.setTitle(title);
132                 // Update lucene index.
133                 update(kelm);
134         }
135
136         /**
137          * Update the description of the knowledge element.
138          * 
139          * @param kelm
140          *            the knoledge element to update
141          * @param description
142          *            the new description
143          */
144         @Transactional
145         public void update(final KnowledgeElementDTO kelmDTO, final String description) {
146                 KnowledgeElement kelm = getKnowledgeElementDAO().get(kelmDTO.getIndex());
147                 kelm.setValue(description.trim());
148                 if (!kelm.getValue().startsWith("<p>")) {
149                         StringBuffer text = new StringBuffer("<p>");
150                         int index = kelm.getValue().indexOf("<p>");
151                         if (index > 0) {
152                                 kelm.setValue(text.append(kelm.getValue().substring(0, index))
153                                                 .append("</p>")
154                                                 .append(kelm.getValue().substring(index)).toString());
155                         } else {
156                                 kelm.setValue(text.append(kelm.getValue()).append("</p>")
157                                                 .toString());
158                         }
159                 }
160         }
161
162         /**
163          * {@inheritDoc}
164          * 
165          * @see org.splat.service.KnowledgeElementService#selectKnowledgeElement(long)
166          */
167         @Transactional(readOnly = true)
168         public KnowledgeElement selectKnowledgeElement(final long index) {
169                 KnowledgeElement result = getKnowledgeElementDAO().get(index);
170                 getStudyService().loadWorkflow(
171                                 result.getOwnerScenario().getOwnerStudy());
172                 return result;
173         }
174
175         /**
176          * {@inheritDoc}
177          * 
178          * @see org.splat.service.KnowledgeElementService#getKnowledgeElement(long)
179          */
180         @Transactional(readOnly = true)
181         public KnowledgeElementDTO getKnowledgeElement(final long index) {
182                 KnowledgeElement kelm = getKnowledgeElementDAO().get(index);
183                 getStudyService().loadWorkflow(kelm.getOwnerScenario().getOwnerStudy());
184                 KnowledgeElementDTO result = BeanHelper.copyBean(kelm,
185                                 KnowledgeElementDTO.class);
186                 result.setScenarioTitle(kelm.getOwnerScenario().getTitle());
187                 result.setStudyTitle(kelm.getOwnerScenario().getOwnerStudy().getTitle());
188                 result.getInvolving().addAll(getAllSteps(kelm.getOwnerScenario()));
189                 return result;
190         }
191
192         /**
193          * Get all steps of the scenario.
194          * @param scenar the scenario
195          * @return collection of steps
196          */
197         private Collection<? extends Step> getAllSteps(final Scenario scenar) {
198                 Vector<Step> result = new Vector<Step>();
199                 Step[] step = getProjectElementService().getSteps(scenar);
200
201                 int base = step[0].getNumber();
202                 int last = step[step.length - 1].getNumber();
203                 for (int i = 0; i < step.length; i++) {
204                         result.add(step[i]);
205                 }
206                 step = getProjectElementService().getSteps(scenar.getOwnerStudy());
207                 for (int i = step.length - 1; i > -1; i--) {
208                         if (step[i].getNumber() >= base) {
209                                 continue;
210                         }
211                         result.add(0, step[i]);
212                 }
213                 for (int i = 0; i < step.length; i++) {
214                         if (step[i].getNumber() <= last) {
215                                 continue;
216                         }
217                         result.add(step[i]);
218                 }
219                 return result;
220         }
221
222         /**
223          * Get the indexService.
224          * 
225          * @return the indexService
226          */
227         public IndexService getIndexService() {
228                 return _indexService;
229         }
230
231         /**
232          * Set the indexService.
233          * 
234          * @param indexService
235          *            the indexService to set
236          */
237         public void setIndexService(final IndexService indexService) {
238                 _indexService = indexService;
239         }
240
241         /**
242          * Get the knowledgeElementDAO.
243          * 
244          * @return the knowledgeElementDAO
245          */
246         public KnowledgeElementDAO getKnowledgeElementDAO() {
247                 return _knowledgeElementDAO;
248         }
249
250         /**
251          * Set the knowledgeElementDAO.
252          * 
253          * @param knowledgeElementDAO
254          *            the knowledgeElementDAO to set
255          */
256         public void setKnowledgeElementDAO(final KnowledgeElementDAO knowledgeElementDAO) {
257                 _knowledgeElementDAO = knowledgeElementDAO;
258         }
259
260         /**
261          * Get the studyService.
262          * 
263          * @return the studyService
264          */
265         public StudyService getStudyService() {
266                 return _studyService;
267         }
268
269         /**
270          * Set the studyService.
271          * 
272          * @param studyService
273          *            the studyService to set
274          */
275         public void setStudyService(final StudyService studyService) {
276                 _studyService = studyService;
277         }
278
279         /**
280          * Get the projectElementService.
281          * @return the projectElementService
282          */
283         public ProjectElementService getProjectElementService() {
284                 return _projectElementService;
285         }
286
287         /**
288          * Set the projectElementService.
289          * @param projectElementService the projectElementService to set
290          */
291         public void setProjectElementService(final ProjectElementService projectElementService) {
292                 _projectElementService = projectElementService;
293         }
294 }