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.view.domain.environment;
018:
019: import java.util.LinkedHashMap;
020: import java.util.Map;
021: import java.util.Map.Entry;
022:
023: import org.romaframework.aspect.core.annotation.CoreClass;
024: import org.romaframework.aspect.view.ViewCallback;
025: import org.romaframework.aspect.view.ViewConstants;
026: import org.romaframework.aspect.view.annotation.ViewClass;
027: import org.romaframework.aspect.view.annotation.ViewField;
028: import org.romaframework.module.admin.EnvironmentHelper;
029: import org.romaframework.module.admin.domain.Environment;
030: import org.romaframework.module.admin.domain.EnvironmentItem;
031: import org.romaframework.module.crud.CRUDInstance;
032:
033: @CoreClass(orderFields="entity sheets")
034: @ViewClass(entity=Environment.class,render=ViewConstants.RENDER_POPUP,layout=ViewConstants.LAYOUT_POPUP)
035: public class EnvironmentInstance extends CRUDInstance<Environment>
036: implements ViewCallback {
037: @ViewField(render=ViewConstants.RENDER_TAB,layout=ViewConstants.LAYOUT_BLOCK,selectionField="sheetSelection")
038: protected Map<String, EnvironmentInstanceSheet> sheets;
039:
040: protected EnvironmentInstanceSheet sheetSelection;
041:
042: private static final String DEFAULT_SHEET = "main";
043: public static final char SHEET_SEPARATOR = '.';
044:
045: public Map<String, EnvironmentInstanceSheet> getSheets() {
046: return sheets;
047: }
048:
049: @Override
050: public void onShow() {
051: sheets = new LinkedHashMap<String, EnvironmentInstanceSheet>();
052:
053: // CREATE DEFAULT MAIN SHEET
054: sheets.put(DEFAULT_SHEET, new EnvironmentInstanceSheet(""));
055:
056: int pos;
057: String sheetName;
058: EnvironmentInstanceSheet sheet;
059:
060: for (Map.Entry<String, EnvironmentItem> entry : entity
061: .getParameters().entrySet()) {
062: // EXTRACT SHEET NAME
063: pos = entry.getKey().indexOf(SHEET_SEPARATOR);
064: sheetName = pos > -1 ? entry.getKey().substring(0, pos)
065: : DEFAULT_SHEET;
066:
067: sheet = sheets.get(sheetName);
068: if (sheet == null) {
069: // NOT FOUND: CREATE IT
070: sheet = new EnvironmentInstanceSheet(sheetName);
071: sheets.put(sheetName, sheet);
072: }
073:
074: // COPY ITEM ON ITS SHEET
075: sheet.getValues().add(entry.getValue());
076: }
077: }
078:
079: @Override
080: public void onDispose() {
081: }
082:
083: /**
084: * Save all items to the map.
085: */
086: @Override
087: public void save() {
088: getEntity().clearParameters();
089:
090: EnvironmentInstanceSheet sheet;
091: for (Entry<String, EnvironmentInstanceSheet> entry : sheets
092: .entrySet()) {
093: sheet = entry.getValue();
094:
095: for (EnvironmentItem item : sheet.getValues()) {
096: if (item == null)
097: continue;
098:
099: getEntity().setParameter(item.getKey(), item);
100: }
101: }
102:
103: super .save();
104:
105: EnvironmentHelper.getInstance().setCurrentEnvironment(entity);
106: }
107:
108: public EnvironmentInstanceSheet getSheetSelection() {
109: return sheetSelection;
110: }
111:
112: public void setSheetSelection(
113: EnvironmentInstanceSheet sheetSelection) {
114: this.sheetSelection = sheetSelection;
115: }
116: }
|