001: package org.sakaiproject.user.tool;
002:
003: import java.util.Iterator;
004: import java.util.List;
005: import java.util.Locale;
006: import java.util.ResourceBundle;
007:
008: import javax.faces.application.FacesMessage;
009: import javax.faces.component.UIComponent;
010: import javax.faces.component.UISelectBoolean;
011: import javax.faces.component.UIViewRoot;
012: import javax.faces.context.FacesContext;
013: import javax.faces.event.ValueChangeEvent;
014: import javax.faces.model.SelectItem;
015:
016: import org.apache.commons.logging.Log;
017: import org.apache.commons.logging.LogFactory;
018:
019: import org.sakaiproject.authz.api.AuthzGroup;
020: import org.sakaiproject.api.privacy.PrivacyManager;
021: import org.sakaiproject.tool.cover.SessionManager;
022: import org.sakaiproject.site.api.Site;
023: import org.sakaiproject.site.cover.SiteService;
024: import org.sakaiproject.tool.cover.ToolManager;
025: import org.sakaiproject.util.ResourceLoader;
026:
027: public class PrivacyBean {
028:
029: private final String VISIBLE = "privacy_visible";
030: private final String HIDDEN = "privacy_hidden";
031: private final String HIDE = "privacy_check_hide";
032: private final String SHOW = "privacy_check_show";
033: private final String SET_SHOW_ALL_STRING = "privacy_show_all_set";
034: private final String SET_HIDE_ALL_STRING = "privacy_hide_all_set";
035: private final String SHOW_ME = "Show Me";
036:
037: private String checkboxText;
038: private boolean changeStatus;
039: private String selectedSite;
040: private boolean siteSelected = false;
041: private String curSite;
042: private boolean allChanged = false;
043: private String changeAllMsg;
044: private boolean noSiteProcessErr = false;
045:
046: private SelectItem[] sites;
047:
048: /** * Resource bundle messages */
049: ResourceLoader msgs = new ResourceLoader("user-tool-prefs");
050:
051: /** Inject PrivacyManager */
052: private PrivacyManager privacyManager;
053:
054: private Log LOG = LogFactory.getLog(PrivacyBean.class);
055:
056: public void setPrivacyManager(PrivacyManager privacyManager) {
057: this .privacyManager = privacyManager;
058: }
059:
060: /** ========== Setters/Getters for UI ========== */
061:
062: public boolean isSiteSelected() {
063: return (isMyWorkspace()) ? siteSelected : false;
064: }
065:
066: public void setSiteSelected(boolean siteSelected) {
067: this .siteSelected = siteSelected;
068: }
069:
070: public boolean getAllChanged() {
071: return allChanged;
072: }
073:
074: public void setAllChanged(boolean allChanged) {
075: this .allChanged = allChanged;
076: }
077:
078: public String getChangeAllMsg() {
079: return changeAllMsg;
080: }
081:
082: public boolean isNoSiteProcessErr() {
083: return noSiteProcessErr;
084: }
085:
086: public boolean isChangeStatus() {
087: return changeStatus;
088: }
089:
090: public void setChangeStatus(boolean changeStatus) {
091: this .changeStatus = changeStatus;
092: }
093:
094: public String getSelectedSite() {
095: return selectedSite;
096: }
097:
098: public void setSelectedSite(String selectedSite) {
099: this .selectedSite = selectedSite;
100: }
101:
102: /**
103: * Returns 'visible' or 'hidden' based on status within site
104: */
105: public String getCurrentStatus() {
106: if (!isMyWorkspace()) {
107: curSite = getContextId();
108: }
109:
110: if (privacyManager.isViewable(curSite, getUserId())) {
111: return getMessageBundleString(VISIBLE);
112: } else {
113: return getMessageBundleString(HIDDEN);
114: }
115: }
116:
117: /**
118: * Return TRUE if privacy set to visible, FALSE if set to hidden
119: */
120: public boolean isShow() {
121: if (isMyWorkspace()) {
122: return privacyManager.isViewable(curSite, getUserId());
123: } else {
124: return privacyManager.isViewable(getContextId(),
125: getUserId());
126: }
127: }
128:
129: /**
130: * Returns TRUE if on MyWorkspace, FALSE if on a specific site
131: */
132: public boolean isMyWorkspace() {
133:
134: // get Site id
135: String siteId = getContextId();
136:
137: if (SiteService.getUserSiteId("admin").equals(siteId))
138: return false;
139:
140: final boolean where = SiteService.isUserSite(siteId);
141:
142: LOG.debug("Result of determinig if My Workspace: " + where);
143:
144: return where;
145: }
146:
147: /**
148: * Returns text currently displayed on checkbox
149: */
150: public String getCheckboxText() {
151: if (!isMyWorkspace()) {
152: curSite = getContextId();
153: }
154:
155: if (privacyManager.isViewable(curSite, getUserId())) {
156: checkboxText = getMessageBundleString(HIDE);
157: } else {
158: checkboxText = getMessageBundleString(SHOW);
159: }
160:
161: return checkboxText;
162: }
163:
164: /**
165: * Sets the text for the checkbox
166: */
167: public void setCheckboxText(String checkboxText) {
168: this .checkboxText = checkboxText;
169: }
170:
171: /**
172: * Returns an array of SelectItem for MyWorkspace page dropdown list
173: * @return
174: */
175: public SelectItem[] getSites() {
176: final List mySites = getSiteList();
177:
178: sites = new SelectItem[mySites.toArray().length + 1];
179:
180: final Iterator siteIter = mySites.iterator();
181: int sitesIndex = 1;
182:
183: sites[0] = new SelectItem("", "");
184:
185: while (siteIter.hasNext()) {
186: final Site site = (Site) siteIter.next();
187: sites[sitesIndex++] = new SelectItem("/site/"
188: + site.getId(), site.getTitle());
189: }
190:
191: return sites;
192: }
193:
194: /** ========== processes iteraction on UI ========== */
195:
196: /**
197: * Determines if ready to change status and calls method to do so
198: */
199: public String processUpdate() {
200: if (isMyWorkspace() && !siteSelected) {
201: noSiteProcessErr = true;
202:
203: return "main";
204: }
205:
206: // if user checked the checkbox
207: if (changeStatus) {
208: processChoice(isMyWorkspace() ? curSite : getContextId(),
209: new Boolean(SHOW_ME.equals(checkboxText)));
210:
211: // Reset the checkbox to not checked
212: changeStatus = false;
213: }
214:
215: return "main";
216: }
217:
218: /**
219: * Does the actual setting of the privacy status
220: */
221: private void processChoice(String contextId, Boolean status) {
222: privacyManager.setViewableState(contextId, getUserId(), status,
223: privacyManager.USER_RECORD_TYPE);
224:
225: }
226:
227: /**
228: * Sets the user's privacy status to Visible for all sites
229: */
230: public String processShowAll() {
231: List mySites = getSiteList();
232:
233: Iterator siteIter = mySites.iterator();
234:
235: while (siteIter.hasNext()) {
236: Site curSite = (Site) siteIter.next();
237:
238: processChoice("/site/" + curSite.getId(), Boolean.TRUE);
239: }
240:
241: allChanged = true;
242: changeAllMsg = getMessageBundleString(SET_SHOW_ALL_STRING);
243:
244: // Below so UI shows no site selected
245: siteSelected = false;
246: selectedSite = "";
247:
248: return "main";
249: }
250:
251: /**
252: * Sets the user's privacy status to Hidden for all sites
253: */
254: public String processHideAll() {
255: List mySites = getSiteList();
256:
257: Iterator siteIter = mySites.iterator();
258:
259: while (siteIter.hasNext()) {
260: Site curSite = (Site) siteIter.next();
261:
262: processChoice("/site/" + curSite.getId(), Boolean.FALSE);
263: }
264:
265: allChanged = true;
266: changeAllMsg = getMessageBundleString(SET_HIDE_ALL_STRING);
267:
268: // Below so UI shows no site selected
269: siteSelected = false;
270: selectedSite = "";
271:
272: return "main";
273: }
274:
275: /**
276: * Sets Bean variables to affect display when dropdown list
277: * in MyWorkspace is changed.
278: */
279: public void processSiteSelected(ValueChangeEvent e) {
280: allChanged = false;
281: noSiteProcessErr = false;
282:
283: if ("".equals((String) e.getNewValue())) {
284: siteSelected = false;
285: } else {
286: curSite = (String) e.getNewValue();
287: siteSelected = true;
288: }
289: }
290:
291: /** ========== Utility functions ========== */
292:
293: /**
294: * Pulls the message from the message bundle using the name passed in
295: *
296: * @param key
297: * The name in the MessageBundle for the message wanted
298: */
299: private String getMessageBundleString(String key) {
300: return msgs.getString(key);
301:
302: }
303:
304: /**
305: * Returns a list of sites user has access to (is a member of)
306: */
307: private List getSiteList() {
308: return SiteService
309: .getSites(
310: org.sakaiproject.site.api.SiteService.SelectionType.ACCESS,
311: null,
312: null,
313: null,
314: org.sakaiproject.site.api.SiteService.SortType.TITLE_ASC,
315: null);
316: }
317:
318: /**
319: * Returns context id (/site/site id)
320: */
321: private String getContextId() {
322: return "/site/"
323: + ToolManager.getCurrentPlacement().getContext();
324: }
325:
326: /**
327: * Returns the current user id
328: */
329: public String getUserId() {
330: return SessionManager.getCurrentSessionUserId();
331: }
332:
333: }
|