001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/portal/api-impl/src/java/org/theospi/portfolio/portal/impl/PortalManagerImpl.java $
003: * $Id:PortalManagerImpl.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2006, 2007 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.portal.impl;
021:
022: import org.sakaiproject.authz.api.Role;
023: import org.sakaiproject.component.cover.ServerConfigurationService;
024: import org.sakaiproject.exception.IdUnusedException;
025: import org.sakaiproject.javax.PagingPosition;
026: import org.sakaiproject.metaobj.shared.mgt.IdManager;
027: import org.sakaiproject.site.api.Site;
028: import org.sakaiproject.site.api.SitePage;
029: import org.sakaiproject.site.api.SiteService;
030: import org.sakaiproject.site.api.ToolConfiguration;
031: import org.sakaiproject.tool.api.Placement;
032: import org.sakaiproject.user.api.User;
033: import org.sakaiproject.user.api.UserDirectoryService;
034: import org.sakaiproject.util.ResourceLoader;
035: import org.theospi.portfolio.portal.intf.PortalManager;
036: import org.theospi.portfolio.portal.model.SitePageWrapper;
037: import org.theospi.portfolio.portal.model.SiteType;
038: import org.theospi.portfolio.portal.model.ToolCategory;
039: import org.theospi.portfolio.portal.model.ToolType;
040: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
041:
042: import java.util.*;
043:
044: /**
045: * Created by IntelliJ IDEA.
046: * User: John Ellis
047: * Date: Feb 11, 2006
048: * Time: 8:38:52 AM
049: * To change this template use File | Settings | File Templates.
050: */
051: public class PortalManagerImpl extends HibernateDaoSupport implements
052: PortalManager {
053:
054: private UserDirectoryService userDirectoryService;
055: private SiteService siteService;
056: private IdManager idManager;
057: private org.sakaiproject.metaobj.security.AuthorizationFacade sakaiAuthzManager;
058: private org.theospi.portfolio.security.AuthorizationFacade ospAuthzManager;
059: private boolean displayToolCategories = true;
060: private boolean displaySiteTypes = true;
061: private boolean useDb = false;
062: private boolean reloadDb = false;
063: private boolean cleanedDb = false;
064: private boolean autoDdl = true;
065:
066: private Map siteTypes;
067: private static final String TYPE_PREFIX = "org.theospi.portfolio.portal.";
068: private ResourceLoader rl = new ResourceLoader();
069:
070: public void init() {
071:
072: if (isUseDb() && isAutoDdl()) {
073: Map<String, SiteType> siteTypesTempMap = loadDbSiteTypes();
074: if (siteTypesTempMap.size() == 0 || isReloadDb()) {
075: siteTypesTempMap = storeComponentsSiteTypes(siteTypes);
076: cleanedDb = true;
077: } else {
078: setSiteTypes(new HashMap(siteTypesTempMap));
079: }
080: }
081: }
082:
083: public Map<String, SiteType> storeComponentsSiteTypes(Map siteTypes) {
084: Map<String, SiteType> siteTypesTempMap = loadDbSiteTypes();
085: getHibernateTemplate().deleteAll(siteTypesTempMap.values());
086: getHibernateTemplate().flush();
087: Map<String, SiteType> returned = new Hashtable<String, SiteType>();
088: Collection<SiteType> siteTypesCol = siteTypes.values();
089: for (Iterator<SiteType> i = siteTypesCol.iterator(); i
090: .hasNext();) {
091: SiteType type = i.next();
092: storeComponentsSiteType(type);
093: returned.put(type.getName(), type);
094: }
095: setSiteTypes(new HashMap(siteTypesTempMap));
096: return returned;
097: }
098:
099: protected void storeComponentsSiteType(SiteType type) {
100: List<ToolCategory> toolCategories = type.getToolCategories();
101: for (Iterator<ToolCategory> i = toolCategories.iterator(); i
102: .hasNext();) {
103: fixComponentsToolCategory(i.next());
104: }
105: getHibernateTemplate().save(type);
106: }
107:
108: protected void fixComponentsToolCategory(ToolCategory toolCategory) {
109: for (Iterator<Map.Entry> i = toolCategory.getTools().entrySet()
110: .iterator(); i.hasNext();) {
111: Map.Entry entry = i.next();
112: if (entry.getValue() instanceof String) {
113: entry.setValue(new ToolType());
114: }
115: }
116:
117: //todo load i18n pages
118: }
119:
120: protected Map<String, SiteType> loadDbSiteTypes() {
121: Map<String, SiteType> returned = new Hashtable<String, SiteType>();
122: List<SiteType> siteTypesList = getHibernateTemplate().loadAll(
123: SiteType.class);
124: for (Iterator<SiteType> i = siteTypesList.iterator(); i
125: .hasNext();) {
126: SiteType type = i.next();
127: returned.put(type.getName(), type);
128: }
129: return returned;
130: }
131:
132: public User getCurrentUser() {
133: return getUserDirectoryService().getCurrentUser();
134: }
135:
136: public Map getSitesByType() {
137: return getSitesByType(null);
138: }
139:
140: public Map getSitesByType(String siteId) {
141: Map typeMap = new Hashtable();
142: boolean addSite = (siteId != null);
143:
144: if (siteId != null) {
145: getOspAuthzManager().pushAuthzGroups(siteId);
146: }
147:
148: User currentUser = getCurrentUser();
149: if (currentUser != null && currentUser.getId().length() > 0) {
150: String mySiteId = addMyWorkspace(typeMap);
151: if (addSite) {
152: addSite = !(siteId.equals(mySiteId));
153: }
154: } else {
155: return createGatewayMap(typeMap, siteId);
156: }
157:
158: List types = getSiteService().getSiteTypes();
159:
160: List allUserSites = getSiteService().getSites(
161: SiteService.SelectionType.ACCESS, null, null, null,
162: SiteService.SortType.TITLE_ASC, null);
163:
164: for (Iterator i = types.iterator(); i.hasNext();) {
165: String type = (String) i.next();
166: SiteType siteType = (SiteType) getSiteTypes().get(type);
167:
168: if (siteType == null) {
169: siteType = SiteType.OTHER;
170: }
171:
172: if (!siteType.isHidden()) {
173: List sites = getSiteService().getSites(
174: SiteService.SelectionType.ACCESS, type, null,
175: null, SiteService.SortType.TITLE_ASC, null);
176:
177: addSpecialSites(siteType.getSpecialSites(), sites,
178: allUserSites);
179:
180: if (sites.size() > 0 && siteType.isDisplayTab()) {
181: if (addSite) {
182: addSite = !checkSites(siteId, sites);
183: }
184:
185: typeMap.put(siteType, sites);
186: }
187: }
188: }
189:
190: if (addSite) {
191: // didn't already get the site... let's add it here
192: Site siteToAdd = getSite(siteId);
193: SiteType typeToAdd = (SiteType) getSiteTypes().get(
194: siteToAdd.getType());
195: if (typeToAdd == null) {
196: typeToAdd = SiteType.OTHER;
197: }
198: List sitesToAdd = (List) typeMap.get(typeToAdd);
199: if (sitesToAdd == null) {
200: sitesToAdd = new ArrayList();
201: typeMap.put(typeToAdd, sitesToAdd);
202: }
203: sitesToAdd.add(siteToAdd);
204: }
205:
206: return typeMap;
207: }
208:
209: protected boolean checkSites(String siteId, List sites) {
210: for (Iterator i = sites.iterator(); i.hasNext();) {
211: Site site = (Site) i.next();
212: if (site.getId().equals(siteId)) {
213: return true;
214: }
215: }
216: return false;
217: }
218:
219: protected void addSpecialSites(List specialSites, List sites,
220: List allUserSites) {
221:
222: if (specialSites == null) {
223: return;
224: }
225:
226: for (Iterator i = allUserSites.iterator(); i.hasNext();) {
227: Site site = (Site) i.next();
228: if (specialSites.contains(site.getId())) {
229: sites.add(site);
230: }
231: }
232: Collections.sort(sites, new SiteTitleComparator());
233: }
234:
235: protected Map createGatewayMap(Map typeMap, String siteId) {
236: String gatewayId = ServerConfigurationService
237: .getGatewaySiteId();
238:
239: //If I'm passing a site, I want to override the configured gateway
240: if (siteId != null) {
241: gatewayId = siteId;
242: }
243:
244: try {
245: Site gateway = getSiteService().getSite(gatewayId);
246: List sites = new ArrayList();
247: sites.add(gateway);
248: typeMap.put(SiteType.GATEWAY, sites);
249: return typeMap;
250: } catch (IdUnusedException e) {
251: throw new RuntimeException(e);
252: }
253: }
254:
255: protected String addMyWorkspace(Map typeMap) {
256: String myWorkspaceId = getSiteService().getUserSiteId(
257: getCurrentUser().getId());
258: try {
259: Site myWorkspace = getSiteService().getSite(myWorkspaceId);
260: List sites = new ArrayList();
261: sites.add(myWorkspace);
262: typeMap.put(SiteType.MY_WORKSPACE, sites);
263: return myWorkspace.getId();
264: } catch (IdUnusedException e) {
265: throw new RuntimeException(e);
266: }
267: }
268:
269: public List getSitesForType(String type, SiteService.SortType sort,
270: PagingPosition page) {
271: String baseType = extractType(type);
272: List sites = getSiteService().getSites(
273: SiteService.SelectionType.ACCESS, baseType, null, null,
274: sort, page);
275:
276: List allUserSites = getSiteService().getSites(
277: SiteService.SelectionType.ACCESS, null, null, null,
278: SiteService.SortType.TITLE_ASC, null);
279:
280: SiteType siteType = (SiteType) getSiteTypes().get(baseType);
281: addSpecialSites(siteType.getSpecialSites(), sites, allUserSites);
282:
283: return sites;
284: }
285:
286: protected String extractType(String type) {
287: return type.substring(TYPE_PREFIX.length());
288: }
289:
290: public Map getPagesByCategory(String siteId) {
291: try {
292: Site site = getSiteService().getSite(siteId);
293:
294: List pages = site.getPages();
295: List toolOrder = new ArrayList(ServerConfigurationService
296: .getToolOrder(site.getType()));
297:
298: SiteType siteType = (SiteType) getSiteTypes().get(
299: site.getType());
300:
301: if (toolOrder == null) {
302: toolOrder = new ArrayList();
303: }
304:
305: if (siteType != null) {
306: while (siteType.getFirstCategory() > toolOrder.size()) {
307: toolOrder.add(new Integer(0));
308: }
309:
310: toolOrder.addAll(siteType.getFirstCategory(), siteType
311: .getToolCategories());
312: }
313:
314: getOspAuthzManager().pushAuthzGroups(siteId);
315: getSakaiAuthzManager().pushAuthzGroups(siteId);
316:
317: Map categories = categorizePages(pages, siteType, toolOrder);
318:
319: List categoryList = new ArrayList(categories.keySet());
320:
321: Collections.sort(categoryList);
322:
323: Map newCategories = new HashMap();
324:
325: int index = 0;
326: for (Iterator i = categoryList.iterator(); i.hasNext();) {
327: ToolCategory category = (ToolCategory) i.next();
328: Object oldValue = categories.get(category);
329: category.setOrder(index);
330: index++;
331: newCategories.put(category, oldValue);
332: }
333:
334: return newCategories;
335: } catch (IdUnusedException e) {
336: throw new RuntimeException(e);
337: }
338: }
339:
340: protected Map categorizePages(List pages, SiteType type,
341: List toolOrder) {
342: Map pageMap = new Hashtable();
343:
344: int index = 0;
345: for (Iterator i = pages.iterator(); i.hasNext();) {
346: SitePage page = (SitePage) i.next();
347: categorizePage(page, pageMap, type, findIndex(page,
348: toolOrder, index));
349: index++;
350: }
351:
352: return pageMap;
353: }
354:
355: protected int findIndex(SitePage page, List toolOrder,
356: int defaultValue) {
357: List tools = page.getTools();
358:
359: if (tools.size() == 1) {
360: ToolConfiguration toolConfig = (ToolConfiguration) tools
361: .get(0);
362: if (toolConfig.getTool() != null) {
363: String toolId = toolConfig.getTool().getId();
364: if (toolOrder.contains(toolId)) {
365: return toolOrder.indexOf(toolId);
366: }
367: }
368: }
369:
370: return defaultValue;
371: }
372:
373: protected void categorizePage(SitePage page, Map pageMap,
374: SiteType siteType, int index) {
375: ToolCategory[] categories = findCategories(page, siteType,
376: index);
377:
378: for (int i = 0; i < categories.length; i++) {
379: List pages = (List) pageMap.get(categories[i]);
380: if (pages == null) {
381: pages = new ArrayList();
382: pageMap.put(categories[i], pages);
383: }
384: pages.add(new SitePageWrapper(page, index));
385: }
386: }
387:
388: protected ToolCategory[] findCategories(SitePage page,
389: SiteType siteType, int index) {
390: List tools = page.getTools();
391:
392: if (tools.size() == 0 || !isDisplayToolCategories()) {
393: return createUncategorized(index);
394: }
395:
396: Placement tool = (Placement) tools.get(0);
397:
398: boolean foundCategory = false;
399: List toolCategories = new ArrayList();
400:
401: if (tool.getTool() != null) {
402: String toolId = tool.getTool().getId();
403: if (siteType != null
404: && siteType.getToolCategories() != null) {
405: for (Iterator i = siteType.getToolCategories()
406: .iterator(); i.hasNext();) {
407: ToolCategory category = (ToolCategory) i.next();
408: if (category.getTools().containsKey(toolId)) {
409: foundCategory = true;
410: Object key = category.getTools().get(toolId);
411: if (key instanceof String) {
412: // no functions or authz to check here...
413: toolCategories.add(new ToolCategory(
414: category));
415: } else if (hasAccess(page, tool,
416: (ToolType) category.getTools().get(
417: toolId))) {
418: toolCategories.add(new ToolCategory(
419: category));
420: }
421: }
422: }
423: }
424: }
425:
426: if (toolCategories.size() == 0 && !foundCategory) {
427: return createUncategorized(index);
428: }
429:
430: return (ToolCategory[]) toolCategories
431: .toArray(new ToolCategory[toolCategories.size()]);
432: }
433:
434: protected boolean hasAccess(SitePage page, Placement tool,
435: ToolType toolType) {
436:
437: if (toolType.getFunctions() == null
438: || toolType.getFunctions().size() == 0) {
439: // nothin to check
440: return true;
441: }
442:
443: for (Iterator i = toolType.getFunctions().iterator(); i
444: .hasNext();) {
445: if (toolType.getQualifierType().equals(
446: ToolType.SAKAI_QUALIFIER)) {
447: if (getSakaiAuthzManager().isAuthorized(
448: (String) i.next(), null)) {
449: return true;
450: }
451: } else {
452: String qualifier = null;
453: if (toolType.getQualifierType().equals(
454: ToolType.PLACEMENT_QUALIFIER)) {
455: qualifier = tool.getId();
456: } else if (toolType.getQualifierType().equals(
457: ToolType.SITE_QUALIFIER)) {
458: qualifier = page.getSiteId();
459: } else {
460: throw new RuntimeException(
461: "invalid tool type qualifier");
462: }
463:
464: if (getOspAuthzManager().isAuthorized(
465: (String) i.next(),
466: getIdManager().getId(qualifier))) {
467: return true;
468: }
469: }
470: }
471:
472: return false;
473: }
474:
475: protected ToolCategory[] createUncategorized(int index) {
476: try {
477: ToolCategory category = (ToolCategory) ToolCategory.UNCATEGORIZED
478: .clone();
479: category.setOrder(index);
480: return new ToolCategory[] { category };
481:
482: } catch (CloneNotSupportedException e) {
483: throw new RuntimeException(e);
484: }
485: }
486:
487: public List getToolsForPage(String pageId) {
488: SitePage page = getSiteService().findPage(pageId);
489: return page.getTools();
490: }
491:
492: public Site getSite(String siteId) {
493: try {
494: return getSiteService().getSite(siteId);
495: } catch (IdUnusedException e) {
496: throw new RuntimeException(e);
497: }
498: }
499:
500: public SitePage getSitePage(String pageId) {
501: return getSiteService().findPage(pageId);
502: }
503:
504: public String getPageCategory(String siteId, String pageId) {
505: Site site = getSite(siteId);
506: SitePage page = site.getPage(pageId);
507: SiteType siteType = (SiteType) getSiteTypes().get(
508: decorateSiteType(site));
509:
510: ToolCategory[] categories = findCategories(page, siteType, 0);
511:
512: if (categories.length == 0) {
513: return ToolCategory.UNCATEGORIZED.getKey();
514: }
515:
516: return categories[0].getKey();
517: }
518:
519: public String decorateSiteType(String siteTypeKey) {
520: return TYPE_PREFIX + siteTypeKey;
521: }
522:
523: public String decorateSiteType(Site site) {
524: if (getSiteService().isUserSite(site.getId())) {
525: return SiteType.MY_WORKSPACE.getKey();
526: } else if (site.getType() != null) {
527: String siteType = decorateSiteType(site.getType());
528:
529: return siteType;
530: } else {
531: return findSpecialSiteType(site);
532: }
533: }
534:
535: protected String findSpecialSiteType(Site site) {
536: for (Iterator i = getSiteTypes().values().iterator(); i
537: .hasNext();) {
538: SiteType type = (SiteType) i.next();
539: if (type.getSpecialSites() != null
540: && type.getSpecialSites().contains(site.getId())) {
541: return type.getKey();
542: }
543: }
544: return null;
545: }
546:
547: public SiteType getSiteType(String siteTypeKey) {
548: for (Iterator i = getSiteTypes().values().iterator(); i
549: .hasNext();) {
550: SiteType siteType = (SiteType) i.next();
551: if (siteType.getKey().equals(siteTypeKey)) {
552: return siteType;
553: }
554: }
555: return null;
556: }
557:
558: public ToolCategory getToolCategory(String siteTypeKey,
559: String toolCategoryKey) {
560: SiteType siteType = getSiteType(siteTypeKey);
561: for (Iterator i = siteType.getToolCategories().iterator(); i
562: .hasNext();) {
563: ToolCategory toolCategory = (ToolCategory) i.next();
564: if (toolCategory.getKey().equals(toolCategoryKey)) {
565: return toolCategory;
566: }
567: }
568: return null;
569: }
570:
571: public boolean isAvailable(String toolId, String siteId) {
572: Site site = getSite(siteId);
573:
574: ToolConfiguration tool = site.getToolForCommonId(toolId);
575:
576: // todo check permissions
577: return tool != null;
578: }
579:
580: public SitePage getPage(String toolId, String siteId) {
581: Site site = getSite(siteId);
582: ToolConfiguration tool = site.getToolForCommonId(toolId);
583:
584: if (tool == null) {
585: return null;
586: }
587:
588: return getSitePage(tool.getPageId());
589: }
590:
591: public boolean isUserInRole(String roleId, String siteId) {
592: Site site = getSite(siteId);
593: Role role = site.getUserRole(getCurrentUser().getId());
594: return role.getId().equals(roleId);
595: }
596:
597: public List getRoles(String siteId) {
598: Site site = getSite(siteId);
599: List roles = new ArrayList();
600: roles.add(site.getUserRole(getCurrentUser().getId()));
601: return roles;
602: }
603:
604: public UserDirectoryService getUserDirectoryService() {
605: return userDirectoryService;
606: }
607:
608: public void setUserDirectoryService(
609: UserDirectoryService userDirectoryService) {
610: this .userDirectoryService = userDirectoryService;
611: }
612:
613: public SiteService getSiteService() {
614: return siteService;
615: }
616:
617: public void setSiteService(SiteService siteService) {
618: this .siteService = siteService;
619: }
620:
621: public Map getSiteTypes() {
622: return siteTypes;
623: }
624:
625: public void setSiteTypes(Map siteTypes) {
626: this .siteTypes = siteTypes;
627: }
628:
629: public org.sakaiproject.metaobj.security.AuthorizationFacade getSakaiAuthzManager() {
630: return sakaiAuthzManager;
631: }
632:
633: public void setSakaiAuthzManager(
634: org.sakaiproject.metaobj.security.AuthorizationFacade sakaiAuthzManager) {
635: this .sakaiAuthzManager = sakaiAuthzManager;
636: }
637:
638: public org.theospi.portfolio.security.AuthorizationFacade getOspAuthzManager() {
639: return ospAuthzManager;
640: }
641:
642: public void setOspAuthzManager(
643: org.theospi.portfolio.security.AuthorizationFacade ospAuthzManager) {
644: this .ospAuthzManager = ospAuthzManager;
645: }
646:
647: public boolean isDisplayToolCategories() {
648: return displayToolCategories;
649: }
650:
651: public void setDisplayToolCategories(boolean displayToolCategories) {
652: this .displayToolCategories = displayToolCategories;
653: }
654:
655: public boolean isDisplaySiteTypes() {
656: return displaySiteTypes;
657: }
658:
659: public Collection getCategoriesInNeedOfFiles() {
660: if (isCleanedDb()) {
661: return loadPages(getHibernateTemplate().loadAll(
662: ToolCategory.class));
663: } else {
664: return null;
665: }
666: }
667:
668: protected Collection loadPages(List list) {
669: for (Iterator<ToolCategory> i = list.iterator(); i.hasNext();) {
670: i.next().getPages().size();
671: }
672:
673: return list;
674: }
675:
676: public void saveToolCategories(Collection toolCategories) {
677: getHibernateTemplate().saveOrUpdateAll(toolCategories);
678: }
679:
680: public void setDisplaySiteTypes(boolean displaySiteTypes) {
681: this .displaySiteTypes = displaySiteTypes;
682: }
683:
684: public IdManager getIdManager() {
685: return idManager;
686: }
687:
688: public void setIdManager(IdManager idManager) {
689: this .idManager = idManager;
690: }
691:
692: public boolean isUseDb() {
693: return useDb;
694: }
695:
696: public byte[] getCategoryPage(String href) {
697: List categories = (List) getHibernateTemplate().find(
698: " from ToolCategory where type_key=?", href);
699:
700: if (categories.size() > 0) {
701: ToolCategory cat = (ToolCategory) categories.get(0);
702:
703: String localeStr = rl.getLocale().toString();
704:
705: if (cat.getPages().get(localeStr) != null) {
706: return (byte[]) cat.getPages().get(localeStr);
707: }
708:
709: localeStr = localeStr.replaceFirst("_.*", "");
710:
711: if (cat.getPages().get(localeStr) != null) {
712: return (byte[]) cat.getPages().get(localeStr);
713: }
714:
715: return (byte[]) cat.getPages().get("");
716: }
717:
718: return new byte[0];
719: }
720:
721: public void setUseDb(boolean useDb) {
722: this .useDb = useDb;
723: }
724:
725: public boolean isReloadDb() {
726: return reloadDb;
727: }
728:
729: public void setReloadDb(boolean reloadDb) {
730: this .reloadDb = reloadDb;
731: }
732:
733: public boolean isCleanedDb() {
734: return cleanedDb;
735: }
736:
737: public void setCleanedDb(boolean cleanedDb) {
738: this .cleanedDb = cleanedDb;
739: }
740:
741: public boolean isAutoDdl() {
742: return autoDdl;
743: }
744:
745: public void setAutoDdl(boolean autoDdl) {
746: this.autoDdl = autoDdl;
747: }
748: }
|