1 16 package org.apache.jetspeed.tutorial.modules.actions.portlets; 17 18 import org.apache.jetspeed.portal.portlets.VelocityPortlet; 19 20 import org.apache.turbine.util.Log; 22 import org.apache.turbine.util.RunData; 23 import org.apache.turbine.services.TurbineServices; 24 25 import org.apache.velocity.context.Context; 27 28 import org.apache.jetspeed.portal.portlets.VelocityPortlet; 30 import org.apache.jetspeed.modules.actions.portlets.VelocityPortletAction; 31 import org.apache.jetspeed.util.PortletConfigState; 32 import org.apache.jetspeed.util.StringUtils; 33 34 40 public class CobiJonesPortletAction extends VelocityPortletAction 41 { 42 43 public static final String INPUT_FIRST_NAME = "firstname"; 44 public static final String INPUT_LAST_NAME = "lastname"; 45 public static final String INPUT_POSITION = "position"; 46 public static final String INPUT_CAPS = "caps"; 47 public static final String INPUT_ACTIVE = "active"; 48 49 public static final String PLAYER = "player"; 50 public static final String COBI_ERROR = "cobierror"; 51 52 53 60 61 62 protected void buildNormalContext(VelocityPortlet portlet, 63 Context context, 64 RunData rundata) 65 { 66 try 67 { 68 Player player = (Player)rundata.getUser().getTemp(PLAYER); 69 if (null == player) 70 { 71 player = createNewPlayer(); 72 rundata.getUser().setTemp(PLAYER, player); 73 } 74 context.put(PLAYER, player); 75 76 String cobiError = (String )rundata.getRequest().getAttribute(COBI_ERROR); 78 if (null != cobiError) 79 { 80 context.put(COBI_ERROR, cobiError); 81 } 82 } 83 catch (Exception e) 84 { 85 Log.error(e); 86 context.put(COBI_ERROR, e.toString()); 87 } 88 } 89 90 91 97 public void doUpdate(RunData rundata, Context context) throws Exception 98 { 99 Player player = (Player)rundata.getUser().getTemp(PLAYER); 100 if (null == player) 101 { 102 player = createNewPlayer(); 103 rundata.getUser().setTemp(PLAYER, player); 104 } 105 106 String cobi = rundata.getParameters().getString(INPUT_FIRST_NAME); 107 String jones = rundata.getParameters().getString(INPUT_LAST_NAME); 108 if (!cobi.equalsIgnoreCase("Cobi") || !jones.equalsIgnoreCase("Jones")) 109 { 110 rundata.getRequest().setAttribute(COBI_ERROR, 111 "Hey now, you cant substitute Cobi with " 112 + cobi + " " + jones + "!"); 113 } 114 player.setFirstName(cobi); 115 player.setLastName(jones); 116 player.setPosition(rundata.getParameters().getString(INPUT_POSITION)); 117 player.setCaps(rundata.getParameters().getInt(INPUT_CAPS)); 118 player.setActive(rundata.getParameters().getBoolean(INPUT_ACTIVE)); 119 120 rundata.getUser().setTemp(PLAYER, player); 121 122 } 123 124 126 public Player createNewPlayer() 127 { 128 return new Player("Cobi", "Jones", "Midfielder", 150, true); 129 130 } 131 132 public class Player 133 { 134 private String firstName; 135 private String lastName; 136 private String position; 137 private int caps; 138 boolean active; 139 140 public Player(String firstName, 141 String lastName, 142 String position, 143 int caps, 144 boolean active) 145 { 146 this.firstName = firstName; 147 this.lastName = lastName; 148 this.position = position; 149 this.caps = caps; 150 this.active = active; 151 } 152 153 154 public String getFirstName() 155 { 156 return this.firstName; 157 } 158 159 public void setFirstName(String firstName) 160 { 161 this.firstName = firstName; 162 } 163 164 public String getLastName() 165 { 166 return this.lastName; 167 } 168 169 public void setLastName(String lastName) 170 { 171 this.lastName = lastName; 172 } 173 174 public String getPosition() 175 { 176 return this.position; 177 } 178 179 public void setPosition(String position) 180 { 181 this.position = position; 182 } 183 184 public int getCaps() 185 { 186 return this.caps; 187 } 188 189 public void setCaps(int caps) 190 { 191 this.caps = caps; 192 } 193 194 public boolean getActive() 195 { 196 return this.active; 197 } 198 199 public void setActive(boolean active) 200 { 201 this.active = active; 202 } 203 } 204 } 205 | Popular Tags |