1 16 package org.apache.jetspeed.portal; 17 18 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 22 23 import org.apache.ecs.ConcreteElement; 25 26 import org.apache.jetspeed.om.profile.Entry; 28 import org.apache.jetspeed.om.profile.MetaInfo; 29 import org.apache.jetspeed.om.profile.PSMLDocument; 30 import org.apache.jetspeed.om.profile.Parameter; 31 import org.apache.jetspeed.om.profile.Profile; 32 import org.apache.jetspeed.om.profile.psml.PsmlParameter; 33 import org.apache.jetspeed.portal.Portlet; 34 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 35 import org.apache.jetspeed.services.logging.JetspeedLogger; 36 import org.apache.jetspeed.services.rundata.JetspeedRunData; 37 38 import org.apache.turbine.util.RunData; 40 41 57 public class JetspeedPortletInstance implements PortletInstance 58 { 59 62 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedPortletInstance.class.getName()); 63 64 65 private Profile m_profile = null; 66 67 68 private Profile m_clonedProfile = null; 69 70 73 private JetspeedRunData m_rundata = null; 74 75 79 private Portlet m_portlet = null; 80 81 87 public JetspeedPortletInstance(Portlet portlet, RunData data) 88 { 89 this(portlet, ((JetspeedRunData) data).getProfile()); 90 m_rundata = (JetspeedRunData)data; 91 92 m_clonedProfile = ((JetspeedRunData)data).getCustomizedProfile(); 94 } 95 96 102 public JetspeedPortletInstance(Portlet portlet, Profile profile) 103 { 104 m_portlet = portlet; 105 m_profile = profile; 106 } 107 108 112 protected JetspeedPortletInstance() 113 { 114 } 116 117 120 public String getAttribute(String name, String dftValue) 121 { 122 Entry entry = getEntry(); 123 124 if (entry == null) 125 { 126 return dftValue; 127 } 128 Parameter attr = entry.getParameter(name); 129 if (attr != null) 130 { 131 return attr.getValue(); 132 } 133 else 134 { 135 return dftValue; 136 } 137 } 138 139 142 public String getAttribute(String name) 143 { 144 Entry entry = getEntry(); 145 146 if (entry == null) 147 { 148 return ""; 149 } 150 Parameter attr = entry.getParameter(name); 151 if (attr != null) 152 { 153 return attr.getValue(); 154 } 155 else 156 { 157 return ""; 158 } 159 } 160 161 164 public void setAttribute(String name, String value) 165 { 166 setupForUpdate(); 168 169 Entry entry = getEntry(); 170 171 Parameter attr = entry.getParameter(name); 172 173 if(value == null) 175 { 176 removeAttribute(name); 177 } 178 else if (attr != null) 179 { 180 attr.setValue(value); 181 } 182 else 184 { 185 PsmlParameter newAttr = new PsmlParameter(); 186 newAttr.setName(name); 187 newAttr.setValue(value); 188 entry.addParameter(newAttr); 189 } 190 } 191 192 195 public void removeAttribute(String name) 196 { 197 setupForUpdate(); 199 200 Entry entry = getEntry(); 201 202 Iterator params = entry.getParameterIterator(); 204 int index = -1; 205 int count = 0; 206 while (params.hasNext()) 207 { 208 Parameter param = (Parameter) params.next(); 209 if (param.getName().equalsIgnoreCase(name)) 210 { 211 index = count; 212 break; 213 } 214 count++; 215 } 216 217 if (index != -1) 220 { 221 entry.removeParameter(index); 222 } 223 } 224 225 228 public void removeAllAttributes() 229 { 230 setupForUpdate(); 232 233 Entry entry = getEntry(); 234 235 entry.removeAllParameter(); 236 } 237 238 241 public Iterator getAttributes() 242 { 243 Entry entry = getEntry(); 244 245 return entry.getParameterIterator(); 246 } 247 248 251 public Iterator getAttributeNames() 252 { 253 Iterator itr = getAttributes(); 254 ArrayList list = new ArrayList (); 255 while(itr.hasNext()); 256 { 257 Parameter param = (Parameter) itr.next(); 258 list.add(param.getName()); 259 } 260 261 return list.iterator(); 262 } 263 264 267 public PSMLDocument getDocument() 268 { 269 return getProfile().getDocument(); 270 } 271 272 275 public Profile getProfile() 276 { 277 if (m_clonedProfile != null) return m_clonedProfile; 279 280 return m_profile; 281 } 282 283 286 public Entry getEntry() 287 { 288 return getDocument().getEntryById(m_portlet.getID()); 289 } 290 291 294 public String getDescription() 295 { 296 Entry entry = getEntry(); 297 298 String description = null; 299 if (entry != null) 300 { 301 MetaInfo metaInfo = entry.getMetaInfo(); 302 if (metaInfo != null) 303 { 304 description = metaInfo.getDescription(); 305 } 306 } 307 return m_portlet.getDescription(description); 308 } 309 310 313 public String getName() 314 { 315 return getEntry().getParent(); 316 } 317 318 321 public String getTitle() 322 { 323 Entry entry = getEntry(); 324 325 String title = null; 326 if (entry != null) 327 { 328 MetaInfo metaInfo = entry.getMetaInfo(); 329 if (metaInfo != null) 330 { 331 title = metaInfo.getTitle(); 332 } 333 } 334 return m_portlet.getTitle(title); 335 } 336 337 340 public String getImage() 341 { 342 Entry entry = getEntry(); 343 344 String image = null; 345 if (entry != null) 346 { 347 MetaInfo metaInfo = entry.getMetaInfo(); 348 if (metaInfo != null) 349 { 350 return image = metaInfo.getImage(); 351 } 352 } 353 return m_portlet.getImage(image); 354 } 355 356 359 public Portlet getPortlet() 360 { 361 return m_portlet; 362 } 363 364 367 public ConcreteElement getContent() 368 { 369 return m_portlet.getContent(m_rundata); 370 } 371 372 375 public boolean isShowTitleBar() 376 { 377 return m_portlet.isShowTitleBar(m_rundata); 378 } 379 380 385 private void setupForUpdate() 386 { 387 if (m_clonedProfile != null) return; 388 389 try 390 { 391 m_clonedProfile = (Profile) m_profile.clone(); 392 } 393 catch (CloneNotSupportedException e) 394 { 395 logger.warn("JetspeedPortletInstance: cannot clone Profile!: " + e); 396 } 397 } 398 } 399 400 | Popular Tags |