1 16 package org.apache.jetspeed.tutorial.modules.actions.portlets; 17 18 19 import java.util.List ; 21 import java.sql.Connection ; 22 23 24 import org.apache.turbine.util.Log; 26 import org.apache.turbine.util.RunData; 27 import org.apache.turbine.services.TurbineServices; 28 import org.apache.turbine.util.DynamicURI; 29 30 import org.apache.velocity.context.Context; 32 33 import org.apache.torque.util.Criteria; 35 import org.apache.torque.Torque; 36 37 import org.apache.jetspeed.portal.portlets.VelocityPortlet; 39 import org.apache.jetspeed.modules.actions.portlets.VelocityPortletAction; 40 import org.apache.jetspeed.util.PortletConfigState; 41 import org.apache.jetspeed.util.StringUtils; 42 import org.apache.jetspeed.util.template.JetspeedLink; 43 import org.apache.jetspeed.util.template.JetspeedLinkFactory; 44 45 46 import org.apache.jetspeed.tutorial.om.Cafe; 48 import org.apache.jetspeed.tutorial.om.CafePeer; 49 50 56 public class TutorialCafeAction extends VelocityPortletAction 57 { 58 public static final String UI_MODE = "js_mode"; 61 public static final String UI_ROW_ID = "js_rowid"; 62 63 public static final String UI_EDIT = "Edit"; 64 public static final String UI_UPDATE = "Update"; 65 public static final String UI_ADD = "Add"; 66 public static final String UI_DELETE = "Delete"; 67 public static final String UI_REFRESH = "Refresh"; 68 public static final String UI_VALIDATE = "Validate"; 69 70 private static final String SESSION_CAFE = "cafe.instance"; 71 private static final String SESSION_UPDATE_MODE = "cafe.mode"; 72 private static final String UI_CAFE = "cafe"; 73 74 private static final String NO_CURRENT_REC = "Cafe Tutorial: no current record"; 75 76 83 84 protected void buildNormalContext(VelocityPortlet portlet, 85 Context context, 86 RunData rundata) 87 { 88 String mode = null; 89 Cafe cafe = null; 90 91 try 92 { 93 mode = this.getQueryParameter(rundata, UI_MODE, UI_REFRESH); 94 cafe = (Cafe)rundata.getUser().getTemp(SESSION_CAFE); 95 96 if (mode.equalsIgnoreCase(UI_REFRESH)) 100 { 101 if (cafe != null) 102 { 103 rundata.getParameters().setProperties(cafe); 104 } 105 } 106 else if (mode.equalsIgnoreCase(UI_EDIT) || 107 mode.equalsIgnoreCase(UI_DELETE)) 108 { 109 int rowid = Integer.parseInt(this.getQueryParameter(rundata, UI_ROW_ID)); 110 111 context.put(UI_ROW_ID, String.valueOf(rowid)); 112 113 Criteria criteria = new Criteria(); 115 criteria.add( CafePeer.CAFE_ID, rowid); 116 List cafes = CafePeer.doSelect(criteria); 117 if (cafes != null && cafes.size() > 0) 118 { 119 cafe = (Cafe)cafes.get(0); 120 } 121 else 122 { 123 throw new Exception ("Cafe for id="+rowid+" not found."); 124 } 125 126 rundata.getUser().setTemp(SESSION_CAFE, cafe); 127 128 rundata.getUser().setTemp(SESSION_UPDATE_MODE, mode); 129 130 } 131 else if (mode.equalsIgnoreCase(UI_ADD)) 132 { 133 cafe = new Cafe(); 134 cafe.setCafeName(""); 135 rundata.getUser().setTemp(SESSION_CAFE, cafe); 136 rundata.getUser().setTemp(SESSION_UPDATE_MODE, mode); 137 } 138 139 context.put(UI_CAFE, cafe); 140 context.put(UI_MODE, mode); 141 142 143 } 144 catch (Exception e) 145 { 146 Log.error(e); 147 rundata.setMessage(e.toString()); 148 } 149 } 150 151 152 153 159 public void doUpdate(RunData rundata, Context context) throws Exception 160 { 161 Cafe cafe = null; 162 Connection con = null; 163 164 try 165 { 166 con = Torque.getConnection(); 167 168 cafe = (Cafe)rundata.getUser().getTemp(SESSION_CAFE); 169 170 if(cafe == null) 171 { 172 Log.error(NO_CURRENT_REC); 173 rundata.setMessage(NO_CURRENT_REC); 174 return; 175 } 176 177 rundata.getParameters().setProperties(cafe); 178 179 validate(cafe); 180 181 cafe.save(con); 182 183 con.commit(); 184 185 returnToBrowser(rundata, true); 186 187 } 188 catch (Exception e) 189 { 190 Log.error("error in saving cafe: " + e); 191 rundata.setMessage(e.toString()); 192 if (con != null) 193 { 194 con.rollback(); 195 } 196 197 } 198 finally 199 { 200 try 201 { 202 Torque.closeConnection(con); 203 } 204 catch (Exception e) 205 {} 206 } 207 208 } 209 210 216 public void doDelete(RunData rundata, Context context) throws Exception 217 { 218 Cafe cafe = null; 219 Connection con = null; 220 221 try 222 { 223 con = Torque.getConnection(); 224 225 cafe = (Cafe)rundata.getUser().getTemp(SESSION_CAFE); 226 227 if(cafe == null) 228 { 229 Log.error(NO_CURRENT_REC); 230 rundata.setMessage(NO_CURRENT_REC); 231 return; 232 } 233 234 235 CafePeer.doDelete(cafe, con); 236 237 con.commit(); 238 239 returnToBrowser(rundata, true); 240 241 } 242 catch (Exception e) 243 { 244 Log.error("error in deleting cafe: " + e); 245 rundata.setMessage(e.toString()); 246 247 if (con != null) 248 { 249 con.rollback(); 250 } 251 252 } 253 finally 254 { 255 try 256 { 257 Torque.closeConnection(con); 258 } 259 catch (Exception e) 260 {} 261 } 262 263 } 264 265 271 public void doCancel(RunData rundata, Context context) throws Exception 272 { 273 returnToBrowser(rundata, false); 274 } 275 276 280 public static String getQueryParameter(RunData rundata, String attrName) 281 { 282 String str = rundata.getParameters().getString(attrName); 283 if(str != null && str.length() > 0 && str.trim().length() > 0 ) 284 return str; 285 return null; 286 } 287 288 292 public static String getQueryParameter(RunData rundata, String attrName, String defaultValue) 293 { 294 String str = getQueryParameter(rundata, attrName); 295 if(str == null) 296 return defaultValue; 297 else 298 return str; 299 } 300 301 302 306 private void validate(Cafe cafe) throws Exception 307 { 308 309 if(isEmpty(cafe.getCafeName())) 310 { 311 throw new Exception ("Cafe Name must be entered."); 312 } 313 } 314 315 320 public static boolean isEmpty(String str) 321 { 322 if(!(str != null && str.trim().length() > 0)) 323 { 324 return true; 325 } 326 return false; 327 } 328 329 333 private void returnToBrowser(RunData rundata, boolean refresh) 334 { 335 try 336 { 337 JetspeedLink link = JetspeedLinkFactory.getInstance(rundata); 338 DynamicURI duri = link.getPaneByName("TutorialCafeBrowser"); 339 if (refresh) 340 { 341 duri.addQueryData(CafeBrowserAction.BROWSER_COMMAND, CafeBrowserAction.BROWSER_REFRESH); 342 } 343 rundata.setRedirectURI(duri.toString()); 344 JetspeedLinkFactory.putInstance(link); 345 } 346 catch (Exception e) 347 { 348 Log.error(e); 349 rundata.setMessage(e.toString()); 350 } 351 } 352 353 } 354 | Popular Tags |