001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/wizard/tool/src/java/org/theospi/portfolio/wizard/tool/DecoratedCategory.java $
003: * $Id:DecoratedCategory.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.wizard.tool;
021:
022: import java.util.ArrayList;
023: import java.util.Collections;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import org.sakaiproject.tool.api.ToolSession;
028: import org.sakaiproject.tool.cover.SessionManager;
029: import org.theospi.portfolio.matrix.WizardPageHelper;
030: import org.theospi.portfolio.matrix.model.WizardPageDefinition;
031: import org.theospi.portfolio.wizard.WizardFunctionConstants;
032: import org.theospi.portfolio.wizard.model.Wizard;
033: import org.theospi.portfolio.wizard.model.WizardCategory;
034: import org.theospi.portfolio.wizard.model.WizardPageSequence;
035:
036: /**
037: * Created by IntelliJ IDEA.
038: * User: John Ellis
039: * Date: Jan 13, 2006
040: * Time: 11:44:25 AM
041: * To change this template use File | Settings | File Templates.
042: */
043: public class DecoratedCategory extends DecoratedCategoryChild {
044:
045: public static final String NEW_PAGE = "org.theospi.portfolio.wizard.tool.DecoratedCategory.newPage";
046:
047: private WizardCategory base;
048: private List categoryPageList;
049: private boolean selected;
050:
051: private DecoratedCategory parentCategory = null;
052:
053: public DecoratedCategory(WizardCategory base, WizardTool tool) {
054: super (tool, 0);
055: this .base = base;
056: }
057:
058: public DecoratedCategory(DecoratedCategory parentCategory,
059: WizardCategory base, WizardTool tool, int indent) {
060: super (tool, indent);
061: this .parentCategory = parentCategory;
062: this .base = base;
063: }
064:
065: public WizardCategory getBase() {
066: return base;
067: }
068:
069: public void setBase(WizardCategory base) {
070: this .base = base;
071: }
072:
073: /**
074: * This returns the concat description string. This is currently acceptable
075: * because the wizard description is not html
076: * @return String
077: */
078: public String getDescription() {
079: String desc = base.getDescription();
080: if (desc != null && desc.length() > 100)
081: return desc.substring(0, 100) + "...";
082: return desc;
083: }
084:
085: public String processActionNewPage() {
086: if (getBase().getWizard().isPublished())
087: return null;
088: WizardPageSequence wizardPage = new WizardPageSequence(
089: new WizardPageDefinition());
090: wizardPage.getWizardPageDefinition().setSiteId(
091: getParent().getWorksite().getId());
092: wizardPage.getWizardPageDefinition().setNewId(
093: getParent().getIdManager().createId());
094: wizardPage.setCategory(getBase());
095:
096: getParent().getCurrent().getRootCategory().setCategoryPageList(
097: null);
098:
099: return new DecoratedWizardPage(this , wizardPage, getParent(),
100: getIndent() + 1).processActionEdit(true);
101: }
102:
103: protected void resequencePages() {
104: int index = 0;
105: for (Iterator i = getBase().getChildPages().iterator(); i
106: .hasNext();) {
107: WizardPageSequence page = (WizardPageSequence) i.next();
108: page.setSequence(index);
109: index++;
110: }
111: getParent().getCurrent().getRootCategory().setCategoryPageList(
112: null);
113: }
114:
115: protected void resequenceCategories() {
116: int index = 0;
117: for (Iterator i = getBase().getChildCategories().iterator(); i
118: .hasNext();) {
119: WizardCategory category = (WizardCategory) i.next();
120: category.setSequence(index);
121: index++;
122: }
123: getParent().getCurrent().getRootCategory().setCategoryPageList(
124: null);
125: }
126:
127: public List getCategoryPageList() {
128: if (categoryPageList == null) {
129: ToolSession session = SessionManager
130: .getCurrentToolSession();
131: if (session.getAttribute(NEW_PAGE) != null
132: && session.getAttribute(WizardPageHelper.CANCELED) == null) {
133: WizardPageSequence page = (WizardPageSequence) session
134: .getAttribute(NEW_PAGE);
135: page.setSequence(page.getCategory().getChildPages()
136: .size());
137: page.getCategory().getChildPages().add(page);
138: session.removeAttribute(NEW_PAGE);
139: } else if (session.getAttribute(WizardPageHelper.CANCELED) != null) {
140: session.removeAttribute(NEW_PAGE);
141: session.removeAttribute(WizardPageHelper.CANCELED);
142: }
143:
144: categoryPageList = new ArrayList();
145: addCategoriesPages(categoryPageList);
146: }
147: return categoryPageList;
148: }
149:
150: protected List addCategoriesPages(List categoryPages) {
151: if (getParent().getCurrent().getBase().getType().equals(
152: WizardFunctionConstants.WIZARD_TYPE_HIERARCHICAL)) {
153: if (parentCategory == null)
154: categoryPages.add(getParent().getCurrent());
155: for (Iterator i = getBase().getChildCategories().iterator(); i
156: .hasNext();) {
157: WizardCategory category = (WizardCategory) i.next();
158: DecoratedCategory decoratedCategory = new DecoratedCategory(
159: this , category, getParent(), getIndent() + 1);
160: categoryPages.add(decoratedCategory);
161: decoratedCategory.addCategoriesPages(categoryPages);
162: }
163: }
164:
165: for (Iterator i = getBase().getChildPages().iterator(); i
166: .hasNext();) {
167: WizardPageSequence page = (WizardPageSequence) i.next();
168: categoryPages.add(new DecoratedWizardPage(this , page,
169: getParent(), getIndent() + 1));
170: }
171: return categoryPages;
172: }
173:
174: public void setCategoryPageList(List categoryPageList) {
175: this .categoryPageList = categoryPageList;
176: }
177:
178: public DecoratedCategory getParentCategory() {
179: return parentCategory;
180: }
181:
182: public void setParentCategory(DecoratedCategory parentCategory) {
183: this .parentCategory = parentCategory;
184: }
185:
186: public String processActionSave() {
187: List parentCategories = getParentCategory().getBase()
188: .getChildCategories();
189:
190: if (!parentCategories.contains(getBase())) {
191: parentCategories.add(getBase());
192: getBase().setParentCategory(getParentCategory().getBase());
193: }
194:
195: getParentCategory().resequenceCategories();
196:
197: return "editWizardPages";
198: }
199:
200: public String processActionCancel() {
201: getParent().setCurrentCategory(null);
202: return "editWizardPages";
203: }
204:
205: public String getTitle() {
206: return getBase().getTitle();
207: }
208:
209: public String processActionEdit() {
210: getParent().setCurrentCategory(this );
211: return "editWizardCategory";
212: }
213:
214: public String processActionDelete() {
215: if (getBase().getWizard().isPublished())
216: return null;
217: DecoratedCategory parentCategory = getParentCategory();
218: parentCategory.getBase().getChildCategories().remove(getBase());
219: parentCategory.resequenceCategories();
220: if (getBase().getId() != null)
221: getParent().getDeletedItems().add(getBase());
222: return "continue";
223: }
224:
225: public String processActionConfirmDelete() {
226: getParent().setCurrentCategory(this );
227: return "confirmDeleteCategory";
228: }
229:
230: public String moveUp() {
231: if (getBase().getSequence() != 0) {
232: Collections.swap(getBase().getParentCategory()
233: .getChildCategories(), getBase().getSequence(),
234: getBase().getSequence() - 1);
235: getParentCategory().resequenceCategories();
236: }
237: return null;
238: }
239:
240: public String moveDown() {
241: if (getBase().getSequence() < getBase().getParentCategory()
242: .getChildCategories().size() - 1) {
243: Collections.swap(getBase().getParentCategory()
244: .getChildCategories(), getBase().getSequence(),
245: getBase().getSequence() + 1);
246: getParentCategory().resequenceCategories();
247: }
248: return null;
249: }
250:
251: public boolean isSelected() {
252: return selected;
253: }
254:
255: public void setSelected(boolean selected) {
256: this .selected = selected;
257: }
258:
259: public boolean isFirst() {
260: return getBase().getSequence() == 0;
261: }
262:
263: public boolean isLast() {
264: return getBase().getSequence() >= getBase().getParentCategory()
265: .getChildCategories().size() - 1;
266: }
267:
268: public String processActionNewCategory() {
269: if (getBase().getWizard().isPublished())
270: return null;
271: WizardCategory wizardCategory = new WizardCategory(getBase()
272: .getWizard());
273: getParent().setCurrentCategory(
274: new DecoratedCategory(this , wizardCategory,
275: getParent(), getIndent() + 1));
276: return "editWizardCategory";
277: }
278:
279: public boolean isCategory() {
280: return true;
281: }
282:
283: public boolean isContainerForMove() {
284: if (getParent().getMoveCategoryChild() == null) {
285: return false;
286: }
287: DecoratedCategoryChild child = getParent()
288: .getMoveCategoryChild();
289: if (child instanceof DecoratedCategory) {
290: DecoratedCategory category = (DecoratedCategory) child;
291: return category.getParentCategory() != this
292: && category != this ;
293: } else if (child instanceof DecoratedWizardPage) {
294: DecoratedWizardPage page = (DecoratedWizardPage) child;
295: return page.getCategory() != this ;
296: }
297: return false;
298: }
299:
300: public String processActionMoveTo() {
301: if (getBase().getWizard().isPublished())
302: return null;
303: DecoratedCategoryChild child = getParent()
304: .getMoveCategoryChild();
305: child.setMoveTarget(false);
306: if (child instanceof DecoratedCategory) {
307: DecoratedCategory category = (DecoratedCategory) child;
308: DecoratedCategory oldParent = category.getParentCategory();
309: oldParent.getBase().getChildCategories().remove(
310: category.getBase());
311: getBase().getChildCategories().add(category.getBase());
312: category.getBase().setParentCategory(getBase());
313: oldParent.resequenceCategories();
314: resequenceCategories();
315: } else if (child instanceof DecoratedWizardPage) {
316: DecoratedWizardPage page = (DecoratedWizardPage) child;
317: DecoratedCategory oldParent = page.getCategory();
318: oldParent.getBase().getChildPages().remove(page.getBase());
319: getBase().getChildPages().add(page.getBase());
320: page.getBase().setCategory(getBase());
321: oldParent.resequencePages();
322: resequencePages();
323: }
324: child.setMoveTarget(false);
325: getParent().setMoveCategoryChild(null);
326: return null;
327: }
328:
329: public boolean getHasChildren() {
330: return getBase().getChildPages().size() > 0
331: || getBase().getChildCategories().size() > 0;
332: }
333:
334: public DecoratedCategory getCategory() {
335: return null;
336: }
337:
338: public boolean isWizard() {
339: return false;
340: }
341: }
|