001: /*
002: *
003: * Copyright 2007 Luca Molino (luca.molino@assetdata.it)
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * 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: package org.romaframework.module.users;
018:
019: import java.security.NoSuchAlgorithmException;
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.romaframework.aspect.persistence.PersistenceAspect;
026: import org.romaframework.aspect.persistence.QueryByExample;
027: import org.romaframework.aspect.persistence.QueryByText;
028: import org.romaframework.core.flow.ObjectContext;
029: import org.romaframework.module.admin.InfoHelper;
030: import org.romaframework.module.admin.domain.Info;
031: import org.romaframework.module.users.domain.BaseAccount;
032: import org.romaframework.module.users.domain.BaseProfile;
033:
034: /**
035: * @author l.molino
036: */
037: public class UsersHelper {
038:
039: protected List<BaseProfile> profiles;
040:
041: protected Map<BaseProfile, List<BaseAccount>> cache;
042:
043: private boolean cached = false;
044:
045: public static UsersHelper instance = new UsersHelper();
046:
047: public UsersHelper() {
048: profiles = new ArrayList<BaseProfile>();
049: cache = new HashMap<BaseProfile, List<BaseAccount>>();
050:
051: checkForProfiles();
052: }
053:
054: public void invalidateCache() {
055: cached = false;
056: }
057:
058: private void checkForProfiles() {
059: if (cached)
060: return;
061:
062: synchronized (profiles) {
063: if (!cached) {
064: QueryByText query = new QueryByText(BaseProfile.class,
065: null);
066: query.setStrategy(PersistenceAspect.STRATEGY_DETACHING);
067: profiles = ObjectContext.getInstance()
068: .getContextComponent(PersistenceAspect.class)
069: .query(query);
070: cached = true;
071: }
072: }
073: }
074:
075: public List<BaseProfile> getProfileList() {
076: checkForProfiles();
077:
078: return profiles;
079: }
080:
081: public BaseProfile[] getProfileArray() {
082: checkForProfiles();
083:
084: BaseProfile[] profileArray = new BaseProfile[profiles.size()];
085: if (profiles.size() > 0)
086: profiles.toArray(profileArray);
087:
088: return profileArray;
089: }
090:
091: public BaseProfile getProfile(String iName) {
092: checkForProfiles();
093:
094: synchronized (profiles) {
095: for (BaseProfile profile : profiles) {
096: if (profile.getName().equals(iName))
097: return profile;
098: }
099:
100: }
101:
102: return null;
103: }
104:
105: public BaseProfile setProfile(String iName) {
106: return setProfile(iName, BaseProfile.MODE_ALLOW_ALL_BUT);
107: }
108:
109: public BaseProfile setProfile(String iName, byte iMode) {
110: BaseProfile profile = new BaseProfile(iName, null, iMode, "");
111:
112: return setProfile(profile);
113: }
114:
115: public BaseProfile setProfile(BaseProfile iProfile) {
116: synchronized (profiles) {
117: if (!profiles.contains(iProfile)) {
118: iProfile = ObjectContext.getInstance()
119: .getContextComponent(PersistenceAspect.class)
120: .createObject(iProfile);
121: profiles.add(iProfile);
122: }
123:
124: }
125: return iProfile;
126: }
127:
128: public List<BaseAccount> getAccountList(String iProfileName) {
129: return getAccountList(getProfile(iProfileName));
130: }
131:
132: public List<BaseAccount> getAccountList(BaseProfile iProfile) {
133: List<BaseAccount> result;
134: synchronized (cache) {
135: result = cache.get(iProfile);
136: if (result != null)
137: return result;
138: }
139:
140: BaseAccount filter = new BaseAccount();
141: filter.setProfile(iProfile);
142: result = ObjectContext.getInstance().getContextComponent(
143: PersistenceAspect.class).query(
144: new QueryByExample(BaseAccount.class, filter));
145:
146: synchronized (cache) {
147: cache.put(iProfile, result);
148: }
149:
150: return result;
151: }
152:
153: public BaseAccount getAccount(String iProfileName, String iName) {
154: return getAccount(getProfile(iProfileName), iName);
155: }
156:
157: public BaseAccount getAccount(BaseProfile iProfile, String iName) {
158: List<BaseAccount> result = getAccountList(iProfile);
159:
160: if (result != null) {
161: // CATEGORY FOUND: SEARCH FOR INFO BY TEXT
162: for (BaseAccount i : result) {
163: if (i.getName().equals(iName))
164: // FOUND
165: return i;
166: }
167: }
168: return null;
169: }
170:
171: public BaseAccount setAccount(String iProfileName, String iName,
172: String iPassword) throws NoSuchAlgorithmException {
173: return setAccount(getProfile(iProfileName), iName, iPassword,
174: null);
175: }
176:
177: public BaseAccount setAccount(BaseProfile iProfile, String iName,
178: String iPassword) throws NoSuchAlgorithmException {
179: return setAccount(iProfile, iName, iPassword, null);
180: }
181:
182: public BaseAccount setAccount(String iProfileName, String iName,
183: String iPassword, Info iStatus)
184: throws NoSuchAlgorithmException {
185: if (iStatus == null)
186: iStatus = InfoHelper.getInstance().getInfo(
187: UsersInfoConstants.ACCOUNT_CATEGORY_NAME,
188: UsersInfoConstants.STATUS_ACTIVE);
189: return setAccount(getProfile(iProfileName), iName, iPassword,
190: iStatus);
191: }
192:
193: public BaseAccount setAccount(BaseProfile iProfile, String iName,
194: String iPassword, Info iStatus)
195: throws NoSuchAlgorithmException {
196: if (iStatus == null)
197: iStatus = InfoHelper.getInstance().getInfo(
198: UsersInfoConstants.ACCOUNT_CATEGORY_NAME,
199: UsersInfoConstants.STATUS_ACTIVE);
200: BaseAccount iAccount = new BaseAccount();
201: iAccount.setName(iName);
202: iAccount.setPassword(iPassword);
203: iAccount.setProfile(iProfile);
204: iAccount.setStatus(iStatus);
205: return storeAccount(iAccount);
206: }
207:
208: public BaseAccount setAccount(BaseAccount iAccount) {
209: return storeAccount(iAccount);
210: }
211:
212: private BaseAccount storeAccount(BaseAccount iAccount) {
213: List<BaseAccount> result;
214:
215: synchronized (cache) {
216: result = cache.get(iAccount.getProfile());
217: if (result != null) {
218: // CREATE NEW CATEGORY
219: setProfile(iAccount.getProfile().getName());
220:
221: result = cache.get(iAccount.getProfile());
222: result.add(iAccount);
223: }
224: }
225: return ObjectContext.getInstance().getContextComponent(
226: PersistenceAspect.class).createObject(iAccount);
227: }
228:
229: public static UsersHelper getInstance() {
230: return instance;
231: }
232:
233: }
|