001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.rewriter.admin;
006:
007: import java.util.List;
008: import java.io.UnsupportedEncodingException;
009: import javax.servlet.http.HttpServletRequest;
010:
011: import com.iplanet.jato.RequestHandler;
012: import com.iplanet.jato.util.Encoder;
013: import com.iplanet.jato.model.Model;
014: import com.iplanet.jato.model.DatasetModel;
015: import com.iplanet.jato.model.ModelControlException;
016: import com.iplanet.jato.view.RequestHandlingTiledViewBase;
017: import com.iplanet.jato.view.TiledView;
018: import com.iplanet.jato.view.View;
019: import com.iplanet.jato.view.ViewBean;
020: import com.iplanet.jato.view.event.DisplayEvent;
021: import com.iplanet.jato.view.event.RequestInvocationEvent;
022: import com.iplanet.jato.view.event.TiledViewRequestInvocationEvent;
023: import com.iplanet.jato.view.html.HiddenField;
024: import com.iplanet.jato.view.html.HREF;
025: import com.iplanet.jato.view.html.CheckBox;
026: import com.iplanet.jato.view.html.StaticTextField;
027: import com.iplanet.jato.view.html.TextField;
028:
029: import com.sun.portal.rewriter.admin.model.RewriterModel;
030: import com.iplanet.am.console.base.AMViewBeanBase;
031:
032: /**
033: * The tileview that lists each rule in the select rule admin UI.
034: * The SelectRuleViewBean is the parent of this tileview where
035: * all the tile(s)/rule(s) are displayed.
036: */
037: public class SelectRuleTiledView extends RequestHandlingTiledViewBase
038: implements TiledView, RequestHandler {
039:
040: public static final String CHILD_CB_SELECT_RULE = "cbSelectRule";
041: public static final String CHILD_HREF_EDIT_RULE = "hrefEditRule";
042: public static final String CHILD_RULENAME_LABEL = "ruleNameLabel";
043: public static final String CHILD_HIDDEN_RULE_NAME = "ruleNameHidden";
044: public static final String CHILD_EDIT_LABEL = "editLabel";
045: public static final String CHILD_UPLOAD_URL_TEXT = "ruleSetUploadURL";
046: public static final String CHILD_DOWNLOAD_URL_TEXT = "ruleSetDownloadURL";
047: public static final String CHILD_UPLOAD_LABEL_TEXT = "ruleSetUploadLinkLabel";
048: public static final String CHILD_DOWNLOAD_LABEL_TEXT = "ruleSetDownloadLinkLabel";
049:
050: /* the maximum number of tiles per page */
051: private static final int MAX_DISPLAY_TILES = 200;
052:
053: private List ruleNames = null;
054: private String url = "";
055: private String pgEncoding = "UTF-8";
056:
057: /**
058: * Default constructor.
059: * @param parent the parent view of this tileview.
060: * @param name the name of this tileview.
061: */
062: public SelectRuleTiledView(View parent, String name) {
063: super (parent, name);
064: setMaxDisplayTiles(MAX_DISPLAY_TILES);
065: setPrimaryModel((DatasetModel) getDefaultModel());
066: registerChildren();
067: }
068:
069: /**
070: * Register the child objects of the tileview.
071: */
072: protected void registerChildren() {
073: registerChild(CHILD_CB_SELECT_RULE, CheckBox.class);
074: registerChild(CHILD_HREF_EDIT_RULE, HREF.class);
075: registerChild(CHILD_RULENAME_LABEL, StaticTextField.class);
076: registerChild(CHILD_EDIT_LABEL, StaticTextField.class);
077: registerChild(CHILD_HIDDEN_RULE_NAME, HiddenField.class);
078: registerChild(CHILD_UPLOAD_URL_TEXT, StaticTextField.class);
079: registerChild(CHILD_UPLOAD_LABEL_TEXT, StaticTextField.class);
080: registerChild(CHILD_DOWNLOAD_URL_TEXT, StaticTextField.class);
081: registerChild(CHILD_DOWNLOAD_LABEL_TEXT, StaticTextField.class);
082: }
083:
084: /**
085: * Creates and return the child view objects for the specified name.
086: * @param name the child view name.
087: * @return the child view object that corresponds to the given name.
088: */
089: protected View createChild(String name) {
090: View child = null;
091: if (name.equals(CHILD_CB_SELECT_RULE)) {
092: child = new CheckBox(this , CHILD_CB_SELECT_RULE, "true",
093: "false", false);
094: } else if (name.equals(CHILD_HREF_EDIT_RULE)) {
095: child = new HREF(this , CHILD_HREF_EDIT_RULE, "");
096: } else if (name.equals(CHILD_UPLOAD_URL_TEXT)) {
097: child = new StaticTextField(this , CHILD_UPLOAD_URL_TEXT, "");
098: } else if (name.equals(CHILD_UPLOAD_LABEL_TEXT)) {
099: child = new StaticTextField(this , CHILD_UPLOAD_LABEL_TEXT,
100: "");
101: } else if (name.equals(CHILD_DOWNLOAD_URL_TEXT)) {
102: child = new StaticTextField(this , CHILD_DOWNLOAD_URL_TEXT,
103: "");
104: } else if (name.equals(CHILD_DOWNLOAD_LABEL_TEXT)) {
105: child = new StaticTextField(this ,
106: CHILD_DOWNLOAD_LABEL_TEXT, "");
107: } else if (name.equals(CHILD_RULENAME_LABEL)) {
108: child = new StaticTextField(this , CHILD_RULENAME_LABEL, "");
109: } else if (name.equals(CHILD_EDIT_LABEL)) {
110: child = new StaticTextField(this , CHILD_EDIT_LABEL, "");
111: } else if (name.equals(CHILD_HIDDEN_RULE_NAME)) {
112: child = new HiddenField(this , CHILD_HIDDEN_RULE_NAME, "");
113: } else {
114: throw new IllegalArgumentException(name);
115: }
116: return child;
117: }
118:
119: /**
120: * begins displaying page. we set the required information
121: *
122: * @param event display event
123: * @throws ModelControlException if problem access value of component
124: */
125: public void beginDisplay(DisplayEvent event)
126: throws ModelControlException {
127: if (getPrimaryModel() == null) {
128: throw new ModelControlException("Primary model is null");
129: }
130:
131: RewriterModel rwm = getModel(getRequestContext().getRequest());
132: ruleNames = rwm.getRuleSetNames();
133: pgEncoding = (String) getParentViewBean().getDisplayFieldValue(
134: AMViewBeanBase.PAGE_ENCODING);
135: String reqURI = getRequestContext().getRequest()
136: .getRequestURI();
137: url = reqURI.substring(0, reqURI.indexOf('/', 1))
138: + "/RuleSetServlet?" + AMViewBeanBase.PAGE_ENCODING
139: + "=" + pgEncoding + "&RuleSetName=";
140:
141: if (ruleNames.size() > 0) {
142: getPrimaryModel().setSize(ruleNames.size());
143: } else {
144: getPrimaryModel().setSize(0);
145: }
146: super .beginDisplay(event);
147: resetTileIndex();
148: }
149:
150: /**
151: * moves the current tile position to the next available tile.
152: *
153: * @return true if another tile was available, false if the position
154: * remained unchanged because no next tile was available.
155: *
156: * @throws ModelControlException if manipulation of model fails.
157: */
158: public boolean nextTile() throws ModelControlException {
159: boolean movedToRow = super .nextTile();
160: RewriterModel rwm = getModel(getRequestContext().getRequest());
161: if (movedToRow) {
162: String rule = (String) ruleNames.get(getTileIndex());
163: String encodedRule = "";
164: if (rule != null && rule.length() > 0) {
165: try {
166: encodedRule = Encoder.encode(rule
167: .getBytes(pgEncoding));
168: } catch (UnsupportedEncodingException uee) {
169: // NOP
170: uee.printStackTrace();
171: }
172: }
173:
174: String fullUrl = url + encodedRule;
175: setDisplayFieldValue(CHILD_DOWNLOAD_URL_TEXT, fullUrl
176: + "&action=getRuleSet");
177: setDisplayFieldValue(CHILD_UPLOAD_LABEL_TEXT, rwm
178: .getLocalizedString("SelectRuleJSP.uploadLabel"));
179: setDisplayFieldValue(CHILD_DOWNLOAD_LABEL_TEXT, rwm
180: .getLocalizedString("SelectRuleJSP.downloadLabel"));
181: setDisplayFieldValue(CHILD_UPLOAD_URL_TEXT, fullUrl);
182: setDisplayFieldValue(CHILD_HREF_EDIT_RULE, rule);
183: setDisplayFieldValue(CHILD_RULENAME_LABEL, rule);
184: setDisplayFieldValue(CHILD_EDIT_LABEL, rwm
185: .getLocalizedString("SelectRuleJSP.editLabel"));
186: setDisplayFieldValue(CHILD_UPLOAD_LABEL_TEXT, rwm
187: .getLocalizedString("SelectRuleJSP.uploadLabel"));
188: setDisplayFieldValue(CHILD_DOWNLOAD_LABEL_TEXT, rwm
189: .getLocalizedString("SelectRuleJSP.downloadLabel"));
190: setDisplayFieldValue(CHILD_HIDDEN_RULE_NAME, rule);
191: }
192: return movedToRow;
193: }
194:
195: /**
196: * Returns the model object for the select rule UI.
197: * @return the model object for the select rule UI.
198: */
199: public RewriterModel getModel(HttpServletRequest req) {
200: SelectRuleViewBean svb = (SelectRuleViewBean) getParentViewBean();
201: return (RewriterModel) svb.getModel(req);
202: }
203:
204: /**
205: * Handles the event to edit a given ruleset.
206: * @param event the event to edit a given ruleset.
207: * @exception throws ModelControlException when error(s) occurred.
208: */
209: public void handleHrefEditRuleRequest(RequestInvocationEvent event)
210: throws ModelControlException {
211:
212: int tileNum = ((TiledViewRequestInvocationEvent) event)
213: .getTileNumber();
214: getPrimaryModel().setLocation(tileNum);
215:
216: /* Forward to the edit rule viewbean. */
217: EditRuleViewBean vb = (EditRuleViewBean) getViewBean(EditRuleViewBean.class);
218: vb.currentRuleSetName = (String) getDisplayFieldValue(CHILD_HREF_EDIT_RULE);
219: vb.forwardTo(getRequestContext());
220: }
221:
222: }
|