001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.ac.usecases;
019:
020: import org.apache.lenya.ac.User;
021: import org.apache.lenya.ac.ItemUtil;
022:
023: /**
024: * Usecase to edit a user's profile.
025: */
026: public class UserProfile extends AccessControlUsecase {
027:
028: protected static final String USER_ID = "userId";
029: protected static final String FULL_NAME = "fullName";
030: protected static final String EMAIL = "email";
031: protected static final String DESCRIPTION = "description";
032: protected static final String MENU_LOCALE = "defaultMenuLocale";
033: protected static final String DOCUMENT_LOCALE = "defaultDocumentLocale";
034:
035: /**
036: * Ctor.
037: */
038: public UserProfile() {
039: super ();
040: }
041:
042: /**
043: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckExecutionConditions()
044: */
045: protected void doCheckExecutionConditions() throws Exception {
046:
047: String email = getParameterAsString(UserProfile.EMAIL);
048: if (!ItemUtil.isValidEmail(email)) {
049: addErrorMessage("Please enter a valid e-mail address.");
050: }
051: }
052:
053: /**
054: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
055: */
056: protected void doExecute() throws Exception {
057: super .doExecute();
058:
059: String fullName = getParameterAsString(UserProfile.FULL_NAME);
060: String description = getParameterAsString(UserProfile.DESCRIPTION);
061: String email = getParameterAsString(UserProfile.EMAIL);
062: String defaultMenuLocale = getParameterAsString(UserProfile.MENU_LOCALE);
063: String defaultDocumentLocale = getParameterAsString(UserProfile.DOCUMENT_LOCALE);
064:
065: getUser().setEmail(email);
066: getUser().setName(fullName);
067: getUser().setDescription(description);
068: getUser().setDefaultMenuLocale(defaultMenuLocale);
069: getUser().setDefaultDocumentLocale(defaultDocumentLocale);
070: getUser().save();
071:
072: }
073:
074: private User user;
075:
076: /**
077: * Returns the currently edited user.
078: * @return A user.
079: */
080: protected User getUser() {
081: return this .user;
082: }
083:
084: /**
085: * @see org.apache.lenya.cms.usecase.Usecase#setParameter(java.lang.String, java.lang.Object)
086: */
087: public void setParameter(String name, Object value) {
088: super .setParameter(name, value);
089:
090: if (name.equals(USER_ID)) {
091: String userId = (String) value;
092: this .user = getUserManager().getUser(userId);
093: if (this .user == null) {
094: throw new RuntimeException("User [" + userId
095: + "] not found.");
096: }
097:
098: setParameter(EMAIL, this.user.getEmail());
099: setParameter(DESCRIPTION, this.user.getDescription());
100: setParameter(MENU_LOCALE, this.user.getDefaultMenuLocale());
101: setParameter(DOCUMENT_LOCALE, this.user
102: .getDefaultDocumentLocale());
103: setParameter(FULL_NAME, this.user.getName());
104: }
105: }
106:
107: }
|