001: /**
002: * Copyright 2002 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: *
013: * @ Author Bhavanishankar
014: */package com.sun.portal.netlet.admin;
015:
016: // JDK classes
017:
018: import java.io.Serializable;
019: import java.util.Iterator;
020: import java.util.Map;
021: import java.util.Set;
022: import java.util.StringTokenizer;
023: import java.util.Vector;
024:
025: import javax.servlet.http.HttpServletRequest;
026:
027: import com.iplanet.jato.RequestHandler;
028: import com.iplanet.jato.model.DatasetModel;
029: import com.iplanet.jato.model.ModelControlException;
030: import com.iplanet.jato.util.Encoder;
031: import com.iplanet.jato.view.RequestHandlingTiledViewBase;
032: import com.iplanet.jato.view.TiledView;
033: import com.iplanet.jato.view.View;
034: import com.iplanet.jato.view.ViewBean;
035: import com.iplanet.jato.view.event.DisplayEvent;
036: import com.iplanet.jato.view.event.RequestInvocationEvent;
037: import com.iplanet.jato.view.event.TiledViewRequestInvocationEvent;
038: import com.iplanet.jato.view.html.CheckBox;
039: import com.iplanet.jato.view.html.HREF;
040: import com.iplanet.jato.view.html.HiddenField;
041: import com.iplanet.jato.view.html.StaticTextField;
042: import com.sun.portal.netlet.admin.model.NetletAdminServiceModel;
043: import com.sun.portal.netlet.admin.model.NetletAdminServiceModelImpl;
044:
045: public class NetletRulesTiledView extends RequestHandlingTiledViewBase
046: implements TiledView, RequestHandler {
047: public static final String CHILD_CB_SELECT_RULE = "cbSelectRule";
048: public static final String CHILD_HREF_EDIT_RULE = "hrefEditRule";
049: public static final String CHILD_HREF_TEXT = "hrefText";
050: public static final String CHILD_RULE_NAME = "ruleName";
051: public static final String CHILD_EDIT_LABEL = "editLabel";
052:
053: private NetletAdminServiceModel model = null;
054: private NetletAdminModelManager modelManager = null;
055: private Vector netletRules = null;
056:
057: /* the maximum number of tiles per page */
058: private static final int MAX_DISPLAY_TILES = 100;
059:
060: /**
061: * Default constructor.
062: * @param parent the parent view of this tileview.
063: * @param name the name of this tileview.
064: */
065: public NetletRulesTiledView(View parent, String name) {
066: super (parent, name);
067: setMaxDisplayTiles(MAX_DISPLAY_TILES);
068: setPrimaryModel((DatasetModel) getDefaultModel());
069: registerChildren();
070: }
071:
072: /**
073: * Register the child objects of the tileview.
074: */
075: protected void registerChildren() {
076: registerChild(CHILD_CB_SELECT_RULE, CheckBox.class);
077: registerChild(CHILD_HREF_EDIT_RULE, HREF.class);
078: registerChild(CHILD_HREF_TEXT, StaticTextField.class);
079: registerChild(CHILD_RULE_NAME, HiddenField.class);
080: registerChild(CHILD_EDIT_LABEL, StaticTextField.class);
081: }
082:
083: /**
084: * Creates and return the child view objects for the specified name.
085: * @param name the child view name.
086: * @return the child view object that corresponds to the given name.
087: */
088: protected View createChild(String name) {
089: getModelMgr();
090: if (name.equals(CHILD_CB_SELECT_RULE)) {
091: return new CheckBox(this , CHILD_CB_SELECT_RULE, "true",
092: "false", false);
093: } else if (name.equals(CHILD_HREF_EDIT_RULE)) {
094: return new HREF(this , CHILD_HREF_EDIT_RULE, "");
095: } else if (name.equals(CHILD_HREF_TEXT)) {
096: return new StaticTextField(this , CHILD_HREF_EDIT_RULE, "");
097: } else if (name.equals(CHILD_RULE_NAME)) {
098: return new HiddenField(this , CHILD_RULE_NAME, "");
099: } else if (name.equals(CHILD_EDIT_LABEL)) {
100: return new StaticTextField(this , CHILD_EDIT_LABEL,
101: modelManager.getString("edit.label"));
102: } else {
103: throw new IllegalArgumentException(name);
104: }
105: }
106:
107: public void beginDisplay(DisplayEvent event)
108: throws ModelControlException {
109: getModel(); // Get the admin model manager and model.
110: model.process();
111: Set s = model.getNetletRules();
112: netletRules = new Vector(s);
113: getPrimaryModel().setSize(netletRules.size());
114: }
115:
116: protected NetletAdminServiceModel getModel() {
117: if (model == null) {
118: HttpServletRequest req = getRequestContext().getRequest();
119: model = new NetletAdminServiceModelImpl(req,
120: "srapnetletadminmsgs", getPageSessionAttributes(),
121: getServiceName(), isTemplate());
122: }
123: return model;
124: }
125:
126: public NetletAdminModelManager getModelMgr() {
127: if (modelManager == null) {
128: modelManager = (NetletAdminModelManager) getRequestContext()
129: .getModelManager();
130: }
131: return modelManager;
132: }
133:
134: public boolean nextTile() throws ModelControlException {
135: boolean movedToRow = super .nextTile();
136: if (movedToRow) {
137: if (netletRules == null) {
138: getModel(); // Get the admin model manager and model.
139: model.process();
140: Set s = model.getNetletRules();
141: netletRules = new Vector(s);
142: }
143: // getPrimaryModel().setLocation(getTileIndex());
144: if (netletRules != null) {
145: String nr = (String) netletRules.get(getTileIndex());
146: StringTokenizer tok = new StringTokenizer(nr,
147: EditNetletRuleViewBean.RULE_DELIM);
148: if (tok.hasMoreElements()) {
149: tok = new StringTokenizer(tok.nextToken(),
150: EditNetletRuleViewBean.ALGO_DELIM);
151: }
152: if (tok.hasMoreElements()) {
153: String ruleName = tok.nextToken();
154: setDisplayFieldValue(CHILD_HREF_TEXT, ruleName);
155: setDisplayFieldValue(CHILD_RULE_NAME, ruleName);
156: }
157: }
158: }
159: return movedToRow;
160: }
161:
162: /**
163: * Handles the event to edit a given ruleset.
164: * @param event the event to edit a given ruleset.
165: * @exception throws ModelControlException when error(s) occurred.
166: */
167: public void handleHrefEditRuleRequest(RequestInvocationEvent event)
168: throws ModelControlException {
169: /* Forward to the edit rule viewbean. */
170: getModelMgr();
171: modelManager
172: .setCurrentNetletRulesRow(((TiledViewRequestInvocationEvent) event)
173: .getTileNumber());
174: EditNetletRuleViewBean vb = (EditNetletRuleViewBean) getViewBean(EditNetletRuleViewBean.class);
175: passPgSessionMap(vb);
176: vb.forwardTo(getRequestContext());
177: }
178:
179: /*
180: * Returns the netletRules vector
181: * Called by NetletAdminServiceViewBean when DELETE button of
182: * netlet rules is clicked
183: */
184: public Vector getNetletRules() {
185: if (netletRules == null) {
186: getModel(); // Get the admin model manager and model.
187: model.process();
188: Set s = model.getNetletRules();
189: netletRules = new Vector(s);
190: }
191: return netletRules;
192: }
193:
194: public int getNetletRulesCount() {
195: Vector nrs = getNetletRules();
196: if (nrs != null) {
197: return nrs.size();
198: }
199: return 0;
200: }
201:
202: /*
203: * Gets the pageSessionAttributes map from the parent viewBean (NetletAdminServicViewBean)
204: * and sets these attributes for the other viewBean
205: */
206: public Map getPageSessionAttributes() {
207: NetletAdminServiceViewBean viewBean = (NetletAdminServiceViewBean) getParentViewBean();
208: String attrs = viewBean.getPageSessionAttributeString();
209: Map attributes = null;
210: if (attrs != null) {
211: try {
212: attributes = (Map) Encoder.deserialize(Encoder
213: .decodeHttp64(attrs), false);
214: } catch (Exception ex) {
215: attributes = null;
216: }
217: }
218: return attributes;
219: }
220:
221: public boolean isTemplate() {
222: Map attributes = getPageSessionAttributes();
223: String template = null;
224: if (attributes != null) {
225: template = (String) attributes
226: .get(NetletAdminServiceViewBean.TEMPLATE_ATTR);
227: }
228: if (template != null && template.equals("true")) {
229: return true;
230: } else {
231: return false;
232: }
233: }
234:
235: public String getServiceName() {
236: Map attributes = getPageSessionAttributes();
237: String svcName = null;
238: if (attributes != null)
239: svcName = (String) attributes
240: .get(NetletAdminServiceViewBean.SVC_NAME_ATTR);
241: return svcName;
242: }
243:
244: public void passPgSessionMap(ViewBean other) {
245: Map attributes = getPageSessionAttributes();
246: if ((attributes != null) && (attributes.size() > 0)) {
247: Iterator iter = attributes.keySet().iterator();
248: while (iter.hasNext()) {
249: String key = (String) iter.next();
250: other.setPageSessionAttribute(key,
251: (Serializable) attributes.get(key));
252: }
253: }
254: }
255: }
|