001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
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 org.romaframework.module.admin;
018:
019: import java.util.List;
020:
021: import org.romaframework.aspect.persistence.PersistenceAspect;
022: import org.romaframework.aspect.persistence.QueryByFilter;
023: import org.romaframework.core.flow.ObjectContext;
024: import org.romaframework.module.admin.domain.Environment;
025:
026: public class EnvironmentHelper {
027:
028: private Environment current;
029: private static EnvironmentHelper instance = new EnvironmentHelper();
030:
031: public static final String MAIN_ENVIRONMENT_NAME = "main";
032:
033: protected EnvironmentHelper() {
034: }
035:
036: public synchronized void setCurrent(String iName) {
037: current = getEnvironment(iName);
038: }
039:
040: public synchronized Environment getCurrent() {
041: return current;
042: }
043:
044: public synchronized Environment getDefaultEnvironment() {
045: current = getEnvironment(EnvironmentHelper.MAIN_ENVIRONMENT_NAME);
046: return current;
047: }
048:
049: public synchronized Environment getEnvironment(String iName) {
050: if (current != null && iName != null
051: && iName.equals(current.getName()))
052: // ASKED CURRENT ENV: JUST RETURN IT
053: return current;
054:
055: QueryByFilter filter = new QueryByFilter(Environment.class);
056: filter.addItem("name", QueryByFilter.FIELD_EQUALS, iName);
057: List<Environment> result = ObjectContext.getInstance()
058: .getContextComponent(PersistenceAspect.class).query(
059: filter);
060:
061: if (result != null && result.size() > 0)
062: return result.get(0);
063:
064: return null;
065: }
066:
067: public synchronized Environment createEnvironment(String iName,
068: String iDescription) {
069: current = new Environment(iName, iDescription);
070: PersistenceAspect db = ObjectContext.getInstance()
071: .getContextComponent(PersistenceAspect.class);
072: db.createObject(current);
073: current = db.loadObject(current, "full",
074: PersistenceAspect.STRATEGY_DETACHING);
075: return current;
076: }
077:
078: /**
079: * Save permanently current environment.
080: *
081: * @param iName
082: * Parameter Name
083: * @param iValue
084: * Parameter Value
085: */
086: public void save() {
087: ObjectContext.getInstance().getContextComponent(
088: PersistenceAspect.class).updateObject(current);
089: }
090:
091: /**
092: * Update the environment to be shared by application.
093: *
094: * @param entity
095: */
096: public synchronized void setCurrentEnvironment(Environment entity) {
097: if (entity != null
098: && entity.getName().equals(current.getName()))
099: current = entity;
100: }
101:
102: public static EnvironmentHelper getInstance() {
103: return instance;
104: }
105: }
|