001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/search/tags/sakai_2-4-1/search-impl/impl/src/java/org/sakaiproject/search/component/adapter/rwiki/RWikiEntityContentProducer.java $
003: * $Id: RWikiEntityContentProducer.java 29179 2007-04-19 02:25:25Z ajpoland@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.search.component.adapter.rwiki;
021:
022: import java.io.Reader;
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.sakaiproject.component.api.ComponentManager;
031: import org.sakaiproject.component.cover.ServerConfigurationService;
032: import org.sakaiproject.entity.api.Entity;
033: import org.sakaiproject.entity.api.EntityManager;
034: import org.sakaiproject.entity.api.EntityProducer;
035: import org.sakaiproject.entity.api.Reference;
036: import org.sakaiproject.event.api.Event;
037: import org.sakaiproject.search.api.EntityContentProducer;
038: import org.sakaiproject.search.api.SearchIndexBuilder;
039: import org.sakaiproject.search.api.SearchService;
040: import org.sakaiproject.search.api.SearchUtils;
041: import org.sakaiproject.search.component.adapter.contenthosting.HTMLParser;
042: import org.sakaiproject.search.model.SearchBuilderItem;
043:
044: import uk.ac.cam.caret.sakai.rwiki.service.api.RWikiObjectService;
045: import uk.ac.cam.caret.sakai.rwiki.service.api.RenderService;
046: import uk.ac.cam.caret.sakai.rwiki.service.api.model.RWikiEntity;
047: import uk.ac.cam.caret.sakai.rwiki.service.api.model.RWikiObject;
048: import uk.ac.cam.caret.sakai.rwiki.utils.NameHelper;
049:
050: public class RWikiEntityContentProducer implements
051: EntityContentProducer {
052:
053: private static Log log = LogFactory
054: .getLog(RWikiEntityContentProducer.class);
055:
056: private RenderService renderService = null;
057:
058: private RWikiObjectService objectService = null;
059:
060: private SearchService searchService = null;
061:
062: private SearchIndexBuilder searchIndexBuilder = null;
063:
064: private EntityManager entityManager = null;
065:
066: public void init() {
067: try {
068: ComponentManager cm = org.sakaiproject.component.cover.ComponentManager
069: .getInstance();
070: renderService = (RenderService) load(cm,
071: RenderService.class.getName());
072: objectService = (RWikiObjectService) load(cm,
073: RWikiObjectService.class.getName());
074: searchService = (SearchService) load(cm,
075: SearchService.class.getName());
076: searchIndexBuilder = (SearchIndexBuilder) load(cm,
077: SearchIndexBuilder.class.getName());
078: entityManager = (EntityManager) load(cm,
079: EntityManager.class.getName());
080:
081: if ("true".equals(ServerConfigurationService.getString(
082: "search.enable", "false"))) {
083:
084: searchService
085: .registerFunction(RWikiObjectService.EVENT_RESOURCE_ADD);
086: searchService
087: .registerFunction(RWikiObjectService.EVENT_RESOURCE_WRITE);
088: searchIndexBuilder.registerEntityContentProducer(this );
089: }
090: } catch (Throwable t) {
091: log.error("Failed to init ", t);
092: }
093:
094: }
095:
096: private Object load(ComponentManager cm, String name) {
097: Object o = cm.get(name);
098: if (o == null) {
099: log.error("Cant find Spring component named " + name);
100: }
101: return o;
102: }
103:
104: public boolean isContentFromReader(String cr) {
105: return false;
106: }
107:
108: public Reader getContentReader(String reference) {
109: return null;
110: }
111:
112: public String getContent(String reference) {
113: Reference ref = getReference(reference);
114: Entity cr = ref.getEntity();
115: RWikiEntity rwe = (RWikiEntity) cr;
116: RWikiObject rwo = rwe.getRWikiObject();
117: String pageName = rwo.getName();
118: String pageSpace = NameHelper.localizeSpace(pageName, rwo
119: .getRealm());
120: String renderedPage = renderService.renderPage(rwo, pageSpace,
121: objectService.getComponentPageLinkRender(pageSpace));
122: StringBuilder sb = new StringBuilder();
123: for (HTMLParser hp = new HTMLParser(renderedPage); hp.hasNext();) {
124: SearchUtils.appendCleanString(hp.next(), sb);
125: }
126:
127: String r = sb.toString();
128: if (log.isDebugEnabled()) {
129: log.debug("Wiki.getContent:" + reference + ":" + r);
130: }
131: return r;
132: }
133:
134: public String getTitle(String reference) {
135: Reference ref = getReference(reference);
136: Entity cr = ref.getEntity();
137: RWikiEntity rwe = (RWikiEntity) cr;
138: RWikiObject rwo = rwe.getRWikiObject();
139: String r = SearchUtils.appendCleanString(rwo.getName(), null)
140: .toString();
141: if (log.isDebugEnabled()) {
142: log.debug("Wiki.getTitle:" + reference + ":" + r);
143: }
144: return r;
145: }
146:
147: public boolean matches(String reference) {
148: try {
149: Reference ref = getReference(reference);
150: EntityProducer ep = ref.getEntityProducer();
151: return (ep instanceof RWikiObjectService);
152: } catch (Exception ex) {
153: return false;
154: }
155:
156: }
157:
158: public List getAllContent() {
159: List allPages = objectService.findAllPageNames();
160: List l = new ArrayList();
161: for (Iterator i = allPages.iterator(); i.hasNext();) {
162: String pageName = (String) i.next();
163: String reference = objectService.createReference(pageName);
164: l.add(reference);
165: }
166: return l;
167: }
168:
169: public Integer getAction(Event event) {
170: String eventName = event.getEvent();
171: if (RWikiObjectService.EVENT_RESOURCE_ADD.equals(eventName)
172: || RWikiObjectService.EVENT_RESOURCE_WRITE
173: .equals(eventName)) {
174: return SearchBuilderItem.ACTION_ADD;
175: }
176: if (RWikiObjectService.EVENT_RESOURCE_REMOVE.equals(eventName)) {
177: return SearchBuilderItem.ACTION_DELETE;
178: }
179: return SearchBuilderItem.ACTION_UNKNOWN;
180: }
181:
182: public boolean matches(Event event) {
183: return !SearchBuilderItem.ACTION_UNKNOWN
184: .equals(getAction(event));
185: }
186:
187: public String getTool() {
188: return "wiki";
189: }
190:
191: public String getUrl(String reference) {
192: Reference ref = getReference(reference);
193: return ref.getUrl() + "html";
194: }
195:
196: private String getSiteId(Reference ref) {
197: String context = ref.getContext();
198: if (context.startsWith("/site/")) {
199: context = context.substring("/site/".length());
200: }
201: if (context.startsWith("/")) {
202: context = context.substring(1);
203: }
204: int slash = context.indexOf("/");
205: if (slash > 0) {
206: context = context.substring(0, slash);
207: }
208: if (log.isDebugEnabled()) {
209: log.debug("Wiki.getSiteId" + ref + ":" + context);
210: }
211: return context;
212: }
213:
214: public String getSiteId(String resourceName) {
215:
216: String r = getSiteId(entityManager.newReference(resourceName));
217: if (log.isDebugEnabled()) {
218: log.debug("Wiki.getSiteId" + resourceName + ":" + r);
219: }
220: return r;
221: }
222:
223: public List getSiteContent(String context) {
224: List allPages = objectService.findRWikiSubPages("/site/"
225: + context);
226: List l = new ArrayList();
227: for (Iterator i = allPages.iterator(); i.hasNext();) {
228: RWikiObject page = (RWikiObject) i.next();
229: String reference = objectService.createReference(page
230: .getName());
231: l.add(reference);
232: }
233: return l;
234: }
235:
236: public Iterator getSiteContentIterator(String context) {
237: List allPages = objectService.findRWikiSubPages("/site/"
238: + context);
239: List l = new ArrayList();
240: final Iterator allPagesIterator = allPages.iterator();
241: return new Iterator() {
242:
243: public boolean hasNext() {
244: return allPagesIterator.hasNext();
245: }
246:
247: public Object next() {
248: RWikiObject page = (RWikiObject) allPagesIterator
249: .next();
250: return objectService.createReference(page.getName());
251: }
252:
253: public void remove() {
254: throw new UnsupportedOperationException(
255: "Remove not supported");
256: }
257:
258: };
259: }
260:
261: public boolean isForIndex(String reference) {
262:
263: try {
264: Reference ref = getReference(reference);
265: RWikiEntity rwe = (RWikiEntity) ref.getEntity();
266: RWikiObject rwo = rwe.getRWikiObject();
267: String pageName = rwo.getName();
268: String pageSpace = NameHelper.localizeSpace(pageName, rwo
269: .getRealm());
270: if (objectService.exists(pageName, pageSpace)) {
271: return true;
272: }
273: } catch (Exception ex) {
274: }
275: return false;
276: }
277:
278: public boolean canRead(String reference) {
279: try {
280: Reference ref = getReference(reference);
281: RWikiEntity rwe = (RWikiEntity) ref.getEntity();
282: RWikiObject rwo = rwe.getRWikiObject();
283: return objectService.checkRead(rwo);
284: } catch (Exception ex) {
285: }
286: return false;
287: }
288:
289: public Map getCustomProperties() {
290: return null;
291: }
292:
293: public String getCustomRDF() {
294: return null;
295: }
296:
297: private Reference getReference(String reference) {
298: try {
299: Reference r = entityManager.newReference(reference);
300: if (log.isDebugEnabled()) {
301: log.debug("Wiki.getReference:" + reference + ":" + r);
302: }
303: return r;
304: } catch (Exception ex) {
305: }
306: return null;
307: }
308:
309: private EntityProducer getProducer(Reference ref) {
310: try {
311: return ref.getEntityProducer();
312: } catch (Exception ex) {
313: }
314: return null;
315: }
316:
317: /*
318: * (non-Javadoc)
319: *
320: * @see org.sakaiproject.search.api.EntityContentProducer#getId(java.lang.String)
321: */
322: public String getId(String reference) {
323: try {
324: String r = getReference(reference).getId();
325: if (log.isDebugEnabled()) {
326: log.debug("Wiki.getId:" + reference + ":" + r);
327: }
328: return r;
329: } catch (Exception ex) {
330: return "";
331: }
332: }
333:
334: /*
335: * (non-Javadoc)
336: *
337: * @see org.sakaiproject.search.api.EntityContentProducer#getSubType(java.lang.String)
338: */
339: public String getSubType(String reference) {
340: try {
341: String r = getReference(reference).getSubType();
342: if (log.isDebugEnabled()) {
343: log.debug("Wiki.getSubType:" + reference + ":" + r);
344: }
345: return r;
346: } catch (Exception ex) {
347: return "";
348: }
349: }
350:
351: /*
352: * (non-Javadoc)
353: *
354: * @see org.sakaiproject.search.api.EntityContentProducer#getType(java.lang.String)
355: */
356: public String getType(String reference) {
357: try {
358: String r = getReference(reference).getType();
359: if (log.isDebugEnabled()) {
360: log.debug("Wiki.getType:" + reference + ":" + r);
361: }
362: return r;
363: } catch (Exception ex) {
364: return "";
365: }
366: }
367:
368: /*
369: * (non-Javadoc)
370: *
371: * @see org.sakaiproject.search.api.EntityContentProducer#getType(java.lang.String)
372: */
373: public String getContainer(String reference) {
374: try {
375: String r = getReference(reference).getContainer();
376: if (log.isDebugEnabled()) {
377: log.debug("Wiki.getContainer:" + reference + ":" + r);
378: }
379: return r;
380: } catch (Exception ex) {
381: return "";
382: }
383: }
384:
385: }
|