001: /*
002: * AllowDenyModel.java
003: *
004: * Created on September 13, 2005, 4:42 PM
005: *
006: * To change this template, choose Tools | Options and locate the template under
007: * the Source Creation and Management node. Right-click the template and choose
008: * Open. You can then make changes to the template in the Source Editor.
009: */
010:
011: package com.sun.portal.admin.console.sra.utils;
012:
013: import com.sun.data.provider.DataProvider;
014: import java.util.List;
015: import java.util.ArrayList;
016: import java.util.Iterator;
017: import com.sun.web.ui.model.Option;
018:
019: /**
020: *
021: * @author aa147324
022: */
023: public class AllowDenyModel {
024:
025: private SraObjectListDataProvider _allowDenyDataProvider;
026:
027: private ArrayList operations;
028:
029: private static int ALLOW = 1;
030:
031: private static int DENY = 2;
032:
033: /** Creates a new instance of AllowDenyModel */
034: private AllowDenyModel() {
035: }
036:
037: public AllowDenyModel(String id, ArrayList states, List allowList,
038: List denyList) {
039: operations = new ArrayList();
040: operations
041: .add(new Option(Boolean.TRUE, (String) states.get(0)));
042: operations
043: .add(new Option(Boolean.FALSE, (String) states.get(1)));
044:
045: List allowDenyList = new ArrayList(convertStrToADItem(
046: allowList, true));
047: allowDenyList.addAll(convertStrToADItem(denyList, false));
048: _allowDenyDataProvider = new SraObjectListDataProvider(
049: AllowDenyItem.class.getName() + "_" + id, allowDenyList);
050: }
051:
052: public DataProvider getAllowDenyDataProvider() {
053: return _allowDenyDataProvider;
054: }
055:
056: public List getAllowList() {
057: return convertADItemToStr(getAllowDenyList(ALLOW));
058: }
059:
060: public List getDenyList() {
061: return convertADItemToStr(getAllowDenyList(DENY));
062: }
063:
064: private List convertADItemToStr(List aclObjectlist) {
065: // Using a list of ACL objects, create a list of String objects of the
066: // URL's contained in the ACL object
067: List list = new ArrayList(aclObjectlist.size());
068: Iterator iter = aclObjectlist.iterator();
069: while (iter.hasNext()) {
070: AllowDenyItem element = (AllowDenyItem) iter.next();
071: list.add(element.getItem());
072: }
073: /*This done so that when we save at a level other than top the setAttributes() call
074: // throws an exception. As per Prashant, if the list being passed to setAttributes() is empty
075: // we need to put an empty string in it.
076: if ( list.isEmpty())
077: list.add("");*/
078: return list;
079: }
080:
081: /**
082: * @param
083: * @return
084: */
085: private List convertStrToADItem(List thelist, boolean isAllowed) {
086: List list = new ArrayList(thelist.size());
087: Iterator iter = thelist.iterator();
088:
089: while (iter.hasNext()) {
090: String element = (String) iter.next();
091: list.add(new AllowDenyItem(element, isAllowed));
092: }
093: return list;
094: }
095:
096: public void addNewItem() {
097: if (_allowDenyDataProvider.canAppendRow()) {
098: _allowDenyDataProvider.appendRow(new AllowDenyItem("",
099: false));
100: _allowDenyDataProvider.commitChanges();
101: }
102: }
103:
104: public void deleteItem() {
105: _allowDenyDataProvider.commitChanges();
106: AllowDenyItem[] allowDenyArray = (AllowDenyItem[]) _allowDenyDataProvider
107: .getList().toArray(new AllowDenyItem[0]);
108:
109: for (int i = 0; i < allowDenyArray.length; i++) {
110: AllowDenyItem acl = allowDenyArray[i];
111:
112: if (acl.getSelected()) {
113: _allowDenyDataProvider.removeObject(acl);
114: }
115: }
116: _allowDenyDataProvider.commitChanges();
117: //Following lines to be added
118: // getAllowDenyHostList();
119:
120: // storeDataToStore(ACCESS_HOSTS);
121: // storeDataToStore(DENY_HOSTS);
122: }
123:
124: public ArrayList getOperations() {
125: return operations;
126: }
127:
128: private List getAllowDenyList(int type) {
129: List allowList = new ArrayList();
130: List denyList = new ArrayList();
131: _allowDenyDataProvider.commitChanges();
132: AllowDenyItem[] allowDenyArray = (AllowDenyItem[]) _allowDenyDataProvider
133: .getList().toArray(new AllowDenyItem[0]);
134: for (int i = 0; i < allowDenyArray.length; i++) {
135: AllowDenyItem element = allowDenyArray[i];
136: if (element.getIsAllowed())
137: allowList.add(element);
138: else
139: denyList.add(element);
140: }
141: if (type == ALLOW) {
142: return allowList;
143: }
144: return denyList;
145: }
146: }
|