Skip to content

Commit 288bcac

Browse files
committed
ContextInitializer
1 parent ba748b4 commit 288bcac

File tree

27 files changed

+148
-33
lines changed

27 files changed

+148
-33
lines changed

logicaldoc-cmis/src/test/java/com/logicaldoc/cmis/LDRepositoryTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ public void setUp() throws IOException, SQLException, PluginException {
9090

9191
engine = (SearchEngine) context.getBean("SearchEngine");
9292

93-
fdao = (FolderDAO) context.getBean("FolderDAO");
93+
fdao = (FolderDAO) context.getBean("folderDAO");
9494

95-
ddao = (DocumentDAO) context.getBean("DocumentDAO");
95+
ddao = (DocumentDAO) context.getBean("documentDAO");
9696

9797
try {
9898
addHits();
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.logicaldoc.core;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.sql.SQLException;
6+
import java.util.concurrent.ExecutionException;
7+
8+
import javax.annotation.Resource;
9+
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
import org.springframework.beans.BeanUtils;
13+
import org.springframework.context.ApplicationListener;
14+
import org.springframework.context.event.ContextRefreshedEvent;
15+
16+
import com.logicaldoc.core.document.Document;
17+
import com.logicaldoc.core.document.DocumentDAO;
18+
import com.logicaldoc.core.document.DocumentHistory;
19+
import com.logicaldoc.core.document.DocumentManager;
20+
import com.logicaldoc.core.folder.Folder;
21+
import com.logicaldoc.core.folder.FolderDAO;
22+
import com.logicaldoc.core.folder.FolderHistory;
23+
import com.logicaldoc.core.security.Tenant;
24+
import com.logicaldoc.util.config.ContextProperties;
25+
import com.logicaldoc.util.io.FileUtil;
26+
27+
/**
28+
* A super class for those initializers that prepare the environment for
29+
* execution when the Context has been initialized and the database connected.
30+
*
31+
* @author Marco Meschieri - LogicalDOC
32+
* @since 9.2
33+
*/
34+
public abstract class ContextInitializer implements ApplicationListener<ContextRefreshedEvent> {
35+
36+
private static final Logger log = LoggerFactory.getLogger(ContextInitializer.class);
37+
38+
@Resource(name = "ContextProperties")
39+
protected ContextProperties config;
40+
41+
@Resource(name = "documentManager")
42+
protected DocumentManager documentManager;
43+
44+
@Resource(name = "documentDAO")
45+
protected DocumentDAO documentDAO;
46+
47+
@Resource(name = "folderDAO")
48+
protected FolderDAO folderDAO;
49+
50+
/**
51+
* Concrete implementations must override this method to put their
52+
* initialization logic.
53+
*
54+
* @throws IOException Generic I/O error
55+
* @throws SQLException Error in the data layer
56+
*/
57+
protected abstract void initialize() throws IOException, SQLException;
58+
59+
@Override
60+
public final void onApplicationEvent(ContextRefreshedEvent event) {
61+
if (!isDbInitialized())
62+
return;
63+
64+
try {
65+
initialize();
66+
} catch (Exception e) {
67+
log.error(e.getMessage(), e);
68+
}
69+
}
70+
71+
private boolean isDbInitialized() {
72+
try {
73+
return documentDAO.queryForLong("select count(*) from ld_user") > 0L;
74+
} catch (Exception e) {
75+
return false;
76+
}
77+
}
78+
79+
/**
80+
* Creates a folder
81+
*
82+
* @param path The path
83+
* @param transaction The current transaction
84+
*
85+
* @return The created folder
86+
*
87+
* @throws PersistenceException Error in the data layer
88+
*/
89+
protected Folder prepareFolder(String path, FolderHistory transaction) throws PersistenceException {
90+
Folder root = folderDAO.findRoot(transaction.getTenantId());
91+
root = folderDAO.createPath(root, path, false, transaction);
92+
return root;
93+
}
94+
95+
protected Document prepareDocument(String resourcePath, String documentPath, DocumentHistory transaction)
96+
throws PersistenceException, InterruptedException, ExecutionException, IOException {
97+
FolderHistory fHist = new FolderHistory();
98+
BeanUtils.copyProperties(transaction, fHist);
99+
fHist.setId(0);
100+
Folder target = prepareFolder(FileUtil.getPath(documentPath), fHist);
101+
String fileName = FileUtil.getName(documentPath);
102+
Document doc = documentDAO
103+
.findByFileNameAndParentFolderId(target.getId(), fileName, null, Tenant.DEFAULT_ID, null).stream()
104+
.findFirst().orElse(null);
105+
if (doc == null)
106+
try (InputStream is = this.getClass().getResourceAsStream(resourcePath)) {
107+
Document trainingDoc = new Document();
108+
trainingDoc.setFileName(fileName);
109+
trainingDoc.setFolder(target);
110+
trainingDoc.setLanguage("en");
111+
doc = documentManager.create(is, trainingDoc, transaction).get();
112+
}
113+
return doc;
114+
}
115+
}

logicaldoc-core/src/main/java/com/logicaldoc/core/document/DigestProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
public class DigestProcessor extends Task {
2525
public static final String NAME = "DigestProcessor";
2626

27-
@Resource(name = "DocumentDAO")
27+
@Resource(name = "documentDAO")
2828
protected DocumentDAO documentDao;
2929

3030
private long processed = 0;

logicaldoc-core/src/main/java/com/logicaldoc/core/document/DocumentManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public class DocumentManager {
9595

9696
private static final Logger log = LoggerFactory.getLogger(DocumentManager.class);
9797

98-
@Resource(name = "DocumentDAO")
98+
@Resource(name = "documentDAO")
9999
protected DocumentDAO documentDAO;
100100

101101
@Resource(name = "DocumentLinkDAO")
@@ -104,7 +104,7 @@ public class DocumentManager {
104104
@Resource(name = "DocumentNoteDAO")
105105
protected DocumentNoteDAO documentNoteDAO;
106106

107-
@Resource(name = "FolderDAO")
107+
@Resource(name = "folderDAO")
108108
protected FolderDAO folderDAO;
109109

110110
@Resource(name = "TemplateDAO")

logicaldoc-core/src/main/java/com/logicaldoc/core/document/HibernateDocumentDAO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public class HibernateDocumentDAO extends HibernatePersistentObjectDAO<Document>
8282
@Resource(name = "DocumentNoteDAO")
8383
private DocumentNoteDAO noteDAO;
8484

85-
@Resource(name = "FolderDAO")
85+
@Resource(name = "folderDAO")
8686
private FolderDAO folderDAO;
8787

8888
@Resource(name = "UserDAO")

logicaldoc-core/src/main/java/com/logicaldoc/core/document/HibernateVersionDAO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class HibernateVersionDAO extends HibernatePersistentObjectDAO<Version> i
3636
@Resource(name = "Store")
3737
protected Store store;
3838

39-
@Resource(name = "FolderDAO")
39+
@Resource(name = "folderDAO")
4040
protected FolderDAO folderDAO;
4141

4242
public HibernateVersionDAO() {

logicaldoc-core/src/main/java/com/logicaldoc/core/document/TagsProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class TagsProcessor extends Task {
2121

2222
public static final String NAME = "TagsProcessor";
2323

24-
@Resource(name = "DocumentDAO")
24+
@Resource(name = "documentDAO")
2525
private DocumentDAO documentDao;
2626

2727

logicaldoc-core/src/main/java/com/logicaldoc/core/folder/PathCalculator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class PathCalculator extends Task {
2323

2424
public static final String NAME = "PathCalculator";
2525

26-
@Resource(name = "FolderDAO")
26+
@Resource(name = "folderDAO")
2727
protected FolderDAO folderDao;
2828

2929
private long processed = 0;

logicaldoc-core/src/main/java/com/logicaldoc/core/searchengine/IndexOptimizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class IndexOptimizer extends Task {
2525
@Resource(name = "SearchEngine")
2626
protected SearchEngine indexer;
2727

28-
@Resource(name = "DocumentDAO")
28+
@Resource(name = "documentDAO")
2929
protected DocumentDAO documentDao;
3030

3131
public IndexOptimizer() {

logicaldoc-core/src/main/java/com/logicaldoc/core/searchengine/IndexerTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class IndexerTask extends Task {
6262
@Resource(name = "documentManager")
6363
protected DocumentManager documentManager;
6464

65-
@Resource(name = "DocumentDAO")
65+
@Resource(name = "documentDAO")
6666
protected DocumentDAO documentDao;
6767

6868
@Resource(name = "TenantDAO")

0 commit comments

Comments
 (0)