/*
*
* Copyright 2007 Luca Molino (luca.molino@assetdata.it)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.romaframework.module.users;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.romaframework.aspect.persistence.PersistenceAspect;
import org.romaframework.aspect.persistence.QueryByExample;
import org.romaframework.aspect.persistence.QueryByText;
import org.romaframework.core.flow.ObjectContext;
import org.romaframework.module.admin.InfoHelper;
import org.romaframework.module.admin.domain.Info;
import org.romaframework.module.users.domain.BaseAccount;
import org.romaframework.module.users.domain.BaseProfile;
/**
* @author l.molino
*/
public class UsersHelper {
protected List<BaseProfile> profiles;
protected Map<BaseProfile, List<BaseAccount>> cache;
private boolean cached = false;
public static UsersHelper instance = new UsersHelper();
public UsersHelper() {
profiles = new ArrayList<BaseProfile>();
cache = new HashMap<BaseProfile, List<BaseAccount>>();
checkForProfiles();
}
public void invalidateCache() {
cached = false;
}
private void checkForProfiles() {
if (cached)
return;
synchronized (profiles) {
if (!cached) {
QueryByText query = new QueryByText(BaseProfile.class, null);
query.setStrategy(PersistenceAspect.STRATEGY_DETACHING);
profiles = ObjectContext.getInstance().getContextComponent(PersistenceAspect.class).query(query);
cached = true;
}
}
}
public List<BaseProfile> getProfileList() {
checkForProfiles();
return profiles;
}
public BaseProfile[] getProfileArray() {
checkForProfiles();
BaseProfile[] profileArray = new BaseProfile[profiles.size()];
if (profiles.size() > 0)
profiles.toArray(profileArray);
return profileArray;
}
public BaseProfile getProfile(String iName) {
checkForProfiles();
synchronized (profiles) {
for (BaseProfile profile : profiles) {
if (profile.getName().equals(iName))
return profile;
}
}
return null;
}
public BaseProfile setProfile(String iName) {
return setProfile(iName, BaseProfile.MODE_ALLOW_ALL_BUT);
}
public BaseProfile setProfile(String iName, byte iMode) {
BaseProfile profile = new BaseProfile(iName, null, iMode, "");
return setProfile(profile);
}
public BaseProfile setProfile(BaseProfile iProfile) {
synchronized (profiles) {
if (!profiles.contains(iProfile)) {
iProfile = ObjectContext.getInstance().getContextComponent(PersistenceAspect.class).createObject(iProfile);
profiles.add(iProfile);
}
}
return iProfile;
}
public List<BaseAccount> getAccountList(String iProfileName) {
return getAccountList(getProfile(iProfileName));
}
public List<BaseAccount> getAccountList(BaseProfile iProfile) {
List<BaseAccount> result;
synchronized (cache) {
result = cache.get(iProfile);
if (result != null)
return result;
}
BaseAccount filter = new BaseAccount();
filter.setProfile(iProfile);
result = ObjectContext.getInstance().getContextComponent(PersistenceAspect.class).query(new QueryByExample(BaseAccount.class, filter));
synchronized (cache) {
cache.put(iProfile, result);
}
return result;
}
public BaseAccount getAccount(String iProfileName, String iName) {
return getAccount(getProfile(iProfileName), iName);
}
public BaseAccount getAccount(BaseProfile iProfile, String iName) {
List<BaseAccount> result = getAccountList(iProfile);
if (result != null) {
// CATEGORY FOUND: SEARCH FOR INFO BY TEXT
for (BaseAccount i : result) {
if (i.getName().equals(iName))
// FOUND
return i;
}
}
return null;
}
public BaseAccount setAccount(String iProfileName, String iName, String iPassword) throws NoSuchAlgorithmException {
return setAccount(getProfile(iProfileName), iName, iPassword, null);
}
public BaseAccount setAccount(BaseProfile iProfile, String iName, String iPassword) throws NoSuchAlgorithmException {
return setAccount(iProfile, iName, iPassword, null);
}
public BaseAccount setAccount(String iProfileName, String iName, String iPassword, Info iStatus) throws NoSuchAlgorithmException {
if (iStatus == null)
iStatus = InfoHelper.getInstance().getInfo(UsersInfoConstants.ACCOUNT_CATEGORY_NAME, UsersInfoConstants.STATUS_ACTIVE);
return setAccount(getProfile(iProfileName), iName, iPassword, iStatus);
}
public BaseAccount setAccount(BaseProfile iProfile, String iName, String iPassword, Info iStatus) throws NoSuchAlgorithmException {
if (iStatus == null)
iStatus = InfoHelper.getInstance().getInfo(UsersInfoConstants.ACCOUNT_CATEGORY_NAME, UsersInfoConstants.STATUS_ACTIVE);
BaseAccount iAccount = new BaseAccount();
iAccount.setName(iName);
iAccount.setPassword(iPassword);
iAccount.setProfile(iProfile);
iAccount.setStatus(iStatus);
return storeAccount(iAccount);
}
public BaseAccount setAccount(BaseAccount iAccount) {
return storeAccount(iAccount);
}
private BaseAccount storeAccount(BaseAccount iAccount) {
List<BaseAccount> result;
synchronized (cache) {
result = cache.get(iAccount.getProfile());
if (result != null) {
// CREATE NEW CATEGORY
setProfile(iAccount.getProfile().getName());
result = cache.get(iAccount.getProfile());
result.add(iAccount);
}
}
return ObjectContext.getInstance().getContextComponent(PersistenceAspect.class).createObject(iAccount);
}
public static UsersHelper getInstance() {
return instance;
}
}
|