1 24 25 package com.opencms.defaults; 26 27 import org.opencms.file.CmsObject; 28 import org.opencms.main.CmsException; 29 import org.opencms.main.CmsLog; 30 31 import com.opencms.legacy.CmsLegacyException; 32 import com.opencms.template.A_CmsXmlContent; 33 import com.opencms.template.CmsCacheDirectives; 34 import com.opencms.template.CmsXmlTemplate; 35 import com.opencms.template.CmsXmlTemplateFile; 36 37 import java.lang.reflect.Constructor ; 38 import java.lang.reflect.InvocationTargetException ; 39 import java.lang.reflect.Method ; 40 import java.util.ArrayList ; 41 import java.util.Hashtable ; 42 import java.util.StringTokenizer ; 43 import java.util.Vector ; 44 45 80 public class CmsShowContent extends CmsXmlTemplate { 81 82 85 protected static final String C_CONTENTDEFINITION_CLASS_DATABLOCK = 86 "contentdefinition_class"; 87 88 91 protected static final String C_FILTERMETHOD_DATABLOCK = 92 "filtermethod"; 93 94 97 protected static final String C_FILTER_PARAMETERS_START = "filterparameter"; 98 99 103 protected static final String C_METHODS_TO_USE_DATABLOCK = 104 "methods"; 105 106 109 protected static final String C_LISTENTRY_DATABLOCK = "listentry"; 110 111 114 protected static final String C_MISSING_ID_PARAMETER = 115 "Missing id parameter: " 116 + "You have to provide a parameter id (as tagcontent or url parameter) to specify the " 117 + "dataentry you want to show"; 118 119 122 protected static final String C_CONSTRUCTOR_THROWED_EXCEPTION = 123 "Failed to create contentdefinition object: " 124 + "The constructor of the contentdefinition class throwed an exception"; 125 126 129 protected static final String C_CONSTRUCTOR_IS_NOT_SUBCLASS_OF_A_CMSCONTENTDEFINITION = 130 "Your contentdefinition class is not a subclass of the abstract A_CmsContentDefinition class"; 131 132 135 protected static final String C_NON_NUMERICAL_ID_PARAMETER = 136 "The parameter id is not a numerical value"; 137 138 141 protected static final String C_ERROR_TEXT = "ERROR"; 142 143 146 protected static final String C_ID_PARAMETER = "id"; 147 148 159 public Object getEntry(CmsObject cms, String tagcontent, 160 A_CmsXmlContent doc, Object userObject) throws CmsException { 161 162 CmsXmlTemplateFile template = (CmsXmlTemplateFile)doc; 163 164 String paramId = null; 165 if (tagcontent != null && !tagcontent.trim().equals("")) { 166 paramId = tagcontent; 168 } else { 169 paramId = (String )((Hashtable )userObject).get(C_ID_PARAMETER); 171 } 172 Integer id = null; 173 if (paramId != null) { 174 try { 176 id = new Integer (paramId); 177 } catch (NumberFormatException e) { 178 throw new CmsLegacyException(C_NON_NUMERICAL_ID_PARAMETER, CmsLegacyException.C_UNKNOWN_EXCEPTION); 180 } 181 } else { 182 throw new CmsLegacyException(C_MISSING_ID_PARAMETER); 184 } 185 String cdClassname = template.getDataValue(C_CONTENTDEFINITION_CLASS_DATABLOCK); 187 try { 188 ArrayList getMethods = null; 189 Class cdClass = Class.forName(cdClassname); 191 Constructor constructor = cdClass.getConstructor(new Class [] {CmsObject.class, Integer .class}); 193 A_CmsContentDefinition cdObject = (A_CmsContentDefinition)constructor.newInstance(new Object []{cms, id}); 195 Vector cdVec = new Vector (); 197 cdVec.add(cdObject); 198 registerVariantDeps(cms, doc.getAbsoluteFilename(), null, null, 199 (Hashtable )userObject, null, cdVec, null); 200 boolean showIt = true; 201 if (cdObject.isTimedContent()) { 202 I_CmsTimedContentDefinition curTimed = (I_CmsTimedContentDefinition)cdObject; 203 long currentTime = System.currentTimeMillis(); 204 if (((curTimed.getPublicationDate() != 0) && (currentTime < curTimed.getPublicationDate())) 205 || ((curTimed.getPurgeDate() != 0) && (currentTime > curTimed.getPurgeDate()))) { 206 showIt = false; 207 } 208 } 209 if (!showIt) { 210 throw new CmsLegacyException("requested content is not valid."); 212 } 213 if (template.hasData(C_METHODS_TO_USE_DATABLOCK)) { 214 String datablockContent = template.getDataValue(C_METHODS_TO_USE_DATABLOCK); 218 StringTokenizer tokenizer = new StringTokenizer (datablockContent, ","); 219 int tokens = tokenizer.countTokens(); 220 String [] names = new String [tokens]; 221 for (int i=0; i < tokens; i++) { 222 names[i] = tokenizer.nextToken().trim(); 223 } 224 getMethods = getGetMethodsByName(cdClass, names); 225 } else { 226 getMethods = getGetMethods(cdClass); 228 } 229 try { 230 Method getUniqueIdMethod = cdClass.getMethod("getUniqueId", new Class [] {CmsObject.class}); 231 template.setData("uniqueid", (String )getUniqueIdMethod.invoke(cdObject, new Object [] {cms})); 232 } catch (Exception e) { } 233 setDatablocks(template, cdObject, getMethods); 234 } catch (InvocationTargetException e) { 235 throw new CmsLegacyException(C_CONSTRUCTOR_THROWED_EXCEPTION, e.getTargetException()); 238 } catch (ClassCastException e) { 239 throw new CmsLegacyException(C_CONSTRUCTOR_IS_NOT_SUBCLASS_OF_A_CMSCONTENTDEFINITION, e); 241 } catch (Exception e) { 242 if (e instanceof CmsException) { 244 throw (CmsException)e; 245 } else { 246 throw new CmsLegacyException (e.getMessage(), e); 248 } 249 } 250 return ""; 251 } 252 253 265 public Object getList(CmsObject cms, String tagcontent, 266 A_CmsXmlContent doc, Object userObject) throws CmsException { 267 StringBuffer list = new StringBuffer (); 268 ArrayList getMethods = null; 269 Hashtable parameters = (Hashtable ) userObject; 270 CmsXmlTemplateFile template = (CmsXmlTemplateFile) doc; 271 String contentDefinitionName = template.getDataValue(C_CONTENTDEFINITION_CLASS_DATABLOCK); 272 String filterMethodName = template.getDataValue(C_FILTERMETHOD_DATABLOCK); 273 274 try { 275 Class cdClass = Class.forName(contentDefinitionName); 277 Vector theClass = new Vector (); 279 theClass.add(cdClass); 280 Vector allCdClasses = new Vector (); 281 String userParameter = getUserParameter(parameters, tagcontent); 283 Vector cdObjects = invokeFilterMethod(cms, cdClass, filterMethodName, userParameter); 284 if (template.hasData(C_METHODS_TO_USE_DATABLOCK)) { 285 String datablockContent = template.getDataValue(C_METHODS_TO_USE_DATABLOCK); 289 StringTokenizer tokenizer = new StringTokenizer (datablockContent, ","); 290 int tokens = tokenizer.countTokens(); 291 String [] names = new String [tokens]; 292 for (int i=0; i < tokens; i++) { 293 names[i] = tokenizer.nextToken().trim(); 294 } 295 getMethods = getGetMethodsByName(cdClass, names); 296 } else { 297 getMethods = getGetMethods(cdClass); 299 } 300 int size = cdObjects.size(); 302 long currentTime = System.currentTimeMillis(); 303 for (int i=0; i < size; i++) { 304 boolean showIt = true; 305 A_CmsContentDefinition curCont = (A_CmsContentDefinition)cdObjects.elementAt(i); 306 if (curCont.isTimedContent()) { 307 allCdClasses.add(curCont); 308 I_CmsTimedContentDefinition curTimed = (I_CmsTimedContentDefinition)curCont; 309 if (((curTimed.getPublicationDate() != 0) && (currentTime < curTimed.getPublicationDate())) 310 || ((curTimed.getPurgeDate() != 0) && (currentTime > curTimed.getPurgeDate()))) { 311 showIt = false; 312 } 313 } 314 if (showIt) { 315 try { 316 Method getUniqueIdMethod = cdClass.getMethod("getUniqueId", new Class [] {CmsObject.class}); 317 template.setData("uniqueid", (String )getUniqueIdMethod.invoke(curCont, new Object [] {cms})); 318 } catch (Exception e) { 319 } 320 setDatablocks(template, curCont, getMethods); 321 list.append(template.getProcessedDataValue(C_LISTENTRY_DATABLOCK, this)); 322 } 323 } 324 registerVariantDeps(cms, doc.getAbsoluteFilename(), null, null, parameters, null, allCdClasses, theClass); 326 } catch (Exception e) { 327 if (e instanceof CmsException) { 328 throw (CmsException) e; 329 } else { 330 throw new CmsLegacyException (e.getMessage(), CmsLegacyException.C_UNKNOWN_EXCEPTION, e); 331 } 332 } 333 return list.toString(); 334 } 335 336 349 protected String getUserParameter (Hashtable parameters, String tagcontent) throws CmsException { 350 String userparameter = ""; 351 String parameterName = null; 352 String parameterValue = null; 353 if (tagcontent != null) { 354 int index = tagcontent.indexOf(","); 355 if (index != -1) { 356 parameterName = tagcontent.substring(0, index); 357 if (!(parameterName.startsWith(C_FILTER_PARAMETERS_START))) { 359 throw new CmsLegacyException("The filterparameter has to be \"" 360 +C_FILTER_PARAMETERS_START+"N\" where 0 <= N <= 9."); 361 } 362 parameterValue = (String )parameters.get(parameterName); 363 if (parameterValue != null) { 364 userparameter = parameterValue; 365 } else if (tagcontent.length() > index+1) { 366 userparameter = tagcontent.substring(index+1); 367 } 368 } else { 369 return tagcontent; 370 } 371 } 372 return userparameter; 373 } 374 375 380 protected ArrayList getGetMethods (Class cdClass) { 381 ArrayList getMethods = new ArrayList (); 383 Method [] methods = cdClass.getMethods(); 385 Method m = null; 386 String name = null; 387 for (int i=0; i < methods.length; i++) { 389 m = methods[i]; 390 name = m.getName().toLowerCase(); 391 if (name.startsWith("get")) { 393 if (m.getReturnType().equals(String .class) && m.getParameterTypes().length == 0) { 395 getMethods.add(m); 396 } 397 } 398 } 399 return getMethods; 400 } 401 402 415 protected ArrayList getGetMethodsByName (Class cdClass, String [] names) 416 throws NoSuchMethodException { 417 ArrayList getMethods = new ArrayList (); 419 Class [] argTypes = new Class [0]; 420 for (int i=0; i < names.length; i++) { 421 getMethods.add(cdClass.getMethod(names[i], argTypes)); 422 } 423 return getMethods; 424 } 425 426 436 protected void setDatablocks(CmsXmlTemplateFile template, 437 A_CmsContentDefinition contentDefinition, 438 ArrayList methods) throws CmsException { 439 String datablockName= null; 440 Method method = null; 441 int size = methods.size(); 442 Object [] args = new Object [0]; 443 for (int i=0; i < size; i++) { 444 method = (Method )methods.get(i); 446 datablockName = (method.getName().substring(3)).toLowerCase(); 448 if (datablockName.endsWith("string")) { 450 datablockName = datablockName.substring(0, datablockName.lastIndexOf("string")); 451 } 452 try { 454 template.setData(datablockName, (String )method.invoke(contentDefinition, args)); 455 } catch (Exception e) { 456 if (CmsLog.getLog(this).isErrorEnabled()) { 457 CmsLog.getLog(this).error("Error during automatic call method '" + method.getName(), e); 458 } 459 template.setData(datablockName, C_ERROR_TEXT); 461 } 462 } } 464 465 480 protected Vector invokeFilterMethod (CmsObject cms, Class cdClass, String name, String userparameter) 481 throws NoSuchMethodException , InvocationTargetException , IllegalAccessException { 482 Method method = cdClass.getMethod(name, new Class [] {CmsObject.class, String .class}); 483 return (Vector )method.invoke(cdClass, new Object [] {cms, userparameter}); 484 } 485 486 496 public CmsCacheDirectives getCacheDirectives(CmsObject cms, String templateFile, 497 String elementName, Hashtable parameters, String templateSelector) { 498 499 CmsCacheDirectives result = new CmsCacheDirectives(true, false, false, false, false); 500 result.setCacheUri(true); 501 result.noAutoRenewAfterPublish(); 502 Vector para = new Vector (); 503 para.add(C_ID_PARAMETER); 504 for (int i=0; i < 10; i++) { 505 para.add(C_FILTER_PARAMETERS_START + i); 506 } 507 result.setCacheParameters(para); 508 509 return result; 510 } 511 }
| Popular Tags
|