001: /*
002: * Copyright 2003 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package velosurf.util;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021: import java.util.Locale;
022: import java.util.Arrays;
023: import java.util.Map;
024: import java.util.HashMap;
025: import java.text.MessageFormat;
026:
027: import velosurf.web.l10n.Localizer;
028: import velosurf.model.Entity;
029:
030: /**
031: * Used to store contextual values relatives to the user (in a web context, there is one UserContext per http session).
032: *
033: * @author <a href="mailto:claude.brisson@gmail.com">Claude Brisson</a>
034: */
035: public class UserContext {
036:
037: /** key used to store the user context in the http session.
038: */
039: public static final String USER_CONTEXT_KEY = "velosurf.util.UserContext:session-key";
040:
041: /**
042: * Constructor.
043: */
044: public UserContext() {
045: // Logger.dumpStack();
046: }
047:
048: /**
049: * Last error setter.
050: * @param err error
051: */
052: public synchronized void setError(String err) {
053: error = err;
054: }
055:
056: /**
057: * Last error getter.
058: * @return last error message.
059: */
060: public String getError() {
061: return error;
062: }
063:
064: /** Localizer setter.
065: *
066: * @param loc localizer
067: */
068: public synchronized void setLocalizer(Localizer loc) {
069: localizer = loc;
070: }
071:
072: /** Locale setter.
073: *
074: * @param loc Locale
075: */
076: public synchronized void setLocale(Locale loc) {
077: locale = loc;
078: }
079:
080: /**
081: * Localize a parameterized message.
082: * @param str message to localize
083: * @param params parameters that are meant to replace "{0}", "{1}", ... in the message
084: * @return localized message
085: */
086: public String localize(String str, Object... params) {
087: if (localizer == null) {
088: return MessageFormat.format(str, params);
089: } else if (params.length == 0) {
090: return localizer.get(str);
091: } else {
092: return localizer.get(str, params);
093: }
094: }
095:
096: /**
097: * Clear validation errors.
098: */
099: public synchronized void clearValidationErrors() {
100: validationErrors.clear();
101: }
102:
103: /**
104: * Add a validation error.
105: * @param err validation error
106: */
107: public synchronized void addValidationError(String err) {
108: validationErrors.add(err);
109: }
110:
111: /** Get all validation error messages.
112: *
113: * @return validation error messages
114: */
115: public synchronized List<String> getValidationErrors() {
116: /* returning null allows a test like "#if($db.validationErrors)" */
117: if (validationErrors.size() == 0) {
118: return null;
119: }
120: List<String> ret = new ArrayList<String>(validationErrors);
121: validationErrors.clear();
122: return ret;
123: }
124:
125: /** generic getter.
126: *
127: */
128: public Object get(String key) {
129: if ("error".equalsIgnoreCase(key)) {
130: return getError();
131: } else if ("validationErrors".equalsIgnoreCase(key)) {
132: return getValidationErrors();
133: } else if ("locale".equalsIgnoreCase(key)) {
134: return getLocale();
135: }
136: return null;
137: }
138:
139: /**
140: * Locale getter.
141: * @return current locale
142: */
143: public Locale getLocale() {
144: if (localizer != null) {
145: return localizer.getLocale();
146: } else if (locale != null) {
147: return locale;
148: } else {
149: return Locale.getDefault();
150: }
151: }
152:
153: /**
154: * Set the last inserted ID for an entity.
155: * @param entity entity
156: * @param id last inserted id
157: */
158: public synchronized void setLastInsertedID(Entity entity, long id) {
159: lastInsertedIDs.put(entity, id);
160: }
161:
162: /**
163: * Get the last inserted ID for an entity.
164: * @param entity entity
165: * @return last inserted ID of -1
166: */
167: public long getLastInsertedID(Entity entity) {
168: Long id = lastInsertedIDs.get(entity);
169: if (id != null) {
170: return id.longValue();
171: } else {
172: Logger.error("getLastInsertID called for entity '" + entity
173: + "' which doesn't have any");
174: return -1;
175: }
176: }
177:
178: /** last error message */
179: private String error = "";
180: /** list of validation error messages */
181: private List<String> validationErrors = new ArrayList<String>();
182: /** localizer */
183: private Localizer localizer = null;
184: /** locale */
185: private Locale locale = null;
186: /** map of last inserted IDs */
187: private Map<Entity, Long> lastInsertedIDs = new HashMap<Entity, Long>();
188: }
|