001: /*
002: * (C) Copyright 2000 - 2006 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package com.nabhinc.portal.model;
020:
021: import java.io.Serializable;
022: import java.util.HashMap;
023: import java.util.Properties;
024:
025: import javax.servlet.ServletException;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpSession;
028: import javax.xml.bind.annotation.XmlElement;
029: import javax.xml.bind.annotation.XmlTransient;
030:
031: import com.nabhinc.portal.core.ClientInfo;
032: import com.nabhinc.portal.core.LocaleUtil;
033: import com.nabhinc.portal.core.PortalConstants;
034: import com.nabhinc.portal.core.PortalUtil;
035:
036: /**
037: *
038: *
039: * @author Padmanabh Dabke
040: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
041: */
042: public class PortalApplicationView implements Serializable {
043:
044: private static final long serialVersionUID = -3755633573601263513L;
045: @XmlElement(name="page")
046: private PortalPageState[] pavPageStates = new PortalPageState[0];
047:
048: private transient HashMap<Object, PortalPageState> pavPageStateMap = new HashMap<Object, PortalPageState>();
049:
050: protected transient PortalApplication pavPortalApplication = null;
051:
052: public transient boolean isEditable = false;
053: public transient boolean isAdminable = false;
054: public transient boolean isPsiteCreatable = false;
055: public transient boolean needsPasswordCheck = false;
056: public transient boolean isDesktopMode = false;
057:
058: public PortalApplicationView() {
059:
060: }
061:
062: public PortalApplicationView(PortalPage[] pages,
063: PortalApplication pApp, ClientInfo clInfo,
064: HttpServletRequest request) throws ServletException {
065: Properties localeProps = clInfo.currentLocaleProperties;
066: String urlPrefix = request.getContextPath() + "/portal"
067: + pApp.getPath() + "/";
068:
069: if (pages != null) {
070: this .pavPageStates = new PortalPageState[pages.length];
071: int pageStateCount = 0;
072: if (pApp.isEditable(request))
073: this .isEditable = true;
074:
075: for (int i = 0; i < pages.length; i++) {
076: if (!pages[i].isViewable(request))
077: continue;
078: pavPageStates[pageStateCount] = new PortalPageState(
079: pages[i], LocaleUtil.getLocalizedValue(pages[i]
080: .getName(), localeProps), urlPrefix,
081: request);
082: if (isEditable
083: && pages[i].getEditPermission() != null
084: && (!pages[i].getEditPermission().isSatisfied(
085: request, pages[i])))
086: pavPageStates[pageStateCount].isEditable = false;
087: if (isEditable
088: && pages[i].getDeletePermission() != null
089: && (!pages[i].getDeletePermission()
090: .isSatisfied(request, pages[i])))
091: pavPageStates[pageStateCount].isDeletable = false;
092:
093: pageStateCount++;
094: }
095: // First check if we are in a session failover state
096: this .pavPortalApplication = pApp;
097: HttpSession session = request.getSession();
098: String displayMode = (String) session
099: .getAttribute(PortalConstants.PORTAL_APP_DISPLAY_MODE_ATTRIB
100: + pApp.getIdPrefix());
101: if (displayMode == null) {
102: if (PortalConfiguration.getInstance()
103: .getDefaultDisplayMode().equals(
104: PortalConstants.DISPLAY_MODE_NORMAL))
105: setNormalDisplayMode(request.getSession());
106: else
107: setDesktopDisplayMode(request.getSession());
108: } else {
109: initPageStates(displayMode, session);
110: }
111: computePageMap();
112: }
113:
114: if (pApp.isAdminable(request))
115: this .isAdminable = true;
116: if (pApp.isPsiteCreatable(request))
117: this .isPsiteCreatable = true;
118: if (pApp.isPasswordProtected()
119: && (!PortalUtil.isOwnerOrAdmin(request, pApp
120: .getOwnerName())))
121: this .needsPasswordCheck = true;
122: }
123:
124: public void computePageMap() {
125: pavPageStateMap.clear();
126: for (int i = 0; i < pavPageStates.length; i++) {
127: pavPageStateMap.put(pavPageStates[i].portalPage.getName(),
128: pavPageStates[i]);
129: pavPageStateMap.put(pavPageStates[i].portalPage.getIndex(),
130: pavPageStates[i]);
131: }
132: }
133:
134: public PortalPageState getPageState(Object pageNameOrIndex) {
135: return this .pavPageStateMap.get(pageNameOrIndex);
136: }
137:
138: @XmlTransient
139: public PortalApplication getPortalApplication() {
140: return this .pavPortalApplication;
141: }
142:
143: public PortalPageState[] getPageStates() {
144: return this .pavPageStates;
145: }
146:
147: public void reorderPage(int oldIndex, int newIndex) {
148: if (oldIndex < 0 || oldIndex >= pavPageStates.length)
149: return;
150: if (newIndex < 0 || newIndex >= pavPageStates.length)
151: return;
152: if (oldIndex == newIndex)
153: return;
154:
155: PortalPageState oldTab = pavPageStates[oldIndex];
156: if (oldIndex < newIndex) {
157: for (int i = oldIndex; i < newIndex; i++) {
158: pavPageStates[i] = pavPageStates[i + 1];
159: }
160: pavPageStates[newIndex] = oldTab;
161: } else {
162: for (int i = oldIndex; i > newIndex; i--) {
163: pavPageStates[i] = pavPageStates[i - 1];
164: }
165: pavPageStates[newIndex] = oldTab;
166:
167: }
168: this .pavPortalApplication.reorderPage(oldTab.portalPage,
169: oldIndex, newIndex);
170: }
171:
172: /**
173: * Delete an existing page if the index is within the range.
174: */
175: public void deletePage(int index) {
176:
177: if (index < 0 || index >= pavPageStates.length) {
178: return;
179: }
180: if (this .pavPageStates.length == 1)
181: return;
182: PortalPage deletedPage = this .pavPageStates[index].portalPage;
183: this .pavPortalApplication.deletePage(deletedPage);
184:
185: PortalPageState[] newPages = new PortalPageState[pavPageStates.length - 1];
186:
187: for (int i = 0; i < index; i++) {
188: newPages[i] = pavPageStates[i];
189: }
190:
191: int newLen = newPages.length;
192:
193: for (int i = index; i < newLen; i++) {
194: newPages[i] = pavPageStates[i + 1];
195: }
196:
197: pavPageStates = newPages;
198:
199: }
200:
201: public void addPage(PortalPage p, HttpServletRequest request,
202: ClientInfo clInfo) throws ServletException {
203: Properties localeProps = clInfo.currentLocaleProperties;
204: String urlPrefix = request.getContextPath() + "/portal"
205: + this .pavPortalApplication.getPath() + "/";
206: PortalPageState pState = new PortalPageState(p, LocaleUtil
207: .getLocalizedValue(p.getName(), localeProps),
208: urlPrefix, request);
209: if (this .isDesktopMode) {
210: pState.setDesktopPageMode(request.getSession());
211: } else {
212: pState.setNormalPageMode(request.getSession());
213: }
214: PortalPageState[] newPages = new PortalPageState[pavPageStates.length + 1];
215:
216: for (int i = 0; i < pavPageStates.length; i++) {
217: newPages[i] = pavPageStates[i];
218: }
219: newPages[pavPageStates.length] = pState;
220: this .pavPageStates = newPages;
221: computePageMap();
222: }
223:
224: public void setNormalDisplayMode(HttpSession session) {
225: this .isDesktopMode = false;
226: for (int i = 0; i < pavPageStates.length; i++) {
227: pavPageStates[i].setNormalPageMode(session);
228: }
229: session.setAttribute(
230: PortalConstants.PORTAL_APP_DISPLAY_MODE_ATTRIB
231: + this .pavPortalApplication.getIdPrefix(),
232: PortalConstants.DISPLAY_MODE_NORMAL);
233: }
234:
235: public void setDesktopDisplayMode(HttpSession session) {
236: this .isDesktopMode = true;
237: for (int i = 0; i < pavPageStates.length; i++) {
238: pavPageStates[i].setDesktopPageMode(session);
239: }
240: session.setAttribute(
241: PortalConstants.PORTAL_APP_DISPLAY_MODE_ATTRIB
242: + this .pavPortalApplication.getIdPrefix(),
243: PortalConstants.DISPLAY_MODE_DESKTOP);
244: }
245:
246: private void initPageStates(String displayMode, HttpSession session) {
247: this .isDesktopMode = displayMode
248: .equals(PortalConstants.DISPLAY_MODE_DESKTOP);
249: for (int i = 0; i < this .pavPageStates.length; i++) {
250: boolean restoredFromCache = this.pavPageStates[i]
251: .restoreNavigationState(session);
252: if (!restoredFromCache) {
253: if (this.isDesktopMode) {
254: pavPageStates[i].setDesktopPageMode(session);
255: } else {
256: pavPageStates[i].setNormalPageMode(session);
257: }
258: }
259: }
260: }
261:
262: }
|