1 16 17 package org.apache.jetspeed.portal.controls; 18 19 import org.apache.turbine.util.RunData; 21 22 import org.apache.jetspeed.om.security.JetspeedUser; 24 import org.apache.jetspeed.portal.Portlet; 25 import org.apache.jetspeed.portal.PortletSet; 26 import org.apache.jetspeed.portal.PortletState; 27 import org.apache.jetspeed.portal.PanedPortletController; 28 import org.apache.jetspeed.services.JetspeedSecurity; 29 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 30 import org.apache.jetspeed.services.logging.JetspeedLogger; 31 import org.apache.jetspeed.services.rundata.JetspeedRunData; 32 import org.apache.jetspeed.services.security.PortalResource; 33 import org.apache.jetspeed.util.template.JetspeedLink; 34 import org.apache.jetspeed.util.template.JetspeedLinkFactory; 35 import org.apache.jetspeed.services.persistence.PersistenceManager; 36 import org.apache.jetspeed.portal.PortletInstance; 37 38 import org.apache.velocity.context.Context; 40 41 import java.util.Collection ; 43 import java.util.Comparator ; 44 import java.util.List ; 45 import java.util.TreeSet ; 46 import java.util.Vector ; 47 import java.util.Enumeration ; 48 49 57 public class VelocityPortletSetControl extends VelocityPortletControl 58 { 59 60 63 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(VelocityPortletSetControl.class.getName()); 64 65 71 public void buildContext(RunData rundata, Context context) 72 { 73 if (getPortlet() instanceof PortletSet) 74 { 75 context.put("tabs", getTabs((PortletSet) getPortlet(), rundata, context)); 76 } 77 } 78 79 89 private Collection getTabs(PortletSet portlets, RunData rundata, Context context) 90 { 91 TreeSet tabs = new TreeSet (new PortletTabComparator()); 92 PanedPortletController controller = null; 93 94 if (portlets.getController() instanceof PanedPortletController) 97 { 98 controller = (PanedPortletController) portlets.getController(); 99 } 100 101 int count = 0; 102 for (Enumeration en = portlets.getPortlets(); en.hasMoreElements(); count++) 103 { 104 Portlet p = (Portlet) en.nextElement(); 105 PortalResource portalResource = new PortalResource(p); 106 107 try 109 { 110 JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata); 111 portalResource.setOwner(jsLink.getUserName()); 112 JetspeedLinkFactory.putInstance(jsLink); 113 } 114 catch (Exception e) 115 { 116 logger.warn(e.toString(), e); 117 portalResource.setOwner(null); 118 } 119 JetspeedRunData jdata = (JetspeedRunData) rundata; 120 boolean hasView = JetspeedSecurity.checkPermission((JetspeedUser) jdata.getUser(), 121 portalResource, 122 JetspeedSecurity.PERMISSION_VIEW); 123 if (!hasView) 124 { 125 continue; 126 } 127 if ((p instanceof PortletState) && (((PortletState) p).isClosed(rundata))) 129 { 130 continue; 131 } 132 133 String mstate = p.getAttribute("_menustate", "open", rundata); 134 if (mstate.equals("closed")) 135 { 136 continue; 137 } 138 139 PortletTab tab = new PortletTab(); 140 141 String title = null; 143 PortletInstance pi = PersistenceManager.getInstance(p, rundata); 144 if (pi != null) 145 { 146 title = pi.getTitle(); 147 if (title == null) 148 { 149 title = (p.getTitle() != null) ? p.getTitle() : p.getName(); 150 } 151 } 152 tab.setTitle(title); 153 154 tab.setPosition(p.getPortletConfig().getPosition()); 155 if (tabs.contains(tab)) 156 { 157 PortletTab lastTab = (PortletTab) tabs.last(); 158 int nextPos = lastTab.getPosition() + 1; 159 tab.setPosition(nextPos); 160 } 161 162 if (controller != null) 163 { 164 tab.setSelected(controller.isSelected(p, rundata)); 165 tab.setLink(controller.getPortletURI(p, rundata).toString()); 166 } 167 168 tab.setActions(buildActionList(rundata, p)); 169 tabs.add(tab); 170 } 171 172 return tabs; 173 } 174 175 177 public class PortletTab 178 { 179 private String title = null; 180 private boolean selected = false; 181 private String link = null; 182 private List actions = null; 183 private int position = -1; 184 185 public String getTitle() 186 { 187 return this.title; 188 } 189 190 public void setTitle(String title) 191 { 192 this.title = title; 193 } 194 195 public boolean isSelected() 196 { 197 return this.selected; 198 } 199 200 public void setSelected(boolean selected) 201 { 202 this.selected = selected; 203 } 204 205 public String getLink() 206 { 207 return this.link; 208 } 209 210 public void setLink(String link) 211 { 212 this.link = link; 213 } 214 215 public List getActions() 216 { 217 return (this.actions == null) ? new Vector () : this.actions; 218 } 219 220 public void setActions(List actions) 221 { 222 this.actions = actions; 223 } 224 225 public int getPosition() 226 { 227 return position; 228 } 229 230 public void setPosition(int pos) 231 { 232 position = pos; 233 } 234 } 235 236 240 public class PortletTabComparator implements Comparator 241 { 242 243 246 public int compare(Object o1, Object o2) 247 { 248 try 249 { 250 PortletTab pt1 = (PortletTab) o1; 251 PortletTab pt2 = (PortletTab) o2; 252 int pos1 = pt1.getPosition(); 253 int pos2 = pt2.getPosition(); 254 255 if (pos1 < pos2) 256 { 257 return -1; 258 } 259 else if (pos1 > pos2) 260 { 261 return 1; 262 } 263 else 264 { 265 return 0; 266 } 267 } 268 catch (ClassCastException e) 269 { 270 logger.error( "Exception in compare", e ); 271 return 0; 272 } 273 } 274 } 275 276 } | Popular Tags |