import org.splat.dal.dao.som.DocumentDAO;
import org.splat.dal.dao.som.DocumentTypeDAO;
import org.splat.dal.dao.som.FileDAO;
+import org.splat.dal.dao.som.StudyDAO;
import org.splat.kernel.InvalidPropertyException;
import org.splat.kernel.MissedPropertyException;
import org.splat.kernel.NotApplicableException;
+import org.splat.log.AppLogger;
import org.splat.manox.Reader;
import org.splat.manox.Toolbox;
import org.splat.service.technical.ProjectSettingsService;
-import org.splat.service.technical.ProjectSettingsServiceImpl;
import org.splat.service.technical.RepositoryService;
import org.splat.service.technical.ProjectSettingsServiceImpl.FileNaming;
import org.splat.som.Revision;
*/
public class DocumentServiceImpl implements DocumentService {
+ /**
+ * The logger for the service.
+ */
+ public final static AppLogger LOG = AppLogger
+ .getLogger(DocumentServiceImpl.class);
+
/**
* Injected study service.
*/
/**
* Injected project settings service.
*/
- private ProjectSettingsService _projectSettingsService;
+ private ProjectSettingsService _projectSettings;
/**
* Injected repository service.
*/
* Injected file DAO.
*/
private FileDAO _fileDAO;
+ /**
+ * Injected study DAO.
+ */
+ private StudyDAO _studyDAO;
/**
* {@inheritDoc}
* @see org.splat.service.DocumentService#selectDocument(long)
*/
@Transactional(readOnly = true)
- public Document selectDocument(long index) {
+ public Document selectDocument(final long index) {
return getDocumentDAO().get(index);
}
* @see org.splat.service.DocumentService#selectDocument(java.lang.String, java.lang.String)
*/
@Transactional(readOnly = true)
- public Document selectDocument(String refid, String version) {
+ public Document selectDocument(final String refid, final String version) {
Criterion aCondition = Restrictions.and(Restrictions.eq("did", refid),
Restrictions.eq("version", version));
return getDocumentDAO().findByCriteria(aCondition);
* @see org.splat.service.DocumentService#generateDocumentId(org.splat.dal.bo.som.Document, org.splat.dal.bo.som.Document.Properties)
*/
@Transactional
- public void generateDocumentId(Document aDoc, Properties dprop) {
+ public void generateDocumentId(final Document aDoc, final Properties dprop) {
Study owner = null;
- if (dprop.getOwner() instanceof Study)
+ if (dprop.getOwner() instanceof Study) {
owner = (Study) dprop.getOwner();
- else
+ } else {
owner = ((Scenario) dprop.getOwner()).getOwnerStudy();
+ }
+
+ // Synchronize the object with the current Hibernate session.
+ owner = getStudyDAO().get(owner.getIndex());
- SimpleDateFormat tostring = new SimpleDateFormat("yyyy");
+ SimpleDateFormat tostring = new SimpleDateFormat("yyyy"); //RKV: NOPMD: TODO: Use locale here?
String year = tostring.format(owner.getDate());
String filename = generateEncodedName(aDoc, owner);
String path = owner.getReference();
- ProjectSettingsService.Step step = ProjectSettingsServiceImpl
- .getStep(aDoc.getStep());
- aDoc.setDid(new StringBuffer(path).append(".%")
- .append(Document.suformat).toString()); // Document reference
+ ProjectSettingsService.Step step = getProjectSettings().getStep(
+ aDoc.getStep());
+ aDoc.setDid(new StringBuffer(path).append(".%").append(
+ Document.suformat).toString()); // Document reference
path = new StringBuffer(year).append("/").append(path).append("/")
.append(step.getPath())
// File path relative to the repository vault
* the study
* @return document reference name
*/
- private String generateEncodedName(Document aDoc, Study scope) {
+ private String generateEncodedName(final Document aDoc, final Study scope) {
StringBuffer encoding = new StringBuffer();
FileNaming scheme = getProjectSettings().getFileNamingScheme();
DecimalFormat tostring = new DecimalFormat(Document.suformat);
int number = getStudyService().generateLocalIndex(scope);
if (scheme == FileNaming.encoded) {
- encoding.append(scope.getReference()).append(".")
- .append(tostring.format(number));
+ encoding.append(scope.getReference()).append(".").append(
+ tostring.format(number));
} else { // title and (temporarily) asis
- encoding.append(aDoc.getTitle()).append(".")
- .append(tostring.format(number));
+ encoding.append(aDoc.getTitle()).append(".").append(
+ tostring.format(number));
}
return encoding.toString();
}
* the study
* @return file name
*/
- private String getEncodedRootName(Document aDoc, Study scope) {
+ private String getEncodedRootName(final Document aDoc, final Study scope) {
FileNaming scheme = getProjectSettings().getFileNamingScheme();
- if (scheme == FileNaming.encoded)
+ if (scheme == FileNaming.encoded) {
return scope.getReference();
- else
+ } else {
return aDoc.getTitle();
+ }
}
/**
* @see org.splat.service.DocumentService#initialize(org.splat.dal.bo.som.Document, org.splat.dal.bo.som.Document.Properties)
*/
@Transactional
- public void initialize(Document aDoc, Properties dprop)
+ public void initialize(final Document aDoc, final Properties dprop)
throws MissedPropertyException, InvalidPropertyException,
NotApplicableException {
- if (!aDoc.isUndefined())
+ if (!aDoc.isUndefined()) {
throw new NotApplicableException(
"Cannot initialize an existing Document");
- if (dprop.getName() == null)
+ }
+ if (dprop.getName() == null) {
throw new MissedPropertyException("name");
- if (dprop.getName().length() == 0)
+ }
+ if (dprop.getName().length() == 0) {
throw new InvalidPropertyException("name");
- if (dprop.getOwner() == null)
+ }
+ if (dprop.getOwner() == null) {
throw new MissedPropertyException("owner");
+ }
// if (dprop.owner instanceof Study && !ProjectSettings.getStep(step).appliesTo(Study.class)) {
// throw new InvalidPropertyException("step");
// }
aDoc.setTitle(dprop.getName());
aDoc.getFile().changePath(
- aDoc.getFile()
- .getRelativePath()
- .replace(
- "%n",
- getEncodedRootName(aDoc,
- (Study) dprop.getOwner())));
- if (aDoc.getHistory() == -1)
+ aDoc.getFile().getRelativePath().replace("%n",
+ getEncodedRootName(aDoc, (Study) dprop.getOwner())));
+ if (aDoc.getHistory() == -1) {
aDoc.setHistory(0);
+ }
if (dprop.getDate() == null) {
Calendar current = Calendar.getInstance();
aDoc.setLastModificationDate(current.getTime()); // Today
*
* @see org.splat.service.DocumentService#getSaveDirectory(org.splat.dal.bo.som.Document)
*/
- public java.io.File getSaveDirectory(Document aDoc) {
+ public java.io.File getSaveDirectory(final Document aDoc) {
String mypath = getRepositoryService().getRepositoryVaultPath()
+ aDoc.getSourceFile().getRelativePath();
String[] table = mypath.split("/");
// Cutting the filename
StringBuffer path = new StringBuffer(table[0]);
- for (int i = 1; i < table.length - 1; i++)
+ for (int i = 1; i < table.length - 1; i++) {
path = path.append("/").append(table[i]);
+ }
return new java.io.File(path.append("/").toString());
}
* the file to parse
* @return the extracted properties
*/
- public Properties extractProperties(java.io.File file) {
+ public Properties extractProperties(final java.io.File file) {
Properties fprop = new Properties();
Reader tool = Toolbox.getReader(file);
String value;
- if (tool != null)
+ if (tool != null) {
try {
value = tool.extractProperty("title");
- if (value != null)
+ if (value != null) {
fprop.setName(value);
+ }
value = tool.extractProperty("reference");
- if (value != null)
+ if (value != null) {
fprop.setReference(value);
+ }
} catch (Exception e) {
+ LOG.debug(e.getMessage(), e);
}
+ }
return fprop;
}
* the format
* @return the created "Converts" relation
*/
- public ConvertsRelation attach(Document aDoc, String format) {
+ public ConvertsRelation attach(final Document aDoc, final String format) {
return attach(aDoc, format, null);
}
* @return the created "Converts" relation
*/
@Transactional
- public ConvertsRelation attach(Document aDoc, String format,
- String description) {
+ public ConvertsRelation attach(final Document aDoc, final String format,
+ final String description) {
String path = aDoc.getRelativePath();
File export = new File(path + "." + format);
ConvertsRelation attach = new ConvertsRelation(aDoc, export,
description);
- // RKV Session session = Database.getSession();
// RKV session.save(export);
// RKV session.save(attach);
* the original document
* @return true if the new reference is set
*/
- public boolean buildReferenceFrom(Document aDoc, ProjectElement scope,
- Document lineage) {
- if (aDoc.getProgressState() != ProgressState.inWORK)
+ public boolean buildReferenceFrom(final Document aDoc,
+ final ProjectElement scope, final Document lineage) {
+ if (aDoc.getProgressState() != ProgressState.inWORK) {
return false;
+ }
Study owner = null;
Scenario context = null;
- if (scope instanceof Study)
+ if (scope instanceof Study) {
owner = (Study) scope;
- else {
+ } else {
context = ((Scenario) scope);
owner = context.getOwnerStudy();
}
* the study
* @return true if the new reference is set
*/
- public boolean buildReferenceFrom(Document aDoc, Study scope) {
+ public boolean buildReferenceFrom(final Document aDoc, final Study scope) {
if (aDoc.getProgressState() != ProgressState.inWORK
- && aDoc.getProgressState() != ProgressState.EXTERN)
+ && aDoc.getProgressState() != ProgressState.EXTERN) {
return false;
+ }
DecimalFormat tostring = new DecimalFormat(Document.suformat);
aDoc.setDid(aDoc.getDid().replace("%" + Document.suformat,
* @return true if demoting succeeded
*/
@Transactional
- public boolean demote(Document aDoc) {
+ public boolean demote(final Document aDoc) {
ValidationStep torem;
if (aDoc.getProgressState() == ProgressState.inCHECK) {
for (Iterator<Relation> i = aDoc.getAllRelations().iterator(); i
.hasNext();) {
Relation link = i.next();
- if (!(link instanceof StampRelation))
+ if (!(link instanceof StampRelation)) {
continue;
- if (((StampRelation) link).getStampType() != torem)
+ }
+ if (((StampRelation) link).getStampType() != torem) {
continue;
+ }
i.remove();
break;
}
* @return true if promotion succeeded
*/
@Transactional
- public boolean promote(Document aDoc, Timestamp stamp) {
+ public boolean promote(final Document aDoc, final Timestamp stamp) {
ProgressState newstate = null;
if (aDoc.getProgressState() == ProgressState.inWORK) {
* @see #release()
*/
@Transactional
- public void hold(Document aDoc) {
+ public void hold(final Document aDoc) {
aDoc.setCountag(aDoc.getCountag() + 1);
if (aDoc.isSaved()) {
getDocumentDAO().update(aDoc);
* @see #hold()
*/
@Transactional
- public void release(Document aDoc) {
+ public void release(final Document aDoc) {
aDoc.setCountag(aDoc.getCountag() - 1);
if (aDoc.isSaved()) {
- getDocumentDAO().update(aDoc);
+ getDocumentDAO().merge(aDoc);
}
}
* if the new title is empty
*/
@Transactional
- public void rename(Document aDoc, String title)
+ public void rename(final Document aDoc, final String title)
throws InvalidPropertyException {
- if (title.length() == 0)
+ if (title.length() == 0) {
throw new InvalidPropertyException("name");
+ }
Calendar current = Calendar.getInstance();
aDoc.setTitle(title);
* @param newvers
* the new version
*/
- public void updateAs(Document aDoc, Revision newvers) {
+ public void updateAs(final Document aDoc, final Revision newvers) {
aDoc.setVersion(newvers.setBranch(aDoc.getVersion()).toString()); // Branch names are propagated by the versionning
ProgressState newstate = ProgressState.inCHECK;
- if (newvers.isMinor())
+ if (newvers.isMinor()) {
newstate = ProgressState.inWORK;
+ }
aDoc.setProgressState(null); // Just to tell updateAs(state) to not increment the version number
updateAs(aDoc, newstate);
}
* the new state
*/
@Transactional
- public void updateAs(Document aDoc, ProgressState state) {
+ public void updateAs(final Document aDoc, final ProgressState state) {
Document previous = null;
// Set of version number
if (state == ProgressState.EXTERN) {
- if (aDoc.getProgressState() != ProgressState.EXTERN)
+ if (aDoc.getProgressState() != ProgressState.EXTERN) {
aDoc.setVersion(null); // Strange use-case...
+ }
} else {
Revision myvers = new Revision(aDoc.getVersion());
if (!myvers.isNull()) { // Versionning context
for (Iterator<Relation> i = aDoc.getAllRelations().iterator(); i
.hasNext();) {
Relation link = i.next();
- if (!link.getClass().equals(VersionsRelation.class))
- continue;
- previous = (Document) link.getTo(); // Versioned document
- break;
+ if (link.getClass().equals(VersionsRelation.class)) {
+ previous = (Document) link.getTo(); // Versioned document
+ break;
+ }
}
}
- if (aDoc.getProgressState() != null)
+ if (aDoc.getProgressState() != null) {
myvers.incrementAs(state); // Incrementation if the reversion number is not imposed
+ }
aDoc.setVersion(myvers.toString());
}
// Update this document and the previous version, if exit
if (previous != null) {
previous.setHistory(previous.getHistory() + 1);
- getDocumentDAO().update(previous);
+ getDocumentDAO().merge(previous);
}
aDoc.setProgressState(state);
getDocumentDAO().update(aDoc);
* @see #isStepResult()
* @see #isResultOf(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
*/
- public boolean isStudyResult(DocumentType aType) {
- // -------------------------------
+ public boolean isStudyResult(final DocumentType aType) {
List<ProjectSettingsService.Step> step = getProjectSettings()
.getAllSteps();
ProjectSettingsService.Step lastep = step.get(step.size() - 1);
/**
* Get document by its path.
- * @param path the document path
+ *
+ * @param path
+ * the document path
* @return the document if found or null
*/
- @Transactional(readOnly=true)
+ @Transactional(readOnly = true)
public Document getDocumentByPath(String path) {
String[] parse = path.split("'");
* @param studyService
* the studyService to set
*/
- public void setStudyService(StudyService studyService) {
+ public void setStudyService(final StudyService studyService) {
_studyService = studyService;
}
* @return Project settings service
*/
private ProjectSettingsService getProjectSettings() {
- return _projectSettingsService;
+ return _projectSettings;
}
/**
* @param projectSettingsService
* project settings service
*/
- public void setProjectSettings(ProjectSettingsService projectSettingsService) {
- _projectSettingsService = projectSettingsService;
+ public void setProjectSettings(
+ final ProjectSettingsService projectSettingsService) {
+ _projectSettings = projectSettingsService;
}
/**
* @param documentDAO
* the documentDAO to set
*/
- public void setDocumentDAO(DocumentDAO documentDAO) {
+ public void setDocumentDAO(final DocumentDAO documentDAO) {
_documentDAO = documentDAO;
}
* @param repositoryService
* the repositoryService to set
*/
- public void setRepositoryService(RepositoryService repositoryService) {
+ public void setRepositoryService(final RepositoryService repositoryService) {
_repositoryService = repositoryService;
}
* @param documentTypeDAO
* the documentTypeDAO to set
*/
- public void setDocumentTypeDAO(DocumentTypeDAO documentTypeDAO) {
+ public void setDocumentTypeDAO(final DocumentTypeDAO documentTypeDAO) {
_documentTypeDAO = documentTypeDAO;
}
* @param fileDAO
* the fileDAO to set
*/
- public void setFileDAO(FileDAO fileDAO) {
+ public void setFileDAO(final FileDAO fileDAO) {
_fileDAO = fileDAO;
}
+ /**
+ * Get the studyDAO.
+ *
+ * @return the studyDAO
+ */
+ public StudyDAO getStudyDAO() {
+ return _studyDAO;
+ }
+
+ /**
+ * Set the studyDAO.
+ *
+ * @param studyDAO
+ * the studyDAO to set
+ */
+ public void setStudyDAO(final StudyDAO studyDAO) {
+ _studyDAO = studyDAO;
+ }
+
}