001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/osp/tags/sakai_2-4-1/integration/api-impl/src/java/org/theospi/portfolio/admin/service/ToolPageIntegrationPlugin.java $
003: * $Id: ToolPageIntegrationPlugin.java 11372 2006-06-29 14:58:30Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.admin.service;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.sakaiproject.exception.IdUnusedException;
025: import org.sakaiproject.exception.PermissionException;
026: import org.sakaiproject.site.api.Site;
027: import org.sakaiproject.site.api.SitePage;
028: import org.sakaiproject.site.api.SiteService;
029: import org.sakaiproject.site.api.ToolConfiguration;
030: import org.sakaiproject.tool.api.Tool;
031: import org.sakaiproject.tool.api.ToolManager;
032: import org.theospi.portfolio.admin.model.IntegrationOption;
033: import org.theospi.portfolio.shared.model.OspException;
034:
035: import java.util.Iterator;
036: import java.util.List;
037: import java.util.Map;
038: import java.util.Properties;
039:
040: public class ToolPageIntegrationPlugin extends IntegrationPluginBase {
041: protected final transient Log logger = LogFactory
042: .getLog(getClass());
043: private ToolManager toolManager;
044: private SiteService siteService;
045:
046: protected boolean currentlyIncluded(IntegrationOption option) {
047: if (option instanceof ExistingWorksitePageOption) {
048: return includedInExistingWorksite((ExistingWorksitePageOption) option);
049: }
050:
051: PageOption page = (PageOption) option;
052: return (loadPage(page.getWorksiteId(), page) != null);
053: }
054:
055: protected boolean includedInExistingWorksite(
056: ExistingWorksitePageOption option) {
057: List sites = getSiteService()
058: .getSites(
059: org.sakaiproject.site.api.SiteService.SelectionType.ANY,
060: null,
061: null,
062: null,
063: org.sakaiproject.site.api.SiteService.SortType.NONE,
064: null);
065:
066: for (Iterator i = sites.iterator(); i.hasNext();) {
067: Site site = (Site) i.next();
068: if (isType(site, option)) {
069: if (!checkSite(site, option)) {
070: return false;
071: }
072: }
073: }
074:
075: return true;
076: }
077:
078: protected boolean isType(Site site,
079: ExistingWorksitePageOption option) {
080: if (option.getWorksiteType().equals("user")) {
081: return getSiteService().isUserSite(site.getId());
082: } else {
083: return site.isType(option.getWorksiteType());
084: }
085: }
086:
087: protected boolean checkSite(Site site, PageOption option) {
088: List pages = site.getPages();
089: for (Iterator i = pages.iterator(); i.hasNext();) {
090: SitePage page = (SitePage) i.next();
091: if (page.getTitle().equals(option.getPageName())) {
092: return true;
093: }
094: }
095:
096: return false;
097: }
098:
099: protected SitePage loadPage(String siteId, PageOption pageOption) {
100: Site site;
101: try {
102: site = getSiteService().getSite(siteId);
103: List pages = site.getPages();
104: for (Iterator i = pages.iterator(); i.hasNext();) {
105: SitePage page = (SitePage) i.next();
106: if (page.getTitle().equals(pageOption.getPageName())) {
107: return page;
108: }
109: }
110: } catch (IdUnusedException e) {
111: logger.error("", e);
112: throw new OspException(e);
113: }
114:
115: return null;
116: }
117:
118: public IntegrationOption updateOption(IntegrationOption option) {
119: PageOption page = (PageOption) option;
120:
121: if (!currentlyIncluded(page) && option.isInclude()) {
122: addPage(page);
123: } else if (currentlyIncluded(page) && !option.isInclude()) {
124: removePage(page);
125: }
126:
127: return option;
128: }
129:
130: public boolean executeOption(IntegrationOption option) {
131: PageOption pageOption = (PageOption) option;
132:
133: /*
134: for (Iterator i=pageOption.getTools().iterator();i.hasNext();) {
135: ToolOption toolOption = (ToolOption) i.next();
136: if (getToolManager().getTool(toolOption.getToolId()) == null) {
137: return false;
138: }
139: }
140: */
141:
142: // also check the existing tools, if any
143: /*
144: try {
145: Site site = getSiteService().getSite(pageOption.getWorksiteId());
146:
147: for (Iterator i=site.getPages().iterator();i.hasNext();) {
148: SitePage page = (SitePage) i.next();
149: for (Iterator j=page.getTools().iterator();j.hasNext();) {
150: ToolConfiguration tool = (ToolConfiguration) j.next();
151: if (tool.getTool() == null) {
152: return false;
153: }
154: }
155: }
156: } catch (IdUnusedException e) {
157: logger.warn("", e);
158: }
159: */
160:
161: updateOption(option);
162: return true;
163: }
164:
165: protected void addPage(PageOption page) {
166: if (page instanceof ExistingWorksitePageOption) {
167: ExistingWorksitePageOption option = (ExistingWorksitePageOption) page;
168: List sites = getSiteService()
169: .getSites(
170: SiteService.SelectionType.ANY,
171: null,
172: null,
173: null,
174: org.sakaiproject.site.api.SiteService.SortType.NONE,
175: null);
176:
177: for (Iterator i = sites.iterator(); i.hasNext();) {
178: Site site = (Site) i.next();
179: if (isType(site, option)) {
180: addPage(site.getId(), option);
181: }
182: }
183: } else {
184: addPage(page.getWorksiteId(), page);
185: }
186: }
187:
188: protected void addPage(String siteId, PageOption page) {
189:
190: if (loadPage(siteId, page) != null) {
191: return;
192: }
193:
194: Site site;
195: try {
196: site = getSiteService().getSite(siteId);
197:
198: SitePage pageEdit = site.addPage();
199: pageEdit.setTitle(page.getPageName());
200: pageEdit.setLayout(page.getLayout());
201:
202: for (Iterator i = page.getTools().iterator(); i.hasNext();) {
203: addTool(pageEdit, (ToolOption) i.next());
204: }
205:
206: for (int i = 0; i < page.getPositionFromEnd(); i++) {
207: pageEdit.moveUp();
208: }
209:
210: getSiteService().save(site);
211: } catch (IdUnusedException e) {
212: logger.error("", e);
213: throw new OspException(e);
214: } catch (PermissionException e) {
215: logger.error("", e);
216: throw new OspException(e);
217: }
218: }
219:
220: protected void addTool(SitePage pageEdit, ToolOption toolOption) {
221: Tool reg = new ToolWrapper(toolOption.getToolId());
222: ToolConfiguration tool = pageEdit.addTool(reg);
223: tool.setTitle(toolOption.getTitle());
224: tool.setLayoutHints(toolOption.getLayoutHints());
225: Properties props = tool.getPlacementConfig();
226:
227: for (Iterator i = toolOption.getInitProperties().entrySet()
228: .iterator(); i.hasNext();) {
229: Map.Entry entry = (Map.Entry) i.next();
230: props.setProperty((String) entry.getKey(), (String) entry
231: .getValue());
232: }
233: }
234:
235: protected void removePage(PageOption page) {
236: if (page instanceof ExistingWorksitePageOption) {
237: ExistingWorksitePageOption option = (ExistingWorksitePageOption) page;
238: List sites = getSiteService()
239: .getSites(
240: org.sakaiproject.site.api.SiteService.SelectionType.ANY,
241: null,
242: null,
243: null,
244: org.sakaiproject.site.api.SiteService.SortType.NONE,
245: null);
246:
247: for (Iterator i = sites.iterator(); i.hasNext();) {
248: Site site = (Site) i.next();
249: if (isType(site, option)) {
250: removePage(site.getId(), option);
251: }
252: }
253: } else {
254: removePage(page.getWorksiteId(), page);
255: }
256: }
257:
258: protected void removePage(String siteId, PageOption page) {
259: SitePage sitePage = loadPage(siteId, page);
260:
261: Site site;
262: try {
263: site = getSiteService().getSite(
264: sitePage.getContainingSite().getId());
265: SitePage pageEdit = site.getPage(sitePage.getId());
266:
267: site.removePage(pageEdit);
268: getSiteService().save(site);
269: } catch (IdUnusedException e) {
270: logger.error("", e);
271: throw new OspException(e);
272: } catch (PermissionException e) {
273: logger.error("", e);
274: throw new OspException(e);
275: }
276: }
277:
278: public ToolManager getToolManager() {
279: return toolManager;
280: }
281:
282: public void setToolManager(ToolManager toolManager) {
283: this .toolManager = toolManager;
284: }
285:
286: public SiteService getSiteService() {
287: return siteService;
288: }
289:
290: public void setSiteService(SiteService siteService) {
291: this.siteService = siteService;
292: }
293:
294: }
|