001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/podcasts/tags/sakai_2-4-1/podcasts-app/src/java/org/sakaiproject/tool/podcasts/podPermBean.java $
003: * $Id: podPermBean.java 18092 2006-11-10 15:32:29Z josrodri@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.podcasts;
021:
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Set;
027:
028: import javax.faces.component.UIColumn;
029: import javax.faces.component.UIOutput;
030: import javax.faces.component.UISelectBoolean;
031: import javax.faces.component.html.HtmlDataTable;
032: import javax.faces.component.html.HtmlOutputText;
033: import javax.faces.component.html.HtmlSelectBooleanCheckbox;
034: import javax.faces.context.FacesContext;
035: import javax.faces.el.ValueBinding;
036: import javax.faces.model.SelectItem;
037:
038: import org.apache.commons.logging.Log;
039: import org.apache.commons.logging.LogFactory;
040: import org.sakaiproject.api.app.podcasts.PodcastService;
041: import org.sakaiproject.authz.api.AuthzGroup;
042: import org.sakaiproject.authz.api.GroupNotDefinedException;
043: import org.sakaiproject.authz.api.Role;
044: import org.sakaiproject.authz.cover.AuthzGroupService;
045: import org.sakaiproject.authz.cover.FunctionManager;
046: import org.sakaiproject.exception.IdUnusedException;
047: import org.sakaiproject.exception.PermissionException;
048: import org.sakaiproject.site.cover.SiteService;
049:
050: public class podPermBean {
051:
052: private final String CONTENT = "content";
053: private final String DOT = ".";
054: private final String SLASH = "/";
055: private final String SITE = "site";
056:
057: public class DecoratedCheckboxTableRow {
058: private String rowName;
059: private List checkboxValues;
060: private SelectItem[] checkboxSelectValues;
061:
062: public List getCheckboxValues() {
063: return checkboxValues;
064: }
065:
066: public void setCheckboxValues(List checkboxValues) {
067: this .checkboxValues = checkboxValues;
068: }
069:
070: public String getRowName() {
071: return rowName;
072: }
073:
074: public void setRowName(String rowName) {
075: this .rowName = rowName;
076: }
077:
078: public SelectItem[] getCheckboxSelectValues() {
079: checkboxSelectValues = new SelectItem[checkboxValues.size()];
080:
081: Iterator ckboxValIter = checkboxValues.iterator();
082:
083: for (int i = 0; i < checkboxValues.size(); i++) {
084: checkboxSelectValues[i] = new SelectItem(
085: (Boolean) ckboxValIter.next(), " ");
086: }
087:
088: return checkboxSelectValues;
089: }
090:
091: public void setCheckboxSelectValues(
092: SelectItem[] checkboxSelectValues) {
093: this .checkboxSelectValues = checkboxSelectValues;
094: }
095: }
096:
097: public class HtmlDynamicColumnCheckboxTable extends HtmlDataTable {
098:
099: /** The faces cell values to be put in the table */
100: private List dataTableContents;
101:
102: /** The number of columns to be put in the table */
103: private int numColumns = -1;
104:
105: /** A String value to bind to each checkbox in the table to a bean property */
106: private String checkboxBindingVar;
107:
108: /** A String value to bind the first column in the table to a bean property */
109: private String firstColumnBindingVar;
110:
111: public String getCheckboxBindingVar() {
112: return checkboxBindingVar;
113: }
114:
115: public void setCheckboxBindingVar(String checkboxBindingVar) {
116: this .checkboxBindingVar = checkboxBindingVar;
117: }
118:
119: public String getFirstColumnBindingVar() {
120: return firstColumnBindingVar;
121: }
122:
123: public void setFirstColumnBindingVar(
124: String firstColumnBindingVar) {
125: this .firstColumnBindingVar = firstColumnBindingVar;
126: }
127:
128: /**
129: *
130: *
131: */
132: public HtmlDynamicColumnCheckboxTable() {
133:
134: }
135:
136: /**
137: * This constructs the Faces component structure of the table. It is
138: * created column by column since each column's type is homogeneous.
139: * @ param headers A list of the header for each column
140: */
141: public void prepareDCDataTable(List headers) {
142:
143: numColumns = headers.size();
144: this .setNumColumns(numColumns);
145:
146: Iterator headerIter = headers.iterator();
147:
148: // Set columns.
149: for (int i = 0; i < numColumns; i++) {
150:
151: UIOutput header = new UIOutput();
152: header.setValue(headerIter.next());
153:
154: UIColumn column = new UIColumn();
155:
156: // Set output.
157: if (i == 0) {
158: UIOutput output = new UIOutput();
159: ValueBinding aRow = FacesContext
160: .getCurrentInstance().getApplication()
161: .createValueBinding(
162: "#{cellItem[" + i + "]}");
163: output.setValueBinding("value", aRow);
164:
165: // Create column FACES component and add label to it (column 0).
166: column = new UIColumn();
167: column.getChildren().add(output);
168: column.setHeader(header);
169:
170: } else {
171: UISelectBoolean output = new UISelectBoolean();
172: ValueBinding aRow = FacesContext
173: .getCurrentInstance().getApplication()
174: .createValueBinding(
175: "#{cellItem[" + i + "]}");
176: output.setValueBinding("value", aRow);
177:
178: // Create column FACES component and add checkbox to it.
179: column = new UIColumn();
180: column.getChildren().add(output);
181: column.setHeader(header);
182:
183: }
184:
185: // Add column.
186: this .getChildren().add(column);
187: }
188: }
189:
190: /**
191: * This returns the actual Faces component for each cell.
192: *
193: * @param rowId
194: * The String name for the row being constructed (to be put
195: * in column 0)
196: *
197: * @param colNumber
198: * Which column are we currently constructing
199: *
200: * @param select
201: * Whether the checkbox should be checked or not (ignored for
202: * column 0)
203: *
204: * @return Object Either the component passed in (column = 0) or a
205: * HtmlSelectBooleanCheckbox (every other column)
206: */
207: private Object getRowCell(String rowId, int colNumber,
208: boolean select) {
209:
210: if (colNumber == 0) {
211: // first column of row, add rowId
212: /* HtmlOutputText labelCell = new HtmlOutputText();
213: ValueBinding aRow = FacesContext.getCurrentInstance()
214: .getApplication()
215: .createValueBinding("#{" + firstColumnBindingVar + "}");
216: labelCell.setValueBinding("value", aRow);
217:
218: return labelCell;
219: */return rowId;
220: } else {
221: // Create a checkbox
222: HtmlSelectBooleanCheckbox checkboxCell = new HtmlSelectBooleanCheckbox();
223:
224: checkboxCell.setSelected(select);
225: // checkboxCell.setSubmittedValue(rowId + "_" + colNumber);
226: checkboxCell.setRendererType("javax.faces.Checkbox");
227:
228: ValueBinding aRow = FacesContext.getCurrentInstance()
229: .getApplication().createValueBinding(
230: "#{" + checkboxBindingVar + "["
231: + (colNumber - 1) + "]}");
232: checkboxCell.setValueBinding("value", aRow);
233:
234: // create MethodBinding so when checkbox checked, can process it
235: // Class [] classArray = new Class[1];
236: // classArray[0] = new ValueChangeEvent(checkboxCell,
237: // Boolean.FALSE, Boolean.FALSE);
238:
239: // MethodBinding mb =
240: // FacesContext.getCurrentInstance().getApplication().createMethodBinding("processCheckboxStateChange",
241: // classArray);
242: // checkboxCell.setValueChangeListener(mb);
243: return checkboxCell;
244:
245: }
246:
247: }
248:
249: /**
250: * Returns the dataTableContents.
251: */
252: public List getDataTableContents() {
253:
254: return dataTableContents;
255: }
256:
257: /**
258: * Sets the actual contents of the data table
259: */
260: public void setDataTableContents(List dataTableContents) {
261: this .dataTableContents = dataTableContents;
262: }
263:
264: /**
265: * This constructs the actual contents of the table
266: *
267: * @param firstColumn
268: * List of the labels for the first column
269: * @param headerRow
270: * List of the labels for the headers for each column
271: */
272: public void setDataTableContents(List firstColumn,
273: List headerRow) {
274: dataTableContents = new ArrayList();
275:
276: final int width = headerRow.size();
277:
278: final Iterator roleIter = firstColumn.iterator();
279: int rows = firstColumn.size();
280:
281: for (int i = 0; i < rows; i++) {
282: final List this Row = new ArrayList();
283: final String roleName = (String) roleIter.next();
284: final Collection podcasts = new ArrayList();
285:
286: String podcastFolderRef = "";
287: Iterator permIter = null;
288: AuthzGroup podAuthzGroup = null;
289:
290: try {
291: podcastFolderRef = SLASH
292: + CONTENT
293: + podcastService
294: .retrievePodcastFolderId(podcastService
295: .getSiteId());
296: podAuthzGroup = AuthzGroupService
297: .getAuthzGroup(podcastFolderRef);
298: } catch (PermissionException e) {
299: LOG
300: .warn("PermissionException trying to get roles for site "
301: + podcastService.getSiteId()
302: + e.getMessage());
303: } catch (GroupNotDefinedException e) {
304: LOG
305: .info("GroupNotDefinedException while constructing permission data table contents for site "
306: + podcastService.getSiteId() + ".");
307: }
308:
309: // Create a list of azGroup ids to get permissions
310: Collection podcastCollection = new ArrayList();
311: if (podAuthzGroup != null) {
312: podcastCollection.add(podAuthzGroup.getId());
313: }
314:
315: podcastCollection.add(getSiteRef());
316:
317: // get functions (permissions) for this role
318: Set rolePerms = AuthzGroupService.getAllowedFunctions(
319: roleName, podcastCollection);
320: permIter = rolePerms.iterator();
321:
322: Iterator headerIter = headerRow.iterator();
323:
324: for (int colNumber = 0; colNumber < numColumns; colNumber++) {
325: Object cell = null;
326:
327: if (colNumber != 0) {
328: final String permCheck = CONTENT + DOT
329: + (String) headerIter.next();
330: final boolean isChecked = rolePerms
331: .contains(permCheck);
332:
333: cell = getRowCell(roleName, colNumber,
334: isChecked);
335: } else {
336: cell = getRowCell(roleName, colNumber, false);
337: headerIter.next();
338: }
339:
340: this Row.add(cell);
341: }
342:
343: dataTableContents.add(this Row);
344: }
345:
346: }
347:
348: /**
349: * @param numColumns
350: * The numColumns to set.
351: */
352: public void setNumColumns(int numColumns) {
353: this .numColumns = numColumns;
354: }
355:
356: }
357:
358: /** dataTable subclass dynamically created */
359: private HtmlDynamicColumnCheckboxTable permTable;
360:
361: /** Name of the site for display purposes */
362: private String siteName;
363:
364: /** List of UI components to populate table */
365: private List permTableDataList;
366:
367: /** List of values to put in table */
368: private List checkboxTableValues;
369:
370: // injected beans
371: private Log LOG = LogFactory.getLog(podPermBean.class);
372: private PodcastService podcastService;
373:
374: /**
375: *
376: *
377: */
378: public podPermBean() {
379: }
380:
381: /**
382: *
383: * @return
384: */
385: public String processPermChange() {
386: return "cancel";
387: }
388:
389: /**
390: *
391: * @return
392: */
393: public String processPermCancel() {
394: return "cancel";
395: }
396:
397: /**
398: * @param podcastService
399: * The podcastService to set.
400: */
401: public void setPodcastService(PodcastService podcastService) {
402: this .podcastService = podcastService;
403: }
404:
405: /**
406: * Returns a list of user roles
407: *
408: * @return List List of user roles (String [])
409: */
410: public List getRoleNames() {
411: List rolesInfo = new ArrayList();
412:
413: String siteRef = getSiteRef();
414:
415: try {
416: AuthzGroup realm = AuthzGroupService.getAuthzGroup(siteRef);
417:
418: Set roles = realm.getRoles();
419: Iterator iter = roles.iterator();
420:
421: while (iter.hasNext()) {
422: Role role = (Role) iter.next();
423:
424: if (role != null)
425: rolesInfo.add(role.getId());
426: }
427:
428: } catch (GroupNotDefinedException e) {
429: LOG
430: .error("GroupNotDefinedException trying to get roles for site "
431: + podcastService.getSiteId()
432: + ". "
433: + e.getMessage());
434: }
435:
436: return rolesInfo;
437: }
438:
439: /**
440: * Sets the permissions table
441: */
442: public void setPermDataTable(HtmlDataTable permDataTable) {
443: this .permTable = (HtmlDynamicColumnCheckboxTable) permDataTable;
444:
445: }
446:
447: /**
448: * Returns the permissions table
449: */
450: public HtmlDataTable getPermDataTable() {
451: List roleNames = getRoleNames();
452: List permNames = getPermNames();
453: List permNamesPlus = new ArrayList();
454:
455: permNamesPlus.add("Role");
456: permNamesPlus.addAll(permNames);
457:
458: permTable = new HtmlDynamicColumnCheckboxTable();
459:
460: permTable.setStyleClass("listHier lines");
461: permTable.setCellpadding("0");
462: permTable.setCellspacing("0");
463: permTable.setBorder(0);
464: permTable
465: .setCheckboxBindingVar("podPerms.checkboxTableValues[1].checkboxValues");
466: permTable.setHeaderClass("navIntraTool");
467: permTable.prepareDCDataTable(permNamesPlus);
468: permTable.setDataTableContents(roleNames, permNamesPlus);
469: permTable.setValue("#{podPerms.checkboxTableValues}");
470: permTable.setVar("permItem");
471:
472: setCheckboxTableValues();
473:
474: return (HtmlDataTable) permTable;
475:
476: }
477:
478: /**
479: * Returns the names of the permissions (functions) available
480: *
481: * @return List List of permissions (functions) available (String[])
482: */
483: public List getPermNames() {
484: final List permNames = new ArrayList();
485: final List allFunctions = FunctionManager
486: .getRegisteredFunctions(CONTENT);
487:
488: final Iterator fIter = allFunctions.iterator();
489:
490: while (fIter.hasNext()) {
491: final String permission = (String) fIter.next();
492:
493: // TODO: Determine correct way to filter which permissions to show
494: if (permission.indexOf("all") == -1
495: && permission.indexOf("hidden") == -1) {
496: final String actPermName = permission
497: .substring(permission.indexOf(DOT) + 1);
498: permNames.add(actPermName);
499: }
500: }
501:
502: return permNames;
503: }
504:
505: /**
506: * @return Returns the permTableDataList.
507: */
508: public List getPermDataTableList() {
509: return permTable.getDataTableContents();
510:
511: }
512:
513: /**
514: * @param permTableDataList
515: * The permTableDataList to set.
516: */
517: public void setPermTableDataList(List permTableDataList) {
518: this .permTableDataList = permTableDataList;
519: }
520:
521: /**
522: * Returns the site name if valid site or empty string if not.
523: *
524: * @return Returns the siteName.
525: */
526: public String getSiteName() {
527: siteName = "";
528:
529: try {
530: siteName = SiteService.getSite(podcastService.getSiteId())
531: .getTitle();
532:
533: } catch (IdUnusedException e) {
534: LOG
535: .error("IdUnusedException attempting to get site name for site. "
536: + e.getMessage());
537: }
538:
539: return siteName;
540: }
541:
542: /**
543: * @param siteName
544: * The siteName to set.
545: */
546: public void setSiteName(String siteName) {
547: this .siteName = siteName;
548: }
549:
550: /**
551: *
552: * @return
553: */
554: private String getSiteRef() {
555: return SLASH + SITE + SLASH + podcastService.getSiteId();
556: }
557:
558: /**
559: *
560: * @return
561: */
562: public String getSiteId() {
563: return podcastService.getSiteId();
564: }
565:
566: public void setCheckboxTableValues() {
567: final List roleNames = getRoleNames();
568: final Iterator roleIter = roleNames.iterator();
569:
570: final List permNames = getPermNames();
571:
572: checkboxTableValues = new ArrayList();
573:
574: String podcastFolderRef = "";
575: AuthzGroup podAuthzGroup = null;
576:
577: try {
578: podcastFolderRef = SLASH
579: + CONTENT
580: + podcastService
581: .retrievePodcastFolderId(podcastService
582: .getSiteId());
583: podAuthzGroup = AuthzGroupService
584: .getAuthzGroup(podcastFolderRef);
585: } catch (PermissionException e) {
586: LOG
587: .warn("PermissionException trying to get roles for site "
588: + podcastService.getSiteId()
589: + e.getMessage());
590: } catch (GroupNotDefinedException e) {
591: LOG
592: .error("GroupNotDefinedException while constructing permission table for site "
593: + podcastService.getSiteId() + ".");
594: }
595:
596: // Create a list of azGroup ids to get permissions
597: Collection podcastCollection = new ArrayList();
598: if (podAuthzGroup != null) {
599: podcastCollection.add(podAuthzGroup.getId());
600: }
601:
602: podcastCollection.add(getSiteRef());
603:
604: while (roleIter.hasNext()) {
605: final DecoratedCheckboxTableRow tableRow = new DecoratedCheckboxTableRow();
606:
607: final String roleName = (String) roleIter.next();
608:
609: tableRow.setRowName(roleName);
610:
611: // get functions (permissions) for this role
612: Set rolePerms = AuthzGroupService.getAllowedFunctions(
613: roleName, podcastCollection);
614:
615: final Iterator permNameIter = permNames.iterator();
616: final List checkVal = new ArrayList();
617:
618: for (int j = 0; j < permNames.size(); j++) {
619: final String testPerm = CONTENT + DOT
620: + permNameIter.next();
621:
622: checkVal.add(new Boolean(rolePerms.contains(testPerm)));
623: }
624: tableRow.setCheckboxValues(checkVal);
625:
626: checkboxTableValues.add(tableRow);
627: }
628: }
629:
630: public List getCheckboxTableValues() {
631: return checkboxTableValues;
632: }
633:
634: public void setCheckboxTableValues(List checkboxTableValues) {
635: this.checkboxTableValues = checkboxTableValues;
636: }
637:
638: }
|