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.users.view.domain.baseprofile;
018:
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.Collections;
022: import java.util.Iterator;
023: import java.util.Map;
024: import java.util.TreeMap;
025: import java.util.Map.Entry;
026:
027: import org.romaframework.aspect.core.annotation.AnnotationConstants;
028: import org.romaframework.aspect.core.annotation.CoreClass;
029: import org.romaframework.aspect.reporting.annotation.ReportingField;
030: import org.romaframework.aspect.session.SessionAspect;
031: import org.romaframework.aspect.session.SessionInfo;
032: import org.romaframework.aspect.view.ViewCallback;
033: import org.romaframework.aspect.view.annotation.ViewClass;
034: import org.romaframework.aspect.view.annotation.ViewField;
035: import org.romaframework.aspect.view.feature.ViewFieldFeatures;
036: import org.romaframework.core.flow.ObjectContext;
037: import org.romaframework.core.schema.SchemaAction;
038: import org.romaframework.core.schema.SchemaClass;
039: import org.romaframework.core.schema.SchemaField;
040: import org.romaframework.core.schema.SchemaManager;
041: import org.romaframework.core.validation.CustomValidation;
042: import org.romaframework.core.validation.ValidationException;
043: import org.romaframework.module.crud.CRUDInstance;
044: import org.romaframework.module.users.domain.BaseFunction;
045: import org.romaframework.module.users.domain.BaseProfile;
046:
047: @CoreClass(orderFields="entity availableClassNames availableMembers functions")
048: @ViewClass(entity=BaseProfile.class)
049: public class BaseProfileInstance extends CRUDInstance<BaseProfile>
050: implements CustomValidation, ViewCallback {
051: private static final String DEF_HOME_PAGE = "HomePage";
052:
053: private static final String CLASS_ELEMENT = "< Class >";
054:
055: @ViewField(selectionField="selectedAvailableClassName",enabled=AnnotationConstants.FALSE)
056: @ReportingField(visible=AnnotationConstants.FALSE)
057: protected ArrayList<String> availableClassNames;
058:
059: @ViewField(selectionField="selectedAvailableMember",enabled=AnnotationConstants.FALSE)
060: protected ArrayList<String> availableMembers;
061:
062: @ViewField(visible=AnnotationConstants.FALSE)
063: protected String selectedAvailableMember;
064:
065: protected SchemaClass selectedAvailableClass;
066:
067: @ViewField(visible=AnnotationConstants.FALSE)
068: private BaseFunction selectedFunction;
069:
070: @ViewField(label="Mode",render="select",selectionField="entity.mode",selectionMode=ViewFieldFeatures.SELECTION_MODE_INDEX)
071: @ReportingField(visible=AnnotationConstants.FALSE)
072: public String[] getModes() {
073: return BaseProfileHelper.MODES;
074: }
075:
076: @ViewField(selectionField="selectedFunction",enabled=AnnotationConstants.TRUE)
077: public Map<String, BaseFunction> getFunctions() {
078: return entity.getFunctions();
079: }
080:
081: @Override
082: public void onCreate() {
083: getEntity().setMode(BaseProfile.MODE_ALLOW_ALL_BUT);
084: getEntity().setFunctions(new TreeMap<String, BaseFunction>());
085: getEntity().setHomePage(DEF_HOME_PAGE);
086: }
087:
088: @Override
089: public void save() {
090: // CHECK FOR FUNCTION UPDATING
091: String key;
092:
093: boolean found = false;
094: while (!found) {
095: found = true;
096:
097: for (Entry<String, BaseFunction> fun : getEntity()
098: .getFunctions().entrySet()) {
099: key = fun.getKey();
100: if (!getEntity().getFunctions().get(key).getName()
101: .equals(key)) {
102: // FOUND INCHOERENCE: FIX IT
103: getEntity().getFunctions().put(
104: fun.getValue().getName(), fun.getValue());
105: getEntity().getFunctions().remove(key);
106:
107: found = false;
108: break;
109: }
110: }
111: }
112:
113: super .save();
114:
115: // UPDATE ALL IN-MEMORY USERS ACCOUNT
116: Collection<SessionInfo> activeSessions = ObjectContext
117: .getInstance().getComponent(SessionAspect.class)
118: .getSessionInfos();
119:
120: synchronized (activeSessions) {
121: for (SessionInfo s : activeSessions) {
122: if (s.getAccount() != null
123: && s.getAccount().getProfile().equals(
124: getEntity()))
125: // REPLACE IT WITH CURRENT ENTITY
126: s.getAccount().setProfile(getEntity());
127: }
128: }
129: }
130:
131: @Override
132: public void onShow() {
133: SchemaManager schemaMgr = ObjectContext.getInstance()
134: .getComponent(SchemaManager.class);
135: Collection<SchemaClass> infos = schemaMgr.getAllClassesInfo();
136:
137: availableClassNames = new ArrayList<String>();
138: for (SchemaClass cls : infos)
139: availableClassNames.add(cls.getName());
140:
141: Collections.sort(availableClassNames);
142: }
143:
144: public void setSelectedAvailableClassName(String iSelection) {
145: availableMembers = new ArrayList<String>();
146: selectedAvailableClass = ObjectContext.getInstance()
147: .getComponent(SchemaManager.class).getClassInfo(
148: iSelection);
149: availableMembers.add(CLASS_ELEMENT);
150:
151: for (Iterator<SchemaAction> it = selectedAvailableClass
152: .getActionIterator(); it.hasNext();)
153: availableMembers.add(it.next().getName() + "()");
154:
155: for (Iterator<SchemaField> it = selectedAvailableClass
156: .getFieldIterator(); it.hasNext();)
157: availableMembers.add(it.next().getName());
158:
159: ObjectContext.getInstance().refresh(this , "availableMembers");
160: }
161:
162: public void add() {
163: if (availableMembers == null)
164: return;
165:
166: String funcName = selectedAvailableClass.getName();
167: if (!selectedAvailableMember.equals(CLASS_ELEMENT)) {
168: funcName += "." + selectedAvailableMember;
169: if (funcName.endsWith("()"))
170: funcName = funcName.substring(0, funcName.length() - 2);
171: }
172:
173: boolean allowFunction = getEntity().getMode().byteValue() == BaseProfile.MODE_DENY_ALL_BUT;
174:
175: getEntity().addFunction(funcName, allowFunction);
176:
177: ObjectContext.getInstance().refresh(this , "functions");
178: }
179:
180: @ViewField(visible=AnnotationConstants.FALSE)
181: public String getSelectedAvailableClassName() {
182: if (selectedAvailableClass != null)
183: return selectedAvailableClass.getName();
184: return null;
185: }
186:
187: public ArrayList<String> getAvailableMembers() {
188: return availableMembers;
189: }
190:
191: public void setAvailableMembers(ArrayList<String> availableMembers) {
192: this .availableMembers = availableMembers;
193: }
194:
195: public String getSelectedAvailableMember() {
196: return selectedAvailableMember;
197: }
198:
199: public void setSelectedAvailableMember(
200: String selectedAvailableMember) {
201: this .selectedAvailableMember = selectedAvailableMember;
202: }
203:
204: public void validate() throws ValidationException {
205: }
206:
207: public ArrayList<String> getAvailableClassNames() {
208: return availableClassNames;
209: }
210:
211: public void setAvailableClassNames(
212: ArrayList<String> availableClassNames) {
213: this .availableClassNames = availableClassNames;
214: }
215:
216: public BaseFunction getSelectedFunction() {
217: return selectedFunction;
218: }
219:
220: public void setSelectedFunction(BaseFunction selectedFunction) {
221: this.selectedFunction = selectedFunction;
222: }
223:
224: }
|