1 /*****************************************************************************
5 * Creation date 06.10.2012
8 *****************************************************************************/
10 package org.splat.service;
12 import java.util.Collection;
13 import java.util.Vector;
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;
28 * Knowledge element service implementation.
30 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
32 public class KnowledgeElementServiceImpl implements KnowledgeElementService {
35 * The logger for the service.
37 public final static AppLogger LOG = AppLogger
38 .getLogger(KnowledgeElementServiceImpl.class);
41 * Injected index service.
43 private IndexService _indexService;
45 * Injected knowledge element DAO.
47 private KnowledgeElementDAO _knowledgeElementDAO;
49 * Injected study service.
51 private StudyService _studyService;
53 * Injected project element service.
55 private ProjectElementService _projectElementService;
60 * @see org.splat.service.KnowledgeElementService#approve(org.splat.dal.bo.som.KnowledgeElement)
63 public boolean approve(final KnowledgeElement knowledgeElement) {
64 if (knowledgeElement.getProgressState() != ProgressState.inCHECK) {
67 knowledgeElement.setProgressState(ProgressState.APPROVED);
68 return update(knowledgeElement);
74 * @see org.splat.service.KnowledgeElementService#demote(org.splat.dal.bo.som.KnowledgeElement)
77 public boolean demote(final KnowledgeElement knowledgeElement) {
78 if (knowledgeElement.getProgressState() != ProgressState.APPROVED
79 && knowledgeElement.getProgressState() != ProgressState.inCHECK) {
82 knowledgeElement.setProgressState(ProgressState.inDRAFT);
83 return update(knowledgeElement);
87 * Update knowledge element in the database and in the lucene index.
89 * @param knowledgeElement
90 * the knowledge element to update
91 * @return true if updating succeeded
93 protected boolean update(final KnowledgeElement knowledgeElement) {
95 getKnowledgeElementDAO().update(knowledgeElement);
96 getIndexService().update(knowledgeElement);
98 } catch (Exception error) {
99 LOG.error("Unable to re-index the knowledge '"
100 + knowledgeElement.getIndex() + "', reason:", error);
108 * @see org.splat.service.KnowledgeElementService#promote(org.splat.dal.bo.som.KnowledgeElement)
111 public boolean promote(final KnowledgeElement knowledgeElement) {
112 if (knowledgeElement.getProgressState() != ProgressState.inDRAFT) {
115 knowledgeElement.setProgressState(ProgressState.inCHECK);
116 return update(knowledgeElement);
122 * @see org.splat.service.KnowledgeElementService#rename(org.splat.dal.bo.som.KnowledgeElement, java.lang.String)
125 public void rename(final KnowledgeElementDTO kelmDTO, final String title)
126 throws InvalidPropertyException {
127 if (title.length() == 0) {
128 throw new InvalidPropertyException("name");
130 KnowledgeElement kelm = getKnowledgeElementDAO().get(kelmDTO.getIndex());
131 kelm.setTitle(title);
132 // Update lucene index.
137 * Update the description of the knowledge element.
140 * the knoledge element to update
142 * the new description
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>");
152 kelm.setValue(text.append(kelm.getValue().substring(0, index))
154 .append(kelm.getValue().substring(index)).toString());
156 kelm.setValue(text.append(kelm.getValue()).append("</p>")
165 * @see org.splat.service.KnowledgeElementService#selectKnowledgeElement(long)
167 @Transactional(readOnly = true)
168 public KnowledgeElement selectKnowledgeElement(final long index) {
169 KnowledgeElement result = getKnowledgeElementDAO().get(index);
170 getStudyService().loadWorkflow(
171 result.getOwnerScenario().getOwnerStudy());
178 * @see org.splat.service.KnowledgeElementService#getKnowledgeElement(long)
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()));
193 * Get all steps of the scenario.
194 * @param scenar the scenario
195 * @return collection of steps
197 private Collection<? extends Step> getAllSteps(final Scenario scenar) {
198 Vector<Step> result = new Vector<Step>();
199 Step[] step = getProjectElementService().getSteps(scenar);
201 int base = step[0].getNumber();
202 int last = step[step.length - 1].getNumber();
203 for (int i = 0; i < step.length; i++) {
206 step = getProjectElementService().getSteps(scenar.getOwnerStudy());
207 for (int i = step.length - 1; i > -1; i--) {
208 if (step[i].getNumber() >= base) {
211 result.add(0, step[i]);
213 for (int i = 0; i < step.length; i++) {
214 if (step[i].getNumber() <= last) {
223 * Get the indexService.
225 * @return the indexService
227 public IndexService getIndexService() {
228 return _indexService;
232 * Set the indexService.
234 * @param indexService
235 * the indexService to set
237 public void setIndexService(final IndexService indexService) {
238 _indexService = indexService;
242 * Get the knowledgeElementDAO.
244 * @return the knowledgeElementDAO
246 public KnowledgeElementDAO getKnowledgeElementDAO() {
247 return _knowledgeElementDAO;
251 * Set the knowledgeElementDAO.
253 * @param knowledgeElementDAO
254 * the knowledgeElementDAO to set
256 public void setKnowledgeElementDAO(final KnowledgeElementDAO knowledgeElementDAO) {
257 _knowledgeElementDAO = knowledgeElementDAO;
261 * Get the studyService.
263 * @return the studyService
265 public StudyService getStudyService() {
266 return _studyService;
270 * Set the studyService.
272 * @param studyService
273 * the studyService to set
275 public void setStudyService(final StudyService studyService) {
276 _studyService = studyService;
280 * Get the projectElementService.
281 * @return the projectElementService
283 public ProjectElementService getProjectElementService() {
284 return _projectElementService;
288 * Set the projectElementService.
289 * @param projectElementService the projectElementService to set
291 public void setProjectElementService(final ProjectElementService projectElementService) {
292 _projectElementService = projectElementService;