001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/profile/tags/sakai_2-4-1/profile-app/src/java/org/sakaiproject/tool/profile/ProfileTool.java $
003: * $Id: ProfileTool.java 20322 2007-01-12 22:49:51Z wagnermr@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006, 2007 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.profile;
021:
022: import java.net.MalformedURLException;
023: import java.net.URL;
024: import java.util.regex.Matcher;
025: import java.util.regex.Pattern;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.sakaiproject.api.app.profile.Profile;
030: import org.sakaiproject.api.app.profile.ProfileManager;
031: import org.sakaiproject.site.cover.SiteService;
032: import org.sakaiproject.tool.cover.ToolManager;
033: import org.sakaiproject.util.FormattedText;
034: import org.sakaiproject.util.ResourceLoader;
035:
036: /**
037: * @author rshastri
038: */
039: public class ProfileTool {
040: private static final Log LOG = LogFactory.getLog(ProfileTool.class);
041:
042: /** Resource bundle using current language locale */
043: private static ResourceLoader rb = new ResourceLoader(
044: "org.sakaiproject.tool.profile.bundle.Messages");
045:
046: private static final String NONE = "none";
047:
048: private static final String UNIVERSITY_PHOTO = "universityId";
049:
050: private static final String PICTURE_URL = "pictureUrl";
051:
052: private ProfileManager profileService;
053:
054: private Profile profile;
055:
056: private boolean loadingFirstTime = true;
057:
058: private String pictureIdPreference = NONE;
059:
060: private boolean displayPicture = false;
061:
062: private boolean displayNoProfileMsg = false;
063:
064: private boolean displayEvilTagMsg = false;
065:
066: private boolean displayEmptyFirstNameMsg = false;
067:
068: private boolean displayEmptyLastNameMsg = false;
069:
070: private boolean displayMalformedPictureUrlError = false;
071:
072: private boolean displayMalformedHomepageUrlError = false;
073:
074: private String malformedUrlError = null;
075:
076: private String evilTagMsg = null;
077:
078: /**
079: * Process data for save action on edit page.
080: *
081: * @return navigation outcome: return to main page or if no user is present throw permission exception
082: */
083: public String processActionEditSave() {
084: LOG.debug("processActionEditSave()");
085: displayEvilTagMsg = false;
086: displayEmptyFirstNameMsg = false;
087: displayEmptyLastNameMsg = false;
088: displayMalformedPictureUrlError = false;
089: displayMalformedHomepageUrlError = false;
090: if ((profile != null) && (profile.getUserId() == null)) {
091: LOG.error("processActionEditSave :" + "No User Found");
092:
093: return "permissionException";
094: }
095: if (profile.getFirstName() == null
096: || profile.getFirstName().trim().length() < 1) {
097: displayEmptyFirstNameMsg = true;
098: return "edit";
099: }
100: if (profile.getLastName() == null
101: || profile.getLastName().trim().length() < 1) {
102: displayEmptyLastNameMsg = true;
103: return "edit";
104: }
105: if (profile.getOtherInformation() != null) {
106: StringBuffer alertMsg = new StringBuffer();
107: String errorMsg = null;
108: try {
109: errorMsg = FormattedText.processFormattedText(profile
110: .getOtherInformation(), alertMsg);
111: if (alertMsg.length() > 0) {
112: evilTagMsg = alertMsg.toString();
113: displayEvilTagMsg = true;
114: return "edit";
115: }
116: } catch (Exception e) {
117: LOG.error(" " + errorMsg, e);
118: }
119: }
120:
121: if ((getPictureIdPreference() != null)
122: && getPictureIdPreference().equals(UNIVERSITY_PHOTO)) {
123: profile
124: .setInstitutionalPictureIdPreferred(new Boolean(
125: true));
126: profile.setPictureUrl(null);
127: displayPicture = true;
128: this .pictureIdPreference = UNIVERSITY_PHOTO;
129: } else if ((getPictureIdPreference() != null)
130: && (getPictureIdPreference().equals(PICTURE_URL))) {
131: profile.setInstitutionalPictureIdPreferred(new Boolean(
132: false));
133: displayPicture = true;
134: this .pictureIdPreference = PICTURE_URL;
135: if (profile.getPictureUrl() != null
136: && profile.getPictureUrl().trim().length() > 0) {
137: try {
138: String pictureUrl = validateURL(profile
139: .getPictureUrl());
140: profile.setPictureUrl(pictureUrl);
141: } catch (MalformedURLException e) {
142: this .displayMalformedPictureUrlError = true;
143: this .malformedUrlError = rb.getString("validurl")
144: + " \"" + profile.getPictureUrl() + "\" "
145: + rb.getString("invalid");
146: return "edit";
147: }
148: }
149: } else {
150: // returns null or none
151: profile.setInstitutionalPictureIdPreferred(new Boolean(
152: false));
153: profile.setPictureUrl(null);
154: displayPicture = false;
155: this .pictureIdPreference = NONE;
156: }
157:
158: // Catch a bad url passed in homepage.
159: if (profile.getHomepage() != null
160: && profile.getHomepage().trim().length() > 0) {
161: try {
162: String homepageUrl = validateURL(profile.getHomepage()
163: .trim());
164: profile.setHomepage(homepageUrl);
165: } catch (MalformedURLException e) {
166: this .displayMalformedHomepageUrlError = true;
167: this .malformedUrlError = rb.getString("validurl")
168: + " \"" + profile.getHomepage() + "\" "
169: + rb.getString("invalid");
170: return "edit";
171: }
172: }
173:
174: try {
175: profileService.save(profile);
176: LOG.debug("User record updated for Id :-"
177: + profile.getUserId());
178:
179: return "main";
180: }
181:
182: catch (Exception e) {
183: LOG.debug(e.getMessage(), e);
184: }
185: return "main";
186: }
187:
188: /**
189: * Setup before navigating to edit page
190: *
191: * @return navigation outcome: return to edit page or if no user is present throw permission exception
192: */
193: public String processActionEdit() {
194: LOG.debug("processActionEdit()");
195: try {
196: if ((profile != null) && (profile.getUserId() == null)) {
197: LOG.equals("processActionEdit : " + "No User Found");
198: return "PermissionException";
199: }
200: setPictureIdPreference(profile);
201: return "edit";
202: } catch (Exception e) {
203: LOG.error(e.getMessage(), e);
204: return null;
205: }
206: }
207:
208: /**
209: * @return
210: */
211: public String processCancel() {
212: LOG.debug("processCancel()");
213: profile = profileService.getProfile();
214: return "main";
215: }
216:
217: /**
218: * Setup to fetch a profile
219: *
220: * @return Profile for user logged in or empty profile
221: */
222: public Profile getProfile() {
223: LOG.debug("getProfile()");
224: if (loadingFirstTime) {
225: profile = profileService.getProfile();
226: setPictureIdPreference(profile);
227: loadingFirstTime = false;
228: } else {
229: if (profile == null) {
230: displayNoProfileMsg = true;
231: } else {
232: if ((profile.getFirstName() == null)
233: || (profile.getLastName() == null)) {
234: displayNoProfileMsg = true;
235: } else {
236: if (profile.getFirstName().equalsIgnoreCase("")
237: || profile.getLastName().equalsIgnoreCase(
238: ""))
239: displayNoProfileMsg = true;
240: else
241: displayNoProfileMsg = false;
242: }
243: }
244: }
245: return profile;
246: }
247:
248: /**
249: * Getter for ProfileManager service
250: *
251: * @return instance of ProfileManager
252: */
253: public ProfileManager getProfileService() {
254: LOG.debug("getProfileService()");
255: return profileService;
256: }
257:
258: /**
259: * Setter for ProfileManager service
260: *
261: * @param profileService
262: */
263: public void setProfileService(ProfileManager profileService) {
264: if (LOG.isDebugEnabled()) {
265: LOG.debug("setProfileService(ProfileManager"
266: + profileService + ")()");
267: }
268: this .profileService = profileService;
269:
270: }
271:
272: /**
273: * @return
274: */
275: public boolean isDisplayNoProfileMsg() {
276: LOG.debug("isDisplayNoProfileMsg()");
277: return displayNoProfileMsg;
278: }
279:
280: /**
281: * Getter for property if the tool bean is loaded for first time
282: *
283: * @return boolean value
284: */
285: public boolean isLoadingFirstTime() {
286: LOG.debug("isLoadingFirstTime()");
287: return loadingFirstTime;
288: }
289:
290: /**
291: * Returns display picture preference
292: *
293: * @return String
294: */
295: public String getPictureIdPreference() {
296: LOG.debug("getPictureIdPreference()");
297: return pictureIdPreference;
298: }
299:
300: /**
301: * Set display picture preference
302: *
303: * @param pictureIDPreference
304: */
305: public void setPictureIdPreference(String pictureIdPreference) {
306: if (LOG.isDebugEnabled()) {
307: LOG.debug("setPictureIDPreference(String"
308: + pictureIdPreference + ")");
309: }
310: this .pictureIdPreference = pictureIdPreference;
311: }
312:
313: public boolean isShowTool() {
314: LOG.debug("isShowTool()");
315: return profileService.isShowTool();
316: }
317:
318: public boolean isShowSearch() {
319: LOG.debug("isShowSearch()");
320: return profileService.isShowSearch();
321: }
322:
323: /**
324: * @return
325: */
326: public String getTitle() {
327: LOG.debug("getTitle()");
328: return SiteService.findTool(
329: ToolManager.getCurrentPlacement().getId()).getTitle();
330: }
331:
332: /**
333: * @return
334: */
335: public String getEvilTagMsg() {
336: LOG.debug("getEvilTagMsg()");
337: return evilTagMsg;
338: }
339:
340: /**
341: * @return
342: */
343: public boolean isDisplayEvilTagMsg() {
344: LOG.debug("isDisplayEvilTagMsg()");
345: return displayEvilTagMsg;
346: }
347:
348: /**
349: * @return
350: */
351: public boolean isDisplayEmptyFirstNameMsg() {
352: LOG.debug("isDisplayEmptyFirstNameMsg()");
353: return displayEmptyFirstNameMsg;
354: }
355:
356: /**
357: * @return
358: */
359: public boolean isDisplayEmptyLastNameMsg() {
360: LOG.debug("isDisplayEmptyLastNameMsg()");
361: return displayEmptyLastNameMsg;
362: }
363:
364: /**
365: * @return
366: */
367: public boolean isDisplayNoPicture() {
368: LOG.debug("isDisplayPicture()");
369: return profileService.isDisplayNoPhoto(profile);
370: }
371:
372: /**
373: * @param profile
374: */
375: public void setProfile(Profile profile) {
376: if (LOG.isDebugEnabled()) {
377: LOG.debug("setProfile(Profile" + profile + ")");
378: }
379: this .profile = profile;
380: }
381:
382: /**
383: * @return
384: */
385: public boolean isDisplayPictureURL() {
386: LOG.debug("isDisplayPictureURL()");
387: return profileService.isDisplayPictureURL(profile);
388: }
389:
390: /**
391: * @return
392: */
393: public boolean isDisplayUniversityPhoto() {
394: LOG.debug("isDisplayUniversityPhoto()");
395: return profileService.isDisplayUniversityPhoto(profile);
396: }
397:
398: /**
399: * @return
400: */
401: public boolean isDisplayUniversityPhotoUnavailable() {
402: LOG.debug("isDisplayUniversityPhotoUnavailable()");
403: return profileService
404: .isDisplayUniversityPhotoUnavailable(profile);
405: }
406:
407: /**
408: * @param profile
409: */
410: private void setPictureIdPreference(Profile profile) {
411: if (LOG.isDebugEnabled()) {
412: LOG.debug("setPictureIdPreference(Profile" + profile + ")");
413: }
414: if (profile.isInstitutionalPictureIdPreferred() != null
415: && profile.isInstitutionalPictureIdPreferred()
416: .booleanValue() == true) {
417: this .pictureIdPreference = UNIVERSITY_PHOTO;
418: this .displayPicture = true;
419: } else if (profile.getPictureUrl() != null
420: && profile.getPictureUrl().length() > 0) {
421: this .pictureIdPreference = PICTURE_URL;
422: this .displayPicture = true;
423: } else {
424: this .pictureIdPreference = NONE;
425: this .displayPicture = false;
426: }
427:
428: }
429:
430: /**
431: * @return
432: */
433: public boolean isDisplayMalformedPictureUrlError() {
434: LOG.debug("isDisplayMalformedPictureUrlError()");
435: return displayMalformedPictureUrlError;
436: }
437:
438: /**
439: * @return
440: */
441: public boolean isDisplayMalformedHomepageUrlError() {
442: LOG.debug("isDisplayMalformedHomepageUrlError()");
443: return displayMalformedHomepageUrlError;
444: }
445:
446: /**
447: * @return
448: */
449: public String getMalformedUrlError() {
450: LOG.debug("getMalformedUrlError()");
451: return malformedUrlError;
452: }
453:
454: /**
455: *
456: * @param url
457: * @return
458: * @throws MalformedURLException
459: */
460: private String validateURL(String url) throws MalformedURLException {
461: if (url == null || url.equals("")) {
462: // ignore the empty url field
463: } else if (url.indexOf("://") == -1) {
464: // if it's missing the transport, add http://
465: url = "http://" + url;
466: }
467:
468: if (url != null && !url.equals("")) {
469: // valid protocol?
470: try {
471: // test to see if the input validates as a URL.
472: // Checks string for format only.
473: URL u = new URL(url);
474: } catch (MalformedURLException e1) {
475: try {
476: Pattern pattern = Pattern
477: .compile("\\s*([a-zA-Z0-9]+)://([^\\n]+)");
478: Matcher matcher = pattern.matcher(url);
479: if (matcher.matches()) {
480: // if URL has "unknown" protocol, check remaider with
481: // "http" protocol and accept input it that validates.
482: URL test = new URL("http://" + matcher.group(2));
483: } else {
484: throw e1;
485: }
486: } catch (MalformedURLException e2) {
487: throw e1;
488: }
489: }
490: }
491: return url;
492: }
493:
494: }
|