1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.io.StringReader ; 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.List ; 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 ; 44 import org.w3c.dom.Node ; 45 import org.w3c.dom.NodeList ; 46 import org.xml.sax.InputSource ; 47 48 public class SearchController extends BaseController 49 { 50 private final static Logger logger = Logger.getLogger(SearchController.class.getName()); 51 52 public static String getAttributeValue(String xml,String key) 53 { 54 String value = ""; 55 try 56 { 57 InputSource inputSource = new InputSource (new StringReader (xml)); 58 DOMParser parser = new DOMParser(); 59 parser.parse(inputSource); 60 Document document = parser.getDocument(); 61 NodeList nl = document.getDocumentElement().getChildNodes(); 62 Node 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 e) 76 { 77 e.printStackTrace(); 78 } 79 if(value.equalsIgnoreCase(""))value="This Content is Unititled"; 80 return value; 81 } 82 83 public static String 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 getContentVersions(Integer repositoryId, String searchString, int maxRows, String name, Integer languageId, Integer contentTypeDefinitionId, Integer caseSensitive, Integer stateId) throws SystemException, Bug 104 { 105 List matchingContents = new ArrayList (); 106 107 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 108 Database db = CastorDatabaseService.getDatabase(); 109 try 110 { 111 beginTransaction(db); 112 118 119 String extraArguments = ""; 120 String inverse = ""; 121 122 int index = 4; 123 List arguments = new ArrayList (); 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 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 (true)); 154 oql.bind("%" + searchString + "%"); 155 oql.bind(repositoryId); 156 157 Iterator 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 previousContentId = new Integer (-1); 166 Integer previousLanguageId = new Integer (-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 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 searchString, String replaceString, String [] 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 contentVersionId = contentVersionIds[i]; 213 logger.info("contentVersionId:" + contentVersionId); 214 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(new Integer (contentVersionIds[i]), db); 215 if(contentVersion.getStateId().intValue() != ContentVersionVO.WORKING_STATE.intValue()) 216 { 217 List events = new ArrayList (); 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 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 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 245 246 public BaseEntityVO getNewVO() 247 { 248 return null; 249 } 250 251 } | Popular Tags |