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.Enumeration;
023: import java.util.Hashtable;
024: import java.util.Iterator;
025: import java.util.Properties;
026: import java.util.Stack;
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.event.cover.EventTrackingService;
032: import org.sakaiproject.id.cover.IdManager;
033: import org.sakaiproject.site.api.SitePage;
034: import org.sakaiproject.site.api.ToolConfiguration;
035: import org.sakaiproject.site.cover.SiteService;
036: import org.sakaiproject.util.StringUtil;
037: import org.sakaiproject.util.Xml;
038: import org.sakaiproject.tool.api.Tool;
039: import org.sakaiproject.tool.cover.ToolManager;
040: import org.w3c.dom.Document;
041: import org.w3c.dom.Element;
042: import org.w3c.dom.Node;
043: import org.w3c.dom.NodeList;
044:
045: /**
046: * <p>
047: * BaseToolConfiguration is an implementation of the Site API's ToolConfiguration.
048: * </p>
049: */
050: public class BaseToolConfiguration extends
051: org.sakaiproject.util.Placement implements ToolConfiguration,
052: Identifiable {
053: /** Our log (commons). */
054: private static Log M_log = LogFactory
055: .getLog(BaseToolConfiguration.class);
056:
057: /** A fixed class serian number. */
058: private static final long serialVersionUID = 1L;
059:
060: /** The layout hints. */
061: protected String m_layoutHints = null;
062:
063: /** The SitePage I belong to. */
064: protected SitePage m_page = null;
065:
066: /** The site id I belong to, in case I have no m_page. */
067: protected String m_siteId = null;
068:
069: /** The page id I belong to, in case I have no m_page. */
070: protected String m_pageId = null;
071:
072: /** The site skin, in case I have no m_page. */
073: protected String m_skin = null;
074:
075: /** True if the placement conf has not been read yet. */
076: protected boolean m_configLazy = false;
077:
078: /** The order within the page. */
079: protected int m_pageOrder = -1;
080:
081: /**
082: * ReConstruct
083: *
084: * @param page
085: * The page in which this tool lives.
086: * @param id
087: * The tool (placement) id.
088: * @param toolId
089: * The id (registration code) of the tool to place here.
090: * @param title
091: * The tool title.
092: * @param layoutHints
093: * The layout hints.
094: * @param pageOrder
095: * The order within the page.
096: */
097: protected BaseToolConfiguration(SitePage page, String id,
098: String toolId, String title, String layoutHints,
099: int pageOrder) {
100: super (id, toolId, ToolManager.getTool(toolId), null, null,
101: title);
102:
103: m_page = page;
104: m_layoutHints = layoutHints;
105: m_pageOrder = pageOrder;
106:
107: m_configLazy = true;
108: }
109:
110: /**
111: * ReConstruct - if we don't have a page to follow up to get to certain page and site info.
112: *
113: * @param id
114: * The tool (placement) id.
115: * @param toolId
116: * The id (registration code) of the tool to place here.
117: * @param title
118: * The tool title.
119: * @param layoutHints
120: * The layout hints.
121: * @param pageId
122: * The page id in which this tool lives.
123: * @param siteId
124: * The site id in which this tool lives.
125: * @param skin
126: * The site's skin.
127: * @param pageOrder
128: * The order within the page.
129: */
130: protected BaseToolConfiguration(String id, String toolId,
131: String title, String layoutHints, String pageId,
132: String siteId, String skin, int pageOrder) {
133: super (id, toolId, ToolManager.getTool(toolId), null, null,
134: title);
135:
136: m_page = null;
137:
138: m_layoutHints = layoutHints;
139: m_pageId = pageId;
140: m_siteId = siteId;
141: m_skin = skin;
142: m_pageOrder = pageOrder;
143:
144: m_configLazy = true;
145: }
146:
147: /**
148: * Construct as a copy of another.
149: *
150: * @param other
151: * The other to copy.
152: * @param page
153: * The page in which this tool lives.
154: * @param exact
155: * If true, we copy ids - else we generate a new one.
156: */
157: protected BaseToolConfiguration(ToolConfiguration other,
158: SitePage page, boolean exact) {
159: m_page = page;
160: BaseToolConfiguration bOther = (BaseToolConfiguration) other;
161:
162: if (exact) {
163: m_id = other.getId();
164: } else {
165: m_id = IdManager.createUuid();
166: }
167: m_toolId = other.getToolId();
168: m_tool = other.getTool();
169: m_title = other.getTitle();
170: m_layoutHints = other.getLayoutHints();
171: m_pageId = bOther.m_pageId;
172: m_pageOrder = bOther.m_pageOrder;
173:
174: m_siteId = getContainingPage().getContainingSite().getId();
175: m_skin = bOther.m_skin;
176:
177: Hashtable h = other.getPlacementConfig();
178: // exact copying of ToolConfiguration items vs replacing occurence of site id within item value, depending on "exact" setting -zqian
179: if (exact) {
180: m_config.putAll(other.getPlacementConfig());
181: } else {
182: for (Enumeration e = h.keys(); e.hasMoreElements();) {
183: // replace site id string inside configuration
184: String pOtherConfig = (String) e.nextElement();
185: String pOtherConfigValue = (String) h.get(pOtherConfig);
186: m_config.put(pOtherConfig, pOtherConfigValue
187: .replaceAll(bOther.getSiteId(), m_siteId));
188: }
189: }
190: m_configLazy = bOther.m_configLazy;
191: }
192:
193: /**
194: * Construct using a tool registration for default information.
195: *
196: * @param reg
197: * The tool registration.
198: * @param page
199: * The page in which this tool lives.
200: */
201: protected BaseToolConfiguration(SitePage page) {
202: super (IdManager.createUuid(), null, null, null, null, null);
203:
204: m_page = page;
205: }
206:
207: /**
208: * Construct using a tool registration for default information.
209: *
210: * @param reg
211: * The tool registration.
212: * @param page
213: * The page in which this tool lives.
214: */
215: protected BaseToolConfiguration(Tool reg, SitePage page) {
216: super (IdManager.createUuid(), reg.getId(), reg, null, null,
217: null);
218:
219: m_page = page;
220: }
221:
222: /**
223: * Construct using a tool id.
224: *
225: * @param toolId
226: * The tool id.
227: * @param page
228: * The page in which this tool lives.
229: */
230: protected BaseToolConfiguration(String toolId, SitePage page) {
231: super (IdManager.createUuid(), toolId, null, null, null, null);
232:
233: m_page = page;
234: }
235:
236: /**
237: * Construct from XML element.
238: *
239: * @param el
240: * The XML element.
241: * @param page
242: * The page in which this tool lives.
243: */
244: protected BaseToolConfiguration(Element el, SitePage page) {
245: super ();
246:
247: m_page = page;
248:
249: m_id = el.getAttribute("id");
250: m_toolId = StringUtil.trimToNull(el.getAttribute("toolId"));
251: if (m_toolId != null) {
252: m_tool = ToolManager.getTool(m_toolId);
253: }
254: m_title = StringUtil.trimToNull(el.getAttribute("title"));
255: m_layoutHints = StringUtil.trimToNull(el
256: .getAttribute("layoutHints"));
257:
258: // the children (properties)
259: NodeList children = el.getChildNodes();
260: final int length = children.getLength();
261: for (int i = 0; i < length; i++) {
262: Node child = children.item(i);
263: if (child.getNodeType() != Node.ELEMENT_NODE)
264: continue;
265: Element element = (Element) child;
266:
267: // look for properties
268: if (element.getTagName().equals("properties")) {
269: // re-create properties
270: Xml.xmlToProperties(m_config, element);
271: }
272: }
273: }
274:
275: /**
276: * {@inheritDoc}
277: */
278: public Properties getPlacementConfig() {
279: // if the config has not yet been read, read it
280: if (m_configLazy) {
281: ((BaseSiteService) (SiteService.getInstance())).m_storage
282: .readToolProperties(this , m_config);
283: m_configLazy = false;
284: }
285:
286: return m_config;
287: }
288:
289: /**
290: * Acces the m_config, which is inherited and not visible to this package outside this class -ggolden
291: */
292: protected Properties getMyConfig() {
293: return m_config;
294: }
295:
296: /**
297: * @inheritDoc
298: */
299: public String getLayoutHints() {
300: return m_layoutHints;
301: }
302:
303: /**
304: * @inheritDoc
305: */
306: public int[] parseLayoutHints() {
307: try {
308: if (m_layoutHints == null)
309: return null;
310: String[] parts = StringUtil.split(m_layoutHints, ",");
311: if (parts.length < 2)
312: return null;
313: int[] rv = new int[2];
314: rv[0] = Integer.parseInt(parts[0]);
315: rv[1] = Integer.parseInt(parts[1]);
316: return rv;
317: } catch (Throwable t) {
318: return null;
319: }
320: }
321:
322: /**
323: * {@inheritDoc}
324: */
325: public int getPageOrder() {
326: return m_pageOrder;
327: }
328:
329: /**
330: * {@inheritDoc}
331: */
332: public String getSkin() {
333: // use local copy if no page is set
334: if (m_page == null) {
335: return m_skin;
336: }
337:
338: return m_page.getSkin();
339: }
340:
341: /**
342: * {@inheritDoc}
343: */
344: public String getPageId() {
345: // use local copy if no page is set
346: if (m_page == null) {
347: return m_pageId;
348: }
349:
350: return getContainingPage().getId();
351: }
352:
353: /**
354: * {@inheritDoc}
355: */
356: public String getSiteId() {
357: // use local copy if no page is set
358: if (m_page == null) {
359: return m_siteId;
360: }
361:
362: return getContainingPage().getContainingSite().getId();
363: }
364:
365: /**
366: * {@inheritDoc}
367: */
368: public String getContext() {
369: // the context of a site based placement is the site id
370: return getSiteId();
371: }
372:
373: /**
374: * {@inheritDoc}
375: */
376: public void setLayoutHints(String hints) {
377: m_layoutHints = hints;
378: }
379:
380: /**
381: * {@inheritDoc}
382: */
383: public void moveUp() {
384: if (m_page == null) {
385: M_log.warn("moveUp: null page: " + m_id);
386: return;
387: }
388:
389: ((ResourceVector) m_page.getTools()).moveUp(this );
390: }
391:
392: /**
393: * {@inheritDoc}
394: */
395: public void moveDown() {
396: if (m_page == null) {
397: M_log.warn("moveDown: null page: " + m_id);
398: return;
399: }
400:
401: ((ResourceVector) m_page.getTools()).moveDown(this );
402: }
403:
404: /**
405: * {@inheritDoc}
406: */
407: public SitePage getContainingPage() {
408: return m_page;
409: }
410:
411: /**
412: * {@inheritDoc}
413: */
414: public Element toXml(Document doc, Stack stack) {
415: Element element = doc.createElement("tool");
416: ((Element) stack.peek()).appendChild(element);
417: stack.push(element);
418:
419: element.setAttribute("id", getId());
420: String toolId = getToolId();
421: if (toolId != null)
422: element.setAttribute("toolId", toolId);
423: if (m_title != null)
424: element.setAttribute("title", m_title);
425: if (m_layoutHints != null)
426: element.setAttribute("layoutHints", m_layoutHints);
427:
428: // properties
429: Xml.propertiesToXml(getPlacementConfig(), doc, stack);
430:
431: stack.pop();
432:
433: return (Element) element;
434: }
435:
436: /**
437: * {@inheritDoc}
438: */
439: public void save() {
440: // TODO: security? version?
441: ((BaseSiteService) (SiteService.getInstance())).m_storage
442: .saveToolConfig(this );
443:
444: // track the site change
445: EventTrackingService.post(EventTrackingService.newEvent(
446: SiteService.SECURE_UPDATE_SITE, SiteService
447: .siteReference(getSiteId()), true));
448: }
449: }
|