1 package com.dotmarketing.portlets.contentlet.ajax; 2 3 import java.io.BufferedInputStream ; 4 import java.io.File ; 5 import java.io.FileInputStream ; 6 import java.util.ArrayList ; 7 import java.util.Enumeration ; 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 import java.util.Map ; 12 import java.util.Properties ; 13 import java.util.TreeSet ; 14 15 import javax.servlet.http.HttpServletRequest ; 16 import javax.servlet.http.HttpSession ; 17 18 import org.apache.commons.logging.LogFactory; 19 import org.apache.lucene.document.Document; 20 import org.apache.lucene.queryParser.ParseException; 21 import org.apache.lucene.queryParser.QueryParser; 22 23 import uk.ltd.getahead.dwr.WebContextFactory; 24 25 import com.dotmarketing.cache.FieldsCache; 26 import com.dotmarketing.cms.factories.PublicUserFactory; 27 import com.dotmarketing.factories.IdentifierFactory; 28 import com.dotmarketing.factories.InodeFactory; 29 import com.dotmarketing.lucene.DotAnalyzer; 30 import com.dotmarketing.portlets.contentlet.factories.ContentletFactory; 31 import com.dotmarketing.portlets.contentlet.factories.ReindexationProcessStatus; 32 import com.dotmarketing.portlets.contentlet.model.Contentlet; 33 import com.dotmarketing.portlets.structure.factories.StructureFactory; 34 import com.dotmarketing.portlets.structure.model.Field; 35 import com.dotmarketing.portlets.structure.model.Structure; 36 import com.dotmarketing.util.Config; 37 import com.dotmarketing.util.Logger; 38 import com.dotmarketing.util.LuceneHits; 39 import com.dotmarketing.util.LuceneUtils; 40 import com.dotmarketing.util.UtilMethods; 41 import com.dotmarketing.util.WebKeys; 42 import com.liferay.portal.model.Role; 43 import com.liferay.portal.model.User; 44 45 48 public class ContentletAjax { 49 50 private java.text.DateFormat modDateFormat = java.text.DateFormat.getDateTimeInstance(java.text.DateFormat.SHORT, 51 java.text.DateFormat.SHORT); 52 53 public Map <String , String > getContentletData(String inode) { 54 Contentlet cont = (Contentlet) InodeFactory.getInode(inode, Contentlet.class); 55 Structure targetStructure = cont.getStructure(); 56 List <Field> targetFields = FieldsCache.getFieldsByStructureInode(targetStructure.getInode()); 57 Map <String , String > map = new HashMap <String , String >(); 58 for (Field f : targetFields) { 59 if (f.isIndexed() || f.isListed()) { 60 String fieldName = f.getFieldName(); 61 Object fieldValueObj = com.dotmarketing.portlets.contentlet.factories.ContentletFactory.getFieldValue( 62 cont, f.getFieldName()); 63 String fieldValue = ""; 64 if (fieldValueObj instanceof java.util.Date ) { 65 if (fieldValueObj != null) 66 fieldValue = modDateFormat.format(fieldValueObj); 67 } else if (fieldValueObj instanceof java.sql.Timestamp ) { 68 if (fieldValueObj != null) { 69 java.util.Date fieldDate = new java.util.Date (((java.sql.Timestamp ) fieldValueObj).getTime()); 70 fieldValue = modDateFormat.format(fieldDate); 71 } 72 } else { 73 if (fieldValueObj != null) 74 fieldValue = fieldValueObj.toString(); 75 } 76 map.put(fieldName, fieldValue); 77 } 78 } 79 map.put("identifier", String.valueOf(IdentifierFactory.getIdentifierByInode(cont).getInode())); 80 map.put("inode", String.valueOf(cont.getInode())); 81 return map; 82 } 83 84 106 public List searchContentlets(String structureInode, List <String > fields, List <String > categories, 107 boolean showDeleted, int page, String orderBy) { 108 109 HttpSession sess = WebContextFactory.get().getSession(); 110 HttpServletRequest req = WebContextFactory.get().getHttpServletRequest(); 111 112 User currentUser = null; 114 try { 115 currentUser = com.liferay.portal.util.PortalUtil.getUser(req); 116 } catch (Exception e) { 117 Logger.error(this, "Error trying to obtain the current liferay user from the request.", e); 118 } 119 boolean isAdmin = false; 120 List <Role> roles = new ArrayList <Role>(); 121 if (!PublicUserFactory.isACMSAdmin(currentUser)) { 122 roles = com.dotmarketing.factories.RoleFactory.getAllRolesForUser(currentUser.getUserId()); 123 } else 124 isAdmin = true; 125 126 StringBuffer luceneQuery = new StringBuffer (); 128 129 Map lastSearchMap = new HashMap (); 130 sess.setAttribute(WebKeys.CONTENTLET_LAST_SEARCH, lastSearchMap); 131 132 Structure st = StructureFactory.getStructureByInode(structureInode); 133 lastSearchMap.put("structure", st); 134 135 Map <String , String > fieldsSearch = new HashMap <String , String >(); 136 137 QueryParser parser = new QueryParser ("", new DotAnalyzer()); 138 139 for (int i = 0; i < fields.size(); i = i + 2) { 140 String fieldName = (String ) fields.get(i); 141 String fieldValue = (String ) fields.get(i + 1); 142 if (UtilMethods.isSet(fieldValue)) { 143 fieldsSearch.put(fieldName, fieldValue); 144 145 try { 146 luceneQuery.append("+" + fieldName + ":" + parser.parse(fieldValue).toString() + "* "); 147 } catch (ParseException e) { 148 luceneQuery.append("+" + fieldName + ":" + fieldValue + "* "); 149 } 150 } 151 } 152 lastSearchMap.put("fieldsSearch", fieldsSearch); 153 154 for (String cat : categories) { 155 luceneQuery.append("+c" + cat + "c:on "); 156 } 157 lastSearchMap.put("categories", categories); 158 159 if (!UtilMethods.isSet(orderBy)) 160 orderBy = "modDate desc"; 161 162 lastSearchMap.put("showDeleted", showDeleted); 163 164 lastSearchMap.put("page", page); 165 int perPage = Config.getIntProperty("PER_PAGE"); 166 int offset = perPage * (page - 1); 167 168 lastSearchMap.put("orderBy", orderBy); 169 170 if (!isAdmin) { 172 luceneQuery.append("+("); 173 for (Role role : roles) { 174 luceneQuery.append("permissions:P" + role.getRoleId() + ".1P* "); 175 } 176 luceneQuery.append(")"); 177 } 178 179 luceneQuery.append(" +working:true"); 180 181 long before = System.currentTimeMillis(); 183 LuceneHits hits = ContentletFactory.indexSearch(st, luceneQuery.toString(), showDeleted, 184 offset, perPage, orderBy); 185 long after = System.currentTimeMillis(); 186 Logger.debug(ContentletAjax.class, "searchContentlets: Time to search on lucene =" + (after - before) + " ms."); 187 188 189 before = System.currentTimeMillis(); 190 191 List results = new ArrayList (); 193 194 Map counters = new HashMap (); 196 results.add(counters); 197 198 List headers = new ArrayList (); 200 Map <String , Field> fieldsMapping = new HashMap <String , Field>(); 201 List <Field> stFields = FieldsCache.getFieldsByStructureInode(st.getInode()); 202 for (Field f : stFields) { 203 if (f.isListed()) { 204 fieldsMapping.put(f.getFieldContentlet(), f); 205 headers.add(f.getMap()); 206 } 207 } 208 if (headers.size() == 0) { 209 headers.add("Identifier"); 210 } 211 results.add(headers); 212 213 for (int i = 0; i < hits.length(); i++) { 215 Document doc = hits.doc(i); 216 217 Map <String , String > searchResult = new HashMap <String , String >(); 218 219 for (String fieldContentlet : fieldsMapping.keySet()) { 220 String fieldValue = doc.get(fieldContentlet); 221 Field field = (Field) fieldsMapping.get(fieldContentlet); 222 if (field.getFieldType().equals(WebKeys.TypeField.DATE)) { 223 if (UtilMethods.isSet(fieldValue)) 224 fieldValue = UtilMethods.dateToHTMLDate(LuceneUtils.fromLuceneDate(fieldValue)); 225 } else if (field.getFieldType().equals(WebKeys.TypeField.DATETIME)) { 226 if (UtilMethods.isSet(fieldValue)) 227 fieldValue = UtilMethods.dateToHTMLDate(LuceneUtils.fromLuceneDate(fieldValue)) 228 + " " 229 + UtilMethods.dateToHTMLTime(LuceneUtils.fromLuceneDate(fieldValue)); 230 }else if (field.getFieldType().equals(WebKeys.TypeField.CHECKBOX) || field.getFieldType().equals(WebKeys.TypeField.MULTISELECT)) { 231 if (UtilMethods.isSet(fieldValue)) 232 fieldValue = fieldValue.replaceAll("# #",",").replaceAll("#",""); 233 } 234 searchResult.put(fieldContentlet, fieldValue); 235 } 236 searchResult.put("inode", doc.get("inode")); 237 searchResult.put("Identifier", doc.get("identifier")); 238 searchResult.put("identifier", doc.get("identifier")); 239 String fieldValue = UtilMethods.dateToHTMLDate(LuceneUtils.fromLuceneDate(doc.get("modDate"))) + " " 240 + UtilMethods.dateToHTMLTime(LuceneUtils.fromLuceneDate(doc.get("modDate"))); 241 searchResult.put("modDate", fieldValue); 242 String user = ""; 243 User contentEditor = PublicUserFactory.getUserByUserId(doc.get("modUser")); 244 if (contentEditor.getFirstName() == null || contentEditor.getLastName() == null) { 245 user = doc.get("modUser"); 246 } else { 247 user = contentEditor.getFullName(); 248 } 249 searchResult.put("modUser", user); 250 searchResult.put("working", doc.get("working")); 251 searchResult.put("live", doc.get("live")); 252 searchResult.put("deleted", doc.get("deleted")); 253 searchResult.put("locked", doc.get("locked")); 254 searchResult.put("structureInode", doc.get("structureInode")); 255 searchResult.put("languageId", doc.get("languageId")); 256 searchResult.put("permissions", doc.get("permissions")); 257 258 results.add(searchResult); 259 } 260 261 counters.put("total", hits.getTotal()); 262 counters.put("hasPrevious", page != 1); 263 counters.put("hasNext", offset + perPage < hits.getTotal()); 264 counters.put("luceneQuery", hits.getLuceneQuery().replaceAll("working:true", "live:true")); 266 counters.put("sortByUF", orderBy); 267 268 after = System.currentTimeMillis(); 269 Logger.debug(ContentletAjax.class, "searchContentlets: Time to process results= " + (after - before) + " ms."); 270 271 return results; 272 } 273 274 public Map getReindexationProgress() { 275 return ReindexationProcessStatus.getProcessIndexationMap(); 276 } 277 278 public ArrayList <String []> doSearchGlossaryTerm(String valueToComplete, String language) throws Exception { 279 ArrayList <String []> list = new ArrayList <String []>(15); 280 281 Properties props = retrieveProperties(Long.parseLong(language)); 282 283 String [] term; 284 String key; 285 286 valueToComplete = valueToComplete.toLowerCase(); 287 288 ArrayList <String > keysList = new ArrayList <String >(300); 289 keysList.ensureCapacity(300); 290 291 for (Enumeration keys = props.keys(); keys.hasMoreElements();) 292 keysList.add((String ) keys.nextElement()); 293 294 TreeSet <String > set = new TreeSet <String >(keysList); 295 Iterator <String > iter = set.iterator(); 296 297 for (int i = 0; (i < 15) && iter.hasNext();) { 298 key = iter.next(); 299 if (key.toLowerCase().startsWith(valueToComplete)) { 300 term = new String [] {key, (70 < props.getProperty(key).length() ? props.getProperty(key).substring(0, 69) : props.getProperty(key))}; 301 list.add(term); 302 ++i; 303 } 304 } 305 306 return list; 307 } 308 309 private String getGlobalVariablesPath() { 310 String globalVarsPath = Config.getStringProperty("GLOBAL_VARIABLES_PATH"); 311 if (!UtilMethods.isSet(globalVarsPath)) { 312 globalVarsPath = Config.CONTEXT.getRealPath(File.separator + ".." + File.separator + "common" 313 + File.separator + "ext-ejb" + File.separator + "content" + File.separator); 314 } 315 if (!globalVarsPath.endsWith(File.separator)) 316 globalVarsPath = globalVarsPath + File.separator; 317 return globalVarsPath; 318 } 319 320 private Properties retrieveProperties(long langId) throws Exception { 321 Properties property = new Properties (); 322 323 try { 324 326 String filePath = getGlobalVariablesPath() + "cms_language_" + langId + ".properties"; 327 328 BufferedInputStream is = new BufferedInputStream (new FileInputStream (filePath)); 329 330 LogFactory.getLog(this.getClass()).debug("ClassLoader: " + is); 331 if (is != null) { 332 property = new Properties (); 333 property.load(is); 334 335 LogFactory.getLog(this.getClass()).debug("has: " + property.size() + " keys"); 336 } else { 337 property = new Properties (); 338 } 339 is.close(); 340 } catch (Exception e) { 341 Logger.error(this, "Could not load this file =" + "cms_language_" + langId + "_properties", e); 342 } 343 344 return property; 345 } 346 } | Popular Tags |