KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > portal > community > impl > CommunityConfigServiceImpl


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.portal.community.impl;
6
7 import java.util.*;
8 import org.exoplatform.services.config.ConfigurationService;
9 import org.exoplatform.services.organization.Group;
10 import org.exoplatform.services.organization.Membership;
11 import org.exoplatform.services.organization.OrganizationService;
12 import org.exoplatform.services.portal.community.CommunityConfig;
13 import org.exoplatform.services.portal.community.CommunityConfigService;
14 import org.exoplatform.services.portal.community.CommunityNavigation;
15 import org.exoplatform.services.portal.community.CommunityPortal;
16 /**
17  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
18  * @since Dec 4, 2004
19  * @version $Id$
20  */

21 public class CommunityConfigServiceImpl implements CommunityConfigService{
22   private Map communityPortals_ ;
23   private Map communityNavigations_ ;
24   private OrganizationService orgService_ ;
25   private ConfigurationService confService_ ;
26   
27   public CommunityConfigServiceImpl(ConfigurationService confService,
28                                     OrganizationService orgService) throws Exception JavaDoc {
29     orgService_ = orgService ;
30     confService_ = confService ;
31     communityPortals_ = new HashMap() ;
32     communityNavigations_ = new HashMap() ;
33     CommunityConfig communityConfig =
34       (CommunityConfig)confService.getServiceConfiguration(CommunityConfigService.class);
35     populateConfiguration(communityConfig) ;
36   }
37   
38   private void populateConfiguration(CommunityConfig config) {
39     List list = config.getCommunityPortals() ;
40     for(int i = 0; i < list.size(); i++) {
41       CommunityPortal cp = (CommunityPortal) list.get(i) ;
42       communityPortals_.put(cp.getGroupId(), cp) ;
43     }
44     list = config.getCommunityNavigations() ;
45     for(int i = 0; i < list.size(); i++) {
46       CommunityNavigation cn = (CommunityNavigation) list.get(i) ;
47       communityNavigations_.put(cn.getGroupId(), cn) ;
48     }
49   }
50   
51   public CommunityPortal findCommunityPortal(String JavaDoc user) throws Exception JavaDoc {
52     Collection memberships = orgService_.findMembershipsByUser(user) ;
53     CommunityPortal found = null ;
54     Iterator mitr = memberships.iterator() ;
55     while(mitr.hasNext()) {
56       Membership m = (Membership) mitr.next() ;
57       Iterator i = communityPortals_.values().iterator() ;
58       while(i.hasNext()) {
59         CommunityPortal cp = (CommunityPortal) i.next();
60         if(m.getMembershipType().equals(cp.getMembership()) &&
61            m.getGroupId().equals(cp.getGroupId())) {
62           if(found == null) {
63             found = cp ;
64           } else {
65             if(cp.getPriority() > found.getPriority()) {
66               found = cp ;
67             }
68           }
69         }
70       }
71     }
72     return found;
73   }
74   
75   public CommunityPortal getCommunityPortal(Group group) throws Exception JavaDoc {
76     Iterator i = communityPortals_.values().iterator() ;
77     while(i.hasNext()) {
78       CommunityPortal cp = (CommunityPortal)i.next();
79       if(cp.getGroupId().equals(group.getId())) return cp ;
80     }
81     return null;
82   }
83
84   synchronized public void addCommunityPortal(CommunityPortal cp) throws Exception JavaDoc {
85     communityPortals_.put(cp.getGroupId(), cp) ;
86     confService_.saveServiceConfiguration(CommunityConfigService.class, createCommunityConfig());
87   }
88
89   synchronized public void removeCommunityPortal(CommunityPortal cp) throws Exception JavaDoc {
90     communityPortals_.remove(cp.getGroupId()) ;
91     confService_.saveServiceConfiguration(getClass(), createCommunityConfig());
92   }
93
94   public List findCommunityNavigation(String JavaDoc user) throws Exception JavaDoc {
95     List list = new ArrayList() ;
96     Collection memberships = orgService_.findMembershipsByUser(user) ;
97     Iterator mitr = memberships.iterator() ;
98     while(mitr.hasNext()) {
99       Membership m = (Membership) mitr.next() ;
100       Iterator i = communityNavigations_.values().iterator() ;
101       while(i.hasNext()) {
102         CommunityNavigation cn = (CommunityNavigation) i.next();
103         if(m.getMembershipType().equals(cn.getMembership()) &&
104            m.getGroupId().equals(cn.getGroupId())) {
105           list.add(cn) ;
106         }
107       }
108     }
109     return list;
110   }
111
112   public CommunityNavigation getCommunityNavigation(Group group) throws Exception JavaDoc {
113     Iterator i = communityNavigations_.values().iterator() ;
114     while(i.hasNext()) {
115       CommunityNavigation cn = (CommunityNavigation)i.next();
116       if(cn.getGroupId().equals(group.getId())) return cn ;
117     }
118     return null;
119   }
120
121   synchronized public void addCommunityNavigation(CommunityNavigation cn) throws Exception JavaDoc {
122     communityNavigations_.put(cn.getGroupId(), cn) ;
123     confService_.saveServiceConfiguration(getClass(), createCommunityConfig());
124   }
125
126   synchronized public void removeCommunityNavigation(CommunityNavigation cn) throws Exception JavaDoc {
127     communityNavigations_.remove(cn.getGroupId()) ;
128     confService_.saveServiceConfiguration(getClass(), createCommunityConfig());
129   }
130   
131   private CommunityConfig createCommunityConfig() {
132     CommunityConfig config = new CommunityConfig() ;
133     List portals = config.getCommunityPortals() ;
134     Iterator i = communityPortals_.values().iterator() ;
135     while(i.hasNext()) {
136       CommunityPortal cp = (CommunityPortal)i.next();
137       portals.add(cp) ;
138     }
139     
140     List navigations = config.getCommunityNavigations() ;
141     i = communityNavigations_.values().iterator() ;
142     while(i.hasNext()) {
143       CommunityNavigation cn = (CommunityNavigation)i.next();
144       navigations.add(cn) ;
145     }
146     return config ;
147   }
148 }
149
Popular Tags