001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/trunk/sakai/admin-tools/su/src/java/org/sakaiproject/tool/su/SuTool.java $
003: * $Id: SuTool.java 5970 2006-02-15 03:07:19Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 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.sakaiproject.site.impl;
021:
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Stack;
026: import java.util.Vector;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.sakaiproject.entity.api.ResourceProperties;
031: import org.sakaiproject.entity.api.ResourcePropertiesEdit;
032: import org.sakaiproject.id.cover.IdManager;
033: import org.sakaiproject.site.api.Site;
034: import org.sakaiproject.site.api.SitePage;
035: import org.sakaiproject.site.api.ToolConfiguration;
036: import org.sakaiproject.site.cover.SiteService;
037: import org.sakaiproject.util.BaseResourceProperties;
038: import org.sakaiproject.util.BaseResourcePropertiesEdit;
039: import org.sakaiproject.util.StringUtil;
040: import org.sakaiproject.tool.api.Tool;
041: import org.w3c.dom.Document;
042: import org.w3c.dom.Element;
043: import org.w3c.dom.Node;
044: import org.w3c.dom.NodeList;
045:
046: /**
047: * <p>
048: * BaseSitePage is an implementation of the Site API SitePage.
049: * </p>
050: */
051: public class BaseSitePage implements SitePage, Identifiable {
052: /** Our log (commons). */
053: private static Log M_log = LogFactory.getLog(BaseSitePage.class);
054:
055: /** A fixed class serian number. */
056: private static final long serialVersionUID = 1L;
057:
058: /** The title. */
059: protected String m_title = null;
060:
061: /** The layout. */
062: protected int m_layout = LAYOUT_SINGLE_COL;
063:
064: /** The popup setting. */
065: protected boolean m_popup = false;
066:
067: /** The site id. */
068: protected String m_id = null;
069:
070: /** The properties. */
071: protected ResourcePropertiesEdit m_properties = null;
072:
073: /** the list of tool configurations for this SitePage */
074: protected ResourceVector m_tools = null;
075:
076: /** false while the page's tools have not yet been read in. */
077: protected boolean m_toolsLazy = false;
078:
079: /** Active flag. */
080: protected boolean m_active = false;
081:
082: /** The site I belong to. */
083: protected Site m_site = null;
084:
085: /** The site id I belong to, in case I have no m_site. */
086: protected String m_siteId = null;
087:
088: /** The site skin, in case I have no m_site. */
089: protected String m_skin = null;
090:
091: /**
092: * Construct. Auto-generate the id.
093: *
094: * @param site
095: * The site in which this page lives.
096: */
097: protected BaseSitePage(Site site) {
098: m_site = site;
099: m_id = IdManager.createUuid();
100: m_properties = new BaseResourcePropertiesEdit();
101: m_tools = new ResourceVector();
102: }
103:
104: /**
105: * ReConstruct
106: *
107: * @param site
108: * The site in which this page lives.
109: * @param id
110: * The page id.
111: * @param title
112: * The page title.
113: * @param layout
114: * The layout as a string ("0" or not currently supported).
115: * @param popup
116: * The page popup setting.
117: */
118: protected BaseSitePage(Site site, String id, String title,
119: String layout, boolean popup) {
120: m_site = site;
121: m_id = id;
122:
123: m_properties = new BaseResourcePropertiesEdit();
124: ((BaseResourcePropertiesEdit) m_properties).setLazy(true);
125:
126: m_tools = new ResourceVector();
127: m_toolsLazy = true;
128:
129: m_title = title;
130:
131: if (layout.equals(String.valueOf(LAYOUT_SINGLE_COL))) {
132: m_layout = LAYOUT_SINGLE_COL;
133: } else if (layout.equals(String.valueOf(LAYOUT_DOUBLE_COL))) {
134: m_layout = LAYOUT_DOUBLE_COL;
135: }
136:
137: m_popup = popup;
138: }
139:
140: /**
141: * ReConstruct - if we don't have a site to follow up to get to certain site info.
142: *
143: * @param pageId
144: * The page id.
145: * @param title
146: * The page title.
147: * @param layout
148: * The layout as a string ("0" or not currently supported).
149: * @param popup
150: * The page popup setting.
151: * @param siteId
152: * The page's site's id.
153: * @param skin
154: * The page's site's skin.
155: */
156: protected BaseSitePage(String pageId, String title, String layout,
157: boolean popup, String siteId, String skin) {
158: m_site = null;
159: m_id = pageId;
160: m_popup = popup;
161:
162: m_properties = new BaseResourcePropertiesEdit();
163: ((BaseResourcePropertiesEdit) m_properties).setLazy(true);
164:
165: m_tools = new ResourceVector();
166: m_toolsLazy = true;
167:
168: m_title = title;
169:
170: if (layout.equals(String.valueOf(LAYOUT_SINGLE_COL))) {
171: m_layout = LAYOUT_SINGLE_COL;
172: } else if (layout.equals(String.valueOf(LAYOUT_DOUBLE_COL))) {
173: m_layout = LAYOUT_DOUBLE_COL;
174: }
175:
176: m_popup = popup;
177:
178: m_siteId = siteId;
179: m_skin = skin;
180: }
181:
182: /**
183: * Construct as a copy of another.
184: *
185: * @param other
186: * The other to copy.
187: * @param site
188: * The site in which this page lives.
189: * @param exact
190: * If true, we copy ids - else we generate new ones for page and tools.
191: */
192: protected BaseSitePage(SitePage other, Site site, boolean exact) {
193: BaseSitePage bOther = (BaseSitePage) other;
194:
195: m_site = site;
196:
197: if (exact) {
198: m_id = bOther.m_id;
199: } else {
200: m_id = IdManager.createUuid();
201: }
202: m_title = bOther.m_title;
203: m_layout = bOther.m_layout;
204: m_popup = bOther.m_popup;
205:
206: m_properties = new BaseResourcePropertiesEdit();
207: ResourceProperties pOther = other.getProperties();
208: // exact copying of SitePage properties vs replacing occurence of site id within, depending on "exact" setting --- zqian
209: if (exact) {
210: m_properties.addAll(pOther);
211: } else {
212: Iterator l = pOther.getPropertyNames();
213: while (l.hasNext()) {
214: String pOtherName = (String) l.next();
215: m_properties.addProperty(pOtherName, pOther
216: .getProperty(pOtherName).replaceAll(
217: bOther.getSiteId(), getSiteId()));
218: }
219: }
220:
221: ((BaseResourcePropertiesEdit) m_properties)
222: .setLazy(((BaseResourceProperties) other
223: .getProperties()).isLazy());
224:
225: // deep copy the tools
226: m_tools = new ResourceVector();
227: for (Iterator iTools = bOther.getTools().iterator(); iTools
228: .hasNext();) {
229: BaseToolConfiguration tool = (BaseToolConfiguration) iTools
230: .next();
231: m_tools.add(new BaseToolConfiguration(tool, this , exact));
232: }
233: m_toolsLazy = ((BaseSitePage) other).m_toolsLazy;
234:
235: m_siteId = bOther.m_siteId;
236: m_skin = bOther.m_skin;
237: }
238:
239: /**
240: * Construct from XML element.
241: *
242: * @param el
243: * The XML element.
244: * @param site
245: * The site in which this page lives.
246: */
247: protected BaseSitePage(Element el, Site site) {
248: m_site = site;
249:
250: // setup for properties
251: m_properties = new BaseResourcePropertiesEdit();
252:
253: // setup for page list
254: m_tools = new ResourceVector();
255:
256: m_id = el.getAttribute("id");
257: m_title = StringUtil.trimToNull(el.getAttribute("title"));
258: try {
259: m_layout = Integer.parseInt(StringUtil.trimToNull(el
260: .getAttribute("layout")));
261: } catch (Exception e) {
262: }
263:
264: try {
265: m_popup = Boolean.valueOf(el.getAttribute("popup"))
266: .booleanValue();
267: } catch (Exception e) {
268: }
269:
270: // the children (properties and page list)
271: NodeList children = el.getChildNodes();
272: final int length = children.getLength();
273: for (int i = 0; i < length; i++) {
274: Node child = children.item(i);
275: if (child.getNodeType() != Node.ELEMENT_NODE)
276: continue;
277: Element element = (Element) child;
278:
279: // look for properties
280: if (element.getTagName().equals("properties")) {
281: // re-create properties
282: m_properties = new BaseResourcePropertiesEdit(element);
283: }
284:
285: // look for the tool list
286: else if (element.getTagName().equals("tools")) {
287: NodeList toolsNodes = element.getChildNodes();
288: for (int t = 0; t < toolsNodes.getLength(); t++) {
289: Node toolNode = toolsNodes.item(t);
290: if (toolNode.getNodeType() != Node.ELEMENT_NODE)
291: continue;
292: Element toolEl = (Element) toolNode;
293: if (!toolEl.getTagName().equals("tool"))
294: continue;
295:
296: BaseToolConfiguration tool = new BaseToolConfiguration(
297: toolEl, this );
298: m_tools.add(tool);
299: }
300: }
301: }
302: }
303:
304: /**
305: * @inheritDoc
306: */
307: public String getTitle() {
308: return m_title;
309: }
310:
311: /**
312: * @inheritDoc
313: */
314: public int getLayout() {
315: return m_layout;
316: }
317:
318: /**
319: * @inheritDoc
320: */
321: public String getSkin() {
322: if (m_site != null) {
323: return ((BaseSiteService) (SiteService.getInstance()))
324: .adjustSkin(m_site.getSkin(), m_site.isPublished());
325: }
326:
327: return m_skin;
328: }
329:
330: /**
331: * @inheritDoc
332: */
333: public String getSiteId() {
334: if (m_site != null) {
335: return m_site.getId();
336: }
337:
338: return m_siteId;
339: }
340:
341: /**
342: * @inheritDoc
343: */
344: public boolean isPopUp() {
345: return m_popup;
346: }
347:
348: /**
349: * @inheritDoc
350: */
351: public String getLayoutTitle() {
352: return ((BaseSiteService) (SiteService.getInstance()))
353: .getLayoutNames()[m_layout];
354: }
355:
356: /**
357: * @inheritDoc
358: */
359: public List getTools() {
360: if (m_toolsLazy) {
361: ((BaseSiteService) (SiteService.getInstance())).m_storage
362: .readPageTools(this , m_tools);
363: m_toolsLazy = false;
364: }
365:
366: // TODO: need to sort by layout hint
367: return m_tools;
368: }
369:
370: /**
371: * @inheritDoc
372: */
373: public Collection getTools(String[] toolIds) {
374: List rv = new Vector();
375: if ((toolIds == null) || (toolIds.length == 0))
376: return rv;
377:
378: for (Iterator iTools = getTools().iterator(); iTools.hasNext();) {
379: ToolConfiguration tc = (ToolConfiguration) iTools.next();
380: Tool tool = tc.getTool();
381: if ((tool != null) && (tool.getId() != null)) {
382: for (int i = 0; i < toolIds.length; i++) {
383: if (tool.getId().equals(toolIds[i])) {
384: rv.add(tc);
385: }
386: }
387: }
388: }
389:
390: return rv;
391: }
392:
393: /**
394: * @inheritDoc
395: */
396: public List getTools(int col) {
397: // TODO: need to sort by layout hint
398: List rv = new Vector();
399: for (Iterator iTools = getTools().iterator(); iTools.hasNext();) {
400: ToolConfiguration tc = (ToolConfiguration) iTools.next();
401: // row, col
402: int[] layout = tc.parseLayoutHints();
403: if (layout != null) {
404: if (layout[1] == col) {
405: rv.add(tc);
406: }
407: }
408: // else consider it part of the 0 column
409: else if (col == 0) {
410: rv.add(tc);
411: }
412: }
413: return rv;
414: }
415:
416: /**
417: * @inheritDoc
418: */
419: public ToolConfiguration getTool(String id) {
420: return (ToolConfiguration) ((ResourceVector) getTools())
421: .getById(id);
422: }
423:
424: /**
425: * @inheritDoc
426: */
427: public void setTitle(String title) {
428: m_title = StringUtil.trimToNull(title);
429: }
430:
431: /**
432: * @inheritDoc
433: */
434: public void setLayout(int layout) {
435: if ((layout == LAYOUT_SINGLE_COL)
436: || (layout == LAYOUT_DOUBLE_COL)) {
437: m_layout = layout;
438: } else
439: M_log.warn("setLayout(): set to invalid value: " + layout);
440: }
441:
442: /**
443: * @inheritDoc
444: */
445: public void setPopup(boolean popup) {
446: m_popup = popup;
447: }
448:
449: /**
450: * @inheritDoc
451: */
452: public ToolConfiguration addTool() {
453: BaseToolConfiguration tool = new BaseToolConfiguration(this );
454: ((ResourceVector) getTools()).add(tool);
455:
456: return tool;
457: }
458:
459: /**
460: * @inheritDoc
461: */
462: public ToolConfiguration addTool(Tool reg) {
463: BaseToolConfiguration tool = new BaseToolConfiguration(reg,
464: this );
465: ((ResourceVector) getTools()).add(tool);
466:
467: return tool;
468: }
469:
470: /**
471: * @inheritDoc
472: */
473: public ToolConfiguration addTool(String toolId) {
474: BaseToolConfiguration tool = new BaseToolConfiguration(toolId,
475: this );
476: ((ResourceVector) getTools()).add(tool);
477:
478: return tool;
479: }
480:
481: /**
482: * @inheritDoc
483: */
484: public void removeTool(ToolConfiguration tool) {
485: ((ResourceVector) getTools()).remove(tool);
486: }
487:
488: /**
489: * @inheritDoc
490: */
491: public void moveUp() {
492: if (m_site == null)
493: return;
494: ((ResourceVector) m_site.getPages()).moveUp(this );
495: }
496:
497: /**
498: * @inheritDoc
499: */
500: public void setPosition(int pos) {
501: if (m_site == null)
502: return;
503: ((ResourceVector) m_site.getPages()).moveTo(this , pos);
504: }
505:
506: /**
507: * @inheritDoc
508: */
509: public int getPosition() {
510: if (m_site == null)
511: return -1;
512: return ((ResourceVector) m_site.getPages()).indexOf(this );
513: }
514:
515: /**
516: * @inheritDoc
517: */
518: public void moveDown() {
519: if (m_site == null)
520: return;
521: ((ResourceVector) m_site.getPages()).moveDown(this );
522: }
523:
524: /**
525: * @inheritDoc
526: */
527: public ResourcePropertiesEdit getPropertiesEdit() {
528: if (((BaseResourceProperties) m_properties).isLazy()) {
529: ((BaseSiteService) (SiteService.getInstance())).m_storage
530: .readPageProperties(this , m_properties);
531: ((BaseResourcePropertiesEdit) m_properties).setLazy(false);
532: }
533:
534: return m_properties;
535: }
536:
537: /**
538: * Enable editing.
539: */
540: protected void activate() {
541: m_active = true;
542: }
543:
544: /**
545: * @inheritDoc
546: */
547: public boolean isActiveEdit() {
548: return m_active;
549: }
550:
551: /**
552: * Close the edit object - it cannot be used after this.
553: */
554: protected void closeEdit() {
555: m_active = false;
556: }
557:
558: /**
559: * @inheritDoc
560: */
561: public String getUrl() {
562: String rv = null;
563: if (m_site == null) {
564: rv = ((BaseSiteService) (SiteService.getInstance()))
565: .serverConfigurationService().getPortalUrl()
566: + ((BaseSiteService) (SiteService.getInstance()))
567: .sitePageReference(m_siteId, m_id);
568: }
569:
570: rv = ((BaseSiteService) (SiteService.getInstance()))
571: .serverConfigurationService().getPortalUrl()
572: + ((BaseSiteService) (SiteService.getInstance()))
573: .sitePageReference(m_site.getId(), m_id);
574:
575: return rv;
576: }
577:
578: /**
579: * @inheritDoc
580: */
581: public String getReference() {
582: if (m_site == null) {
583: return ((BaseSiteService) (SiteService.getInstance()))
584: .sitePageReference(m_siteId, m_id);
585: }
586:
587: return ((BaseSiteService) (SiteService.getInstance()))
588: .sitePageReference(m_site.getId(), m_id);
589: }
590:
591: /**
592: * @inheritDoc
593: */
594: public String getReference(String rootProperty) {
595: return getReference();
596: }
597:
598: /**
599: * @inheritDoc
600: */
601: public String getUrl(String rootProperty) {
602: return getUrl();
603: }
604:
605: /**
606: * @inheritDoc
607: */
608: public String getId() {
609: return m_id;
610: }
611:
612: /**
613: * @inheritDoc
614: */
615: public Site getContainingSite() {
616: return m_site;
617: }
618:
619: /**
620: * @inheritDoc
621: */
622: public ResourceProperties getProperties() {
623: if (((BaseResourceProperties) m_properties).isLazy()) {
624: ((BaseSiteService) (SiteService.getInstance())).m_storage
625: .readPageProperties(this , m_properties);
626: ((BaseResourcePropertiesEdit) m_properties).setLazy(false);
627: }
628:
629: return m_properties;
630: }
631:
632: /**
633: * @inheritDoc
634: */
635: public Element toXml(Document doc, Stack stack) {
636: Element page = doc.createElement("page");
637: ((Element) stack.peek()).appendChild(page);
638:
639: page.setAttribute("id", getId());
640: if (m_title != null)
641: page.setAttribute("title", m_title);
642: page.setAttribute("layout", Integer.toString(m_layout));
643: page.setAttribute("popup", Boolean.valueOf(m_popup).toString());
644:
645: // properties
646: stack.push(page);
647: getProperties().toXml(doc, stack);
648: stack.pop();
649:
650: // tools
651: Element list = doc.createElement("tools");
652: page.appendChild(list);
653: stack.push(list);
654: for (Iterator iTools = getTools().iterator(); iTools.hasNext();) {
655: BaseToolConfiguration tool = (BaseToolConfiguration) iTools
656: .next();
657: tool.toXml(doc, stack);
658: }
659: stack.pop();
660:
661: return page;
662: }
663: }
|