1 package org.exoplatform.wiki.service.search.jcr;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.chromattic.api.NoSuchNodeException;
5 import org.chromattic.api.UndeclaredRepositoryException;
6 import org.exoplatform.container.ExoContainerContext;
7 import org.exoplatform.portal.config.model.PortalConfig;
8 import org.exoplatform.services.jcr.ext.hierarchy.NodeHierarchyCreator;
9 import org.exoplatform.services.log.ExoLogger;
10 import org.exoplatform.services.log.Log;
11 import org.exoplatform.wiki.mow.core.api.wiki.WikiNodeType;
12 import org.exoplatform.wiki.service.search.SearchData;
13 import org.exoplatform.wiki.utils.JCRUtils;
14
15 import javax.jcr.Node;
16 import javax.jcr.PathNotFoundException;
17 import java.util.ArrayList;
18 import java.util.List;
19
20
21
22
23 public class JCRTemplateSearchQueryBuilder {
24
25 private Log log = ExoLogger.getLogger(JCRTemplateSearchQueryBuilder.class);
26
27 public static final String ALL_PATH = "%/";
28
29 protected static String PORTAL_PATH = "/exo:applications/"
30 + WikiNodeType.Definition.WIKI_APPLICATION + "/"
31 + WikiNodeType.Definition.WIKIS + "/%/";
32
33 protected static String GROUP_PATH = "/Groups/%/ApplicationData/"
34 + WikiNodeType.Definition.WIKI_APPLICATION + "/";
35
36 protected String USER_PATH = "/Users/%/ApplicationData/" + WikiNodeType.Definition.WIKI_APPLICATION + "/";
37
38 public static String TEMPLATE_PATH = WikiNodeType.Definition.PREFERENCES + "/"
39 + WikiNodeType.Definition.TEMPLATE_CONTAINER + "/%";
40
41 public static String ALL_TEMPLATESPATH = ALL_PATH + TEMPLATE_PATH;
42
43 public static String PORTAL_TEMPLATESPATH = PORTAL_PATH + TEMPLATE_PATH;
44
45 public static String GROUP_TEMPLATESPATH = GROUP_PATH + TEMPLATE_PATH;
46
47 private SearchData searchData;
48
49 private String nodeType = null;
50
51 protected List<String> propertyConstraints = new ArrayList<>();
52
53 public JCRTemplateSearchQueryBuilder(SearchData searchData) {
54 this.searchData = searchData;
55
56 if (PortalConfig.USER_TYPE.equals(searchData.getWikiType())) {
57 NodeHierarchyCreator nodeHierachyCreator = ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(NodeHierarchyCreator.class);
58 try {
59 if (searchData.getWikiOwner() != null && searchData.getWikiOwner().length() > 0) {
60 Node userNode = nodeHierachyCreator.getUserApplicationNode(JCRUtils.createSystemProvider(), searchData.getWikiOwner());
61 USER_PATH = userNode.getPath() + "/" + WikiNodeType.Definition.WIKI_APPLICATION + "/";
62 }
63 } catch (Exception e) {
64 if (e instanceof PathNotFoundException) {
65 throw new NoSuchNodeException(e);
66 } else {
67 throw new UndeclaredRepositoryException(e.getMessage());
68 }
69 }
70 }
71 this.propertyConstraints = new ArrayList<>();
72 }
73
74 public void setNodeType(String nodeType) {
75 this.nodeType = nodeType;
76 }
77
78 public String getNodeType() {
79 return this.nodeType;
80 }
81
82 public List<String> getPropertyConstraints() {
83 return new ArrayList<String>(this.propertyConstraints);
84 }
85
86 public void addPropertyConstraints(List<String> value) {
87 if (value != null) {
88 propertyConstraints.addAll(value);
89 }
90 }
91
92 public void addPropertyConstraint(String value) {
93 if (StringUtils.isNotBlank(value)) {
94 propertyConstraints.add(value);
95 }
96 }
97
98 public String createJcrQueryPath() {
99 String jcrQueryPath = StringUtils.EMPTY;
100 if (searchData.getWikiType() == null && searchData.getWikiOwner() == null) {
101 jcrQueryPath = "jcr:path LIKE '" + ALL_TEMPLATESPATH + "'";
102 }
103 if (searchData.getWikiType() != null) {
104 if (searchData.getWikiType().equals(PortalConfig.USER_TYPE))
105 jcrQueryPath = "jcr:path LIKE '" + USER_PATH + TEMPLATE_PATH + "'";
106 else {
107 if (searchData.getWikiType().equals(PortalConfig.PORTAL_TYPE)) {
108 jcrQueryPath = "jcr:path LIKE '" + PORTAL_TEMPLATESPATH + "'";
109 } else if (searchData.getWikiType().equals(PortalConfig.GROUP_TYPE))
110 jcrQueryPath = "jcr:path LIKE '" + GROUP_TEMPLATESPATH + "'";
111
112 if (searchData.getWikiOwner() != null) {
113 jcrQueryPath = jcrQueryPath.replaceFirst("%", searchData.getWikiOwner());
114 }
115 }
116 }
117 return jcrQueryPath;
118 }
119
120 public String getStatementForSearchingTitle() {
121 StringBuilder statement = new StringBuilder();
122 try {
123 String title = searchData.getTitle();
124 statement.append("SELECT title, jcr:primaryType, path,"+WikiNodeType.Definition.DESCRIPTION)
125 .append(" FROM ")
126 .append(WikiNodeType.WIKI_PAGE)
127 .append(" WHERE ");
128 statement.append(createJcrQueryPath());
129 if (title != null && title.length() > 0) {
130 statement.append(" AND ")
131 .append(" CONTAINS(" + WikiNodeType.Definition.TITLE + ", '")
132 .append(title)
133 .append("') ");
134 }
135 } catch (Exception e) {
136 log.error("Failed to get statement ", e);
137 }
138 return statement.toString();
139 }
140 }