View Javadoc

1   package org.apache.jetspeed.om.dbpsml;
2   
3   // JDK classes
4   import java.util.List;
5   import java.sql.Connection;
6   
7   //Torque classes
8   import org.apache.torque.util.Criteria;
9   import org.apache.torque.util.BasePeer;
10  
11  import org.apache.jetspeed.om.security.Role;
12  import org.apache.jetspeed.om.profile.Profile;
13  import org.apache.jetspeed.om.profile.ProfileLocator;
14  import org.apache.jetspeed.services.psmlmanager.db.DBOperations;
15  import org.apache.jetspeed.services.psmlmanager.db.DBUtils;
16  import org.apache.jetspeed.services.psmlmanager.db.DatabasePsmlManager;
17  import org.apache.jetspeed.services.PsmlManager;
18  import org.apache.jetspeed.services.Profiler;
19  
20  /***
21    * The skeleton for this class was autogenerated by Torque on:
22    *
23    * [Mon Sep 10 13:30:53 PDT 2001]
24    *
25    *  You should add additional methods to this class to meet the
26    *  application requirements.  This class will only be generated as
27    *  long as it does not already exist in the output directory.
28    *
29    * @author <a href="mailto:adambalk@cisco.com">Atul Dambalkar</a>
30    * @version $Id: JetspeedRoleProfilePeer.java,v 1.13 2003/08/13 04:18:17 taylor Exp $
31    */
32  public class JetspeedRoleProfilePeer
33      extends org.apache.jetspeed.om.dbpsml.BaseJetspeedRoleProfilePeer
34      implements DBOperations
35  {
36  
37      /***
38       * Default constructor.
39       */
40      public JetspeedRoleProfilePeer() {}
41  
42  
43      /***
44       * Insert a role profile record in the database table.
45       *
46       * @param profile Profile object that will be inserted in the database
47       * @param connection A database connection to use
48       */
49      public void insert(Profile profile, Connection connection) throws Exception
50      {
51          doInsertOrUpdate(profile, INSERT, connection);
52      }
53  
54      /***
55       * Update role profile record from the database table.
56       *
57       * @param profile Profile object that will be deleted from the database
58       * @param connection A database connection to use
59       */
60      public void update(Profile profile, Connection connection) throws Exception
61      {
62          doInsertOrUpdate(profile, UPDATE, connection);
63      }
64  
65  
66      private void doInsertOrUpdate(Profile profile, int operation, Connection connection)
67                             throws Exception
68      {
69          JetspeedRoleProfile roleProfile = new JetspeedRoleProfile();
70          DatabasePsmlManager service = (DatabasePsmlManager)PsmlManager.getService();
71  
72          roleProfile.setRoleName(profile.getRole().getName());
73          roleProfile.setMediaType(profile.getMediaType());
74          
75  		String language = profile.getLanguage();
76  		if(language != null && (!language.equals("-1")))
77  		{
78  			roleProfile.setLanguage(language);
79  		}
80  		else
81  		{
82  			roleProfile.setLanguage(null);
83  		}
84  
85  		String country = profile.getCountry();
86  		if(country != null && (!country.equals("-1")))
87  		{
88  			roleProfile.setCountry(country);
89  		}
90  		else
91  		{
92  			roleProfile.setCountry(null);
93  		}
94  
95          String name = profile.getName();
96          if (name == null || name.equals(""))
97          {
98              profile.setName(Profiler.FULL_DEFAULT_PROFILE);
99          }
100         else if (!name.endsWith(Profiler.DEFAULT_EXTENSION))
101         {
102             profile.setName(name + Profiler.DEFAULT_EXTENSION);
103         }
104         roleProfile.setPage(profile.getName());
105         roleProfile.setProfile(DBUtils.portletsToBytes(
106                                        profile.getDocument().getPortlets(), service.getMapping()));
107 
108         if (operation == INSERT)
109         {
110             super.doInsert(roleProfile, connection);
111         }
112         else if (operation == UPDATE)
113         {
114             Criteria values = buildCriteria(roleProfile);
115             Criteria select = buildCriteria(roleProfile);
116             select.remove(PROFILE);
117             BasePeer.doUpdate( select, values, connection );
118         }
119     }
120 
121     /***
122      * Delete role profile record from the database table.
123      *
124      * @param profile Profile object that will be deleted from the database
125      * @param connection A database connection to use
126      */
127     public void delete(ProfileLocator locator, Connection connection) throws Exception
128     {
129         super.doDelete(buildCriteria(locator), connection);
130     }
131 
132     /***
133      * Select role profile record from the database table for the given
134      * locator object.
135      *
136      * @param locator ProfileLocator object that will be used to select required
137      * profile from the database
138      * @param connection A database connection to use
139      * @return List of records that statisfy the given locator criteria.
140      */
141     public List select(ProfileLocator locator, Connection connection) throws Exception
142     {
143         return super.doSelect(buildCriteria(locator), connection);
144     }
145 
146     /***
147      * Select role profile record from the database table for the given
148      * locator object and return list ordered by primary key.
149      *
150      * @param locator ProfileLocator object that will be used to select required
151      * profile from the database
152      * @param connection A database connection to use
153      * @return List of records that statisfy the given locator criteria.
154      */
155     public List selectOrdered(ProfileLocator locator, Connection connection) throws Exception
156     {
157         Criteria criteria = buildCriteria(locator);
158 
159         criteria.addAscendingOrderByColumn(ROLE_NAME);
160         criteria.addAscendingOrderByColumn(MEDIA_TYPE);
161         criteria.addAscendingOrderByColumn(LANGUAGE);
162         criteria.addAscendingOrderByColumn(COUNTRY);
163         criteria.addAscendingOrderByColumn(PAGE);
164 
165         return super.doSelect(criteria, connection);
166     }
167 
168     /***
169      * Delete all records from the database table for a role.
170      *
171      * @param role Role object for which all the records will be deleted from the database
172      * @param connection A database connection to use
173      */
174     public void delete(Role role, Connection connection) throws Exception
175     {
176         Criteria criteria = new Criteria();
177 
178         criteria.add(ROLE_NAME, role.getName());
179 
180         super.doDelete(criteria, connection);
181     }
182 
183     /*
184      * Build criteria for selecting, deleting groups
185      *
186      */
187     protected Criteria buildCriteria(ProfileLocator locator)
188     {
189         Criteria criteria = new Criteria();
190         String mediaType = locator.getMediaType();
191         String language = locator.getLanguage();
192         String country = locator.getCountry();
193         String pageName = locator.getName();
194         String roleName = null;
195 
196         Role role = locator.getRole();
197         if (role != null) {
198             roleName = role.getName();
199         }
200 
201         if (roleName != null && roleName.length() > 0)
202         {
203             criteria.add(ROLE_NAME, roleName);
204         }
205 
206         if (pageName != null && pageName.length() > 0)
207         {
208             criteria.add(PAGE, pageName);
209         }
210 
211         if (mediaType != null && mediaType.length() > 0)
212         {
213             criteria.add(MEDIA_TYPE, locator.getMediaType());
214         }
215 
216 		if (language != null && language.length() > 0 && (!language.equals("-1")))
217 		{
218 			criteria.add(LANGUAGE, language);
219 		} 
220 		else if(language != null && language.equals("-1"))
221 		{
222 			criteria.add(LANGUAGE, null);	
223 		}
224 
225 		if (country != null && country.length() > 0 && (!country.equals("-1")))
226 		{
227 			criteria.add(COUNTRY, country);
228 		}
229 		else if(country != null && country.equals("-1"))
230 		{
231 			criteria.add(COUNTRY, null);	
232 		}
233 
234         return criteria;
235     }
236 }
237