001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/presentation/tool/src/java/org/theospi/portfolio/presentation/tool/DecoratedPage.java $
003: * $Id:DecoratedPage.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.theospi.portfolio.presentation.tool;
021:
022: import java.io.ByteArrayInputStream;
023: import java.io.ByteArrayOutputStream;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.util.Collections;
027:
028: import javax.faces.context.ExternalContext;
029: import javax.faces.context.FacesContext;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.sakaiproject.metaobj.shared.model.Id;
034: import org.sakaiproject.tool.api.ToolSession;
035: import org.sakaiproject.tool.cover.SessionManager;
036: import org.theospi.portfolio.presentation.PresentationLayoutHelper;
037: import org.theospi.portfolio.presentation.model.PresentationLayout;
038: import org.theospi.portfolio.presentation.model.PresentationPage;
039: import org.theospi.portfolio.shared.model.Node;
040: import org.theospi.portfolio.style.StyleHelper;
041: import org.theospi.portfolio.style.model.Style;
042:
043: /**
044: * Created by IntelliJ IDEA.
045: * User: John Ellis
046: * Date: Jan 1, 2006
047: * Time: 7:32:11 PM
048: * To change this template use File | Settings | File Templates.
049: */
050: public class DecoratedPage implements Comparable {
051:
052: protected final transient Log logger = LogFactory
053: .getLog(getClass());
054:
055: private FreeFormTool parent;
056: private PresentationPage base;
057: private RegionMap regionMap;
058: private boolean selected;
059: private DecoratedLayout selectedLayout = null;
060: private String layoutName;
061: private String lastModified;
062: private String expandedInformationSection = "true";
063: private boolean newPage = false;
064:
065: public DecoratedPage(PresentationPage base, FreeFormTool parent) {
066: this .base = base;
067: this .parent = parent;
068: initLayout();
069: }
070:
071: protected void initLayout() {
072: if (base.getLayout() != null) {
073: setSelectedLayout(new DecoratedLayout(parent, base
074: .getLayout()));
075: }
076: }
077:
078: public String getStyleName() {
079: ToolSession session = SessionManager.getCurrentToolSession();
080: if (session.getAttribute(StyleHelper.CURRENT_STYLE) != null) {
081: Style style = (Style) session
082: .getAttribute(StyleHelper.CURRENT_STYLE);
083: base.setStyle(style);
084: } else if (session.getAttribute(StyleHelper.UNSELECTED_STYLE) != null) {
085: base.setStyle(null);
086: session.removeAttribute(StyleHelper.UNSELECTED_STYLE);
087: return "";
088: }
089:
090: if (base.getStyle() != null)
091: return base.getStyle().getName();
092: return "";
093: }
094:
095: public boolean isRenderLayoutName() {
096: getLayoutName();
097: return true;
098: }
099:
100: public String getLayoutName() {
101: ToolSession session = SessionManager.getCurrentToolSession();
102: if (session
103: .getAttribute(PresentationLayoutHelper.CURRENT_LAYOUT) != null) {
104: PresentationLayout layout = (PresentationLayout) session
105: .getAttribute(PresentationLayoutHelper.CURRENT_LAYOUT);
106: //base.setLayout(layout);
107: clearRegionsIfDirtyLayout(layout);
108: setSelectedLayout(new DecoratedLayout(getParent(), layout));
109: session
110: .removeAttribute(PresentationLayoutHelper.CURRENT_LAYOUT);
111: } else if (session
112: .getAttribute(PresentationLayoutHelper.UNSELECTED_LAYOUT) != null) {
113: //base.setLayout(null);
114: clearRegions();
115: setSelectedLayout(new DecoratedLayout(getParent(), null));
116: session
117: .removeAttribute(PresentationLayoutHelper.UNSELECTED_LAYOUT);
118: setSelectedLayoutId(null);
119: return null;
120: }
121:
122: if (getSelectedLayout() != null
123: && getSelectedLayout().getBase() != null)
124: return getSelectedLayout().getBase().getName();
125: //return layoutName;
126: setSelectedLayoutId(null);
127: return null;
128: }
129:
130: protected boolean isLayoutDirty(PresentationLayout layout) {
131: if (getSelectedLayout() != null
132: && getSelectedLayout().getBase() != null)
133: return !getSelectedLayout().getBase().equals(layout);
134:
135: return false;
136: }
137:
138: protected void clearRegionsIfDirtyLayout(PresentationLayout layout) {
139: if (isLayoutDirty(layout)) {
140: clearRegions();
141: }
142: }
143:
144: protected void clearRegions() {
145: getBase().getRegions().clear();
146: regionMap = null;
147: }
148:
149: public void setLayoutName(String name) {
150: this .layoutName = name;
151: }
152:
153: public PresentationPage getBase() {
154: return base;
155: }
156:
157: public void setBase(PresentationPage base) {
158: this .base = base;
159: }
160:
161: /**
162: * Any time this is called the calling method MUST close the input stream!!
163: * This has the potential of causing memory leaks if the calling method does not close the stream
164: * @return InputStream
165: */
166: public InputStream getXmlFile() {
167: InputStream inputStream = null;
168: if (getSelectedLayout().getBase() != null) {
169: Node node = getParent().getPresentationManager().getNode(
170: getSelectedLayout().getBase().getXhtmlFileId(),
171: getSelectedLayout().getBase());
172: inputStream = node.getInputStream();
173:
174: // we want to read the entire file into memory so wo can close the inputStream
175: // and thus release the database connection / file connection
176:
177: ByteArrayOutputStream bytesOS = new ByteArrayOutputStream();
178: int buffersize = 1024, s;
179: byte[] buffer = new byte[buffersize];
180: try {
181: while ((s = inputStream.read(buffer)) != -1) {
182: bytesOS.write(buffer, 0, s);
183: }
184: inputStream.close();
185: inputStream = new ByteArrayInputStream(bytesOS
186: .toByteArray());
187: } catch (IOException ioe) {
188: logger.error(ioe);
189: inputStream = null;
190: }
191: }
192:
193: return inputStream;
194: }
195:
196: public boolean isXmlFileNotNull() {
197: InputStream inputStream = getXmlFile();
198: boolean isNotNull = inputStream != null;
199: if (isNotNull) {
200: try {
201: inputStream.close();
202: } catch (IOException ioe) {
203: logger.equals(ioe);
204: }
205: }
206: return isNotNull;
207: }
208:
209: public String getXmlFileId() {
210: return getSelectedLayout().getBase().getId().getValue()
211: + getSelectedLayout().getBase().getModified()
212: .toString();
213: }
214:
215: public RegionMap getRegionMap() {
216: if (regionMap == null) {
217: regionMap = new RegionMap(getBase());
218: }
219: return regionMap;
220: }
221:
222: public void setRegionMap(RegionMap regionMap) {
223: this .regionMap = regionMap;
224: }
225:
226: public FreeFormTool getParent() {
227: return parent;
228: }
229:
230: public void setParent(FreeFormTool parent) {
231: this .parent = parent;
232: }
233:
234: public String processActionArrange() {
235: getParent().setCurrentPage(this );
236: initLayout();
237: return "arrange";
238: }
239:
240: public String processActionEdit() {
241: getParent().setCurrentPage(this );
242: initLayout();
243: return "edit";
244: }
245:
246: public String processActionConfirmDelete() {
247: getParent().setCurrentPage(this );
248: return "confirm";
249: }
250:
251: public String processActionDelete() {
252: getParent().deletePage(this );
253: getParent().reorderPages();
254: return "main";
255: }
256:
257: public String processActionSelectStyle() {
258: ExternalContext context = FacesContext.getCurrentInstance()
259: .getExternalContext();
260: ToolSession session = SessionManager.getCurrentToolSession();
261: session.removeAttribute(StyleHelper.CURRENT_STYLE);
262: session.removeAttribute(StyleHelper.CURRENT_STYLE_ID);
263:
264: session.setAttribute(StyleHelper.STYLE_SELECTABLE, "true");
265: if (base.getStyle() != null)
266: session.setAttribute(StyleHelper.CURRENT_STYLE_ID, base
267: .getStyle().getId().getValue());
268:
269: try {
270: context.redirect("osp.style.helper/listStyle");
271: } catch (IOException e) {
272: throw new RuntimeException("Failed to redirect to helper",
273: e);
274: }
275: return null;
276: }
277:
278: public String processActionSelectLayout() {
279: ExternalContext context = FacesContext.getCurrentInstance()
280: .getExternalContext();
281: ToolSession session = SessionManager.getCurrentToolSession();
282: session
283: .removeAttribute(PresentationLayoutHelper.CURRENT_LAYOUT);
284: session
285: .removeAttribute(PresentationLayoutHelper.CURRENT_LAYOUT_ID);
286:
287: session.setAttribute(
288: PresentationLayoutHelper.LAYOUT_SELECTABLE, "true");
289: if (getSelectedLayout() != null
290: && getSelectedLayout().getBase() != null)
291: session.setAttribute(
292: PresentationLayoutHelper.CURRENT_LAYOUT_ID,
293: getSelectedLayout().getBase().getId().getValue());
294:
295: try {
296: context.redirect("osp.presLayout.helper/listLayout");
297: } catch (IOException e) {
298: throw new RuntimeException("Failed to redirect to helper",
299: e);
300: }
301: return null;
302: }
303:
304: public boolean isSelected() {
305: return selected;
306: }
307:
308: public void setSelected(boolean selected) {
309: this .selected = selected;
310: }
311:
312: public DecoratedLayout getSelectedLayout() {
313: return selectedLayout;
314: }
315:
316: public void setSelectedLayout(DecoratedLayout selectedLayout) {
317: this .selectedLayout = selectedLayout;
318: }
319:
320: /**
321: * Sets the layout to null as well
322: * @param layoutId
323: */
324: public void setSelectedLayoutId(String layoutId) {
325:
326: Id id = getParent().getIdManager().getId(layoutId);
327: PresentationLayout layout = getParent()
328: .getPresentationManager().getPresentationLayout(id);
329: setSelectedLayout(new DecoratedLayout(getParent(), layout));
330: }
331:
332: public String getSelectedLayoutId() {
333: if (getSelectedLayout() != null
334: && getSelectedLayout().getBase() != null) {
335: return getSelectedLayout().getBase().getId().getValue();
336: }
337: return null;
338: }
339:
340: public boolean islayoutSelected() {
341: return (getSelectedLayout() != null && getSelectedLayout()
342: .getBase() != null);
343: }
344:
345: public boolean isLayoutPreviewImage() {
346: if (islayoutSelected()
347: && (getSelectedLayout().getPreviewImage() != null)) {
348: return true;
349: }
350: return false;
351: }
352:
353: public int compareTo(Object o) {
354: DecoratedPage other = (DecoratedPage) o;
355: return getBase().compareTo(other.getBase());
356: }
357:
358: public String pagePropertiesSaved() {
359: getBase().setLayout(getSelectedLayout().getBase());
360: //Make sure the page list is being refreshed after a save.
361: getParent().setPageList(null);
362: return "main";
363: }
364:
365: public boolean getHasLayout() {
366: return getBase().getLayout() != null;
367: }
368:
369: public String moveUp() {
370: if (getBase().getSequence() != 0) {
371: Collections.swap(getParent().getPresentation().getPages(),
372: getBase().getSequence(),
373: getBase().getSequence() - 1);
374: getParent().reorderPages();
375: }
376: return null;
377: }
378:
379: public String moveDown() {
380: if (getBase().getSequence() < getParent().getPresentation()
381: .getPages().size() - 1) {
382: Collections.swap(getParent().getPresentation().getPages(),
383: getBase().getSequence(),
384: getBase().getSequence() + 1);
385: getParent().reorderPages();
386: }
387: return null;
388: }
389:
390: public boolean isLast() {
391: return getBase().getSequence() >= getParent().getPresentation()
392: .getPages().size() - 1;
393: }
394:
395: public String getLastModified() {
396: return lastModified;
397: }
398:
399: public void setLastModified(String lastModified) {
400: this .lastModified = lastModified;
401: }
402:
403: public String getExpandedInformationSection() {
404: if (this .getBase().getTitle() == null
405: || this .getBase().getTitle().equals("")) {
406: return "true";
407: }
408: return "false";
409: }
410:
411: public boolean isNewPage() {
412: return newPage;
413: }
414:
415: public void setNewPage(boolean newPage) {
416: this.newPage = newPage;
417: }
418:
419: }
|