KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > controllers > kernel > impl > simple > SearchController


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.controllers.kernel.impl.simple;
25
26 import java.io.StringReader JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.apache.log4j.Logger;
32 import org.apache.xerces.parsers.DOMParser;
33 import org.exolab.castor.jdo.Database;
34 import org.exolab.castor.jdo.OQLQuery;
35 import org.exolab.castor.jdo.QueryResults;
36 import org.infoglue.cms.entities.content.ContentVersion;
37 import org.infoglue.cms.entities.content.ContentVersionVO;
38 import org.infoglue.cms.entities.kernel.BaseEntityVO;
39 import org.infoglue.cms.exception.Bug;
40 import org.infoglue.cms.exception.SystemException;
41 import org.infoglue.cms.security.InfoGluePrincipal;
42 import org.infoglue.cms.util.ConstraintExceptionBuffer;
43 import org.w3c.dom.Document JavaDoc;
44 import org.w3c.dom.Node JavaDoc;
45 import org.w3c.dom.NodeList JavaDoc;
46 import org.xml.sax.InputSource JavaDoc;
47
48 public class SearchController extends BaseController
49 {
50     private final static Logger logger = Logger.getLogger(SearchController.class.getName());
51
52     public static String JavaDoc getAttributeValue(String JavaDoc xml,String JavaDoc key)
53     {
54         String JavaDoc value = "";
55             try
56             {
57                 InputSource JavaDoc inputSource = new InputSource JavaDoc(new StringReader JavaDoc(xml));
58                 DOMParser parser = new DOMParser();
59                 parser.parse(inputSource);
60                 Document JavaDoc document = parser.getDocument();
61                 NodeList JavaDoc nl = document.getDocumentElement().getChildNodes();
62                 Node JavaDoc n = nl.item(0);
63                 nl = n.getChildNodes();
64
65                 for(int i=0; i<nl.getLength(); i++)
66                 {
67                     n = nl.item(i);
68                     if(n.getNodeName().equalsIgnoreCase(key))
69                     {
70                         value = n.getFirstChild().getNodeValue();
71                         break;
72                     }
73                 }
74             }
75             catch(Exception JavaDoc e)
76             {
77                 e.printStackTrace();
78             }
79         if(value.equalsIgnoreCase(""))value="This Content is Unititled";
80         return value;
81     }
82
83     public static String JavaDoc setScoreImg(double score)
84     {
85         if( 2.0 < score){
86             return "5star.gif";
87         }
88         else if( 1.0 < score){
89             return "4star.gif";
90         }
91         else if( 0.6 < score){
92             return "3star.gif";
93         }
94         else if( 0.4 < score){
95             return "2star.gif";
96         }
97         else{
98             return "1star.gif";
99         }
100     }
101
102     
103     public static List JavaDoc getContentVersions(Integer JavaDoc repositoryId, String JavaDoc searchString, int maxRows, String JavaDoc name, Integer JavaDoc languageId, Integer JavaDoc contentTypeDefinitionId, Integer JavaDoc caseSensitive, Integer JavaDoc stateId) throws SystemException, Bug
104     {
105         List JavaDoc matchingContents = new ArrayList JavaDoc();
106
107         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
108         Database db = CastorDatabaseService.getDatabase();
109         try
110         {
111             beginTransaction(db);
112             /*
113             OQLQuery oql = db.getOQLQuery("SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.isActive = $1 AND cv.versionValue LIKE $2 AND cv.owningContent.repository.repositoryId = $3 ORDER BY cv.owningContent asc, cv.language, cv.contentVersionId desc");
114             oql.bind(new Boolean(true));
115             oql.bind("%" + searchString + "%");
116             oql.bind(repositoryId);
117             */

118             
119             String JavaDoc extraArguments = "";
120             String JavaDoc inverse = "";
121             
122             int index = 4;
123             List JavaDoc arguments = new ArrayList JavaDoc();
124             
125             if(name != null && !name.equalsIgnoreCase(""))
126             {
127                 extraArguments += " AND cv.versionModifier = $" + index;
128                 arguments.add(name);
129                 index++;
130             }
131             if(languageId != null)
132             {
133                 extraArguments += " AND cv.language = $" + index;
134                 arguments.add(languageId);
135                 index++;
136             }
137             if(contentTypeDefinitionId != null)
138             {
139                 extraArguments += " AND cv.owningContent.contentTypeDefinition = $" + index;
140                 arguments.add(contentTypeDefinitionId);
141                 index++;
142             }
143             if(stateId != null)
144             {
145                 extraArguments += " AND cv.stateId = $" + index;
146                 arguments.add(stateId);
147                 index++;
148             }
149                 
150             String JavaDoc sql = "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.isActive = $1 AND cv.versionValue LIKE $2 AND cv.owningContent.repository.repositoryId = $3 " + extraArguments + " ORDER BY cv.owningContent asc, cv.language, cv.contentVersionId desc";
151             logger.info("sql:" + sql);
152             OQLQuery oql = db.getOQLQuery(sql);
153             oql.bind(new Boolean JavaDoc(true));
154             oql.bind("%" + searchString + "%");
155             oql.bind(repositoryId);
156             
157             Iterator JavaDoc iterator = arguments.iterator();
158             while(iterator.hasNext())
159             {
160                 oql.bind(iterator.next());
161             }
162             
163             QueryResults results = oql.execute(Database.ReadOnly);
164             
165             Integer JavaDoc previousContentId = new Integer JavaDoc(-1);
166             Integer JavaDoc previousLanguageId = new Integer JavaDoc(-1);
167             int currentCount = 0;
168             while(results.hasMore() && currentCount < maxRows)
169             {
170                 ContentVersion contentVersion = (ContentVersion)results.next();
171                 logger.info("Found a version matching " + searchString + ":" + contentVersion.getId() + "=" + contentVersion.getOwningContent().getName());
172                 if(contentVersion.getOwningContent().getId().intValue() != previousContentId.intValue() || contentVersion.getLanguage().getId().intValue() != previousLanguageId.intValue())
173                 {
174                     ContentVersion latestContentVersion = ContentVersionController.getContentVersionController().getLatestActiveContentVersion(contentVersion.getOwningContent().getId(), contentVersion.getLanguage().getId(), db);
175                     if(latestContentVersion.getId().intValue() == contentVersion.getId().intValue() && (caseSensitive == null || contentVersion.getVersionValue().indexOf(searchString) > -1))
176                     {
177                         matchingContents.add(contentVersion.getValueObject());
178                         previousContentId = contentVersion.getOwningContent().getId();
179                         previousLanguageId = contentVersion.getLanguage().getId();
180                         currentCount++;
181                     }
182                 }
183             }
184
185             results.close();
186             oql.close();
187
188             commitTransaction(db);
189         }
190         catch ( Exception JavaDoc e )
191         {
192             rollbackTransaction(db);
193             throw new SystemException("An error occurred when we tried to fetch a list of users in this role. Reason:" + e.getMessage(), e);
194         }
195         
196         return matchingContents;
197         
198     }
199     
200     public static int replaceString(String JavaDoc searchString, String JavaDoc replaceString, String JavaDoc[] contentVersionIds, InfoGluePrincipal infoGluePrincipal)throws SystemException, Bug
201     {
202         int replacements = 0;
203         
204         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
205         Database db = CastorDatabaseService.getDatabase();
206         try
207         {
208             beginTransaction(db);
209
210             for(int i=0; i<contentVersionIds.length; i++)
211             {
212                 String JavaDoc contentVersionId = contentVersionIds[i];
213                 logger.info("contentVersionId:" + contentVersionId);
214                 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(new Integer JavaDoc(contentVersionIds[i]), db);
215                 if(contentVersion.getStateId().intValue() != ContentVersionVO.WORKING_STATE.intValue())
216                 {
217                     List JavaDoc events = new ArrayList JavaDoc();
218                     contentVersion = ContentStateController.changeState(contentVersion.getId(), ContentVersionVO.WORKING_STATE, "Automatic by the replace function", true, infoGluePrincipal, null, db, events);
219                     logger.info("Setting the version to working before replacing string...");
220                 }
221                 
222                 String JavaDoc value = contentVersion.getVersionValue();
223                 value = value.replaceAll(searchString, replaceString);
224                 
225                 contentVersion.setVersionValue(value);
226
227                 replacements++;
228             }
229             
230             commitTransaction(db);
231         }
232         catch ( Exception JavaDoc e )
233         {
234             rollbackTransaction(db);
235             throw new SystemException("An error occurred when we tried to fetch a list of users in this role. Reason:" + e.getMessage(), e);
236         }
237         
238         return replacements;
239         
240     }
241     
242     /**
243      * This is a method that never should be called.
244      */

245
246     public BaseEntityVO getNewVO()
247     {
248         return null;
249     }
250     
251 }
Popular Tags