KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > navigation > forms > FavoritesForm


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.navigation.forms;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import com.sslexplorer.core.CoreUtil;
32 import com.sslexplorer.navigation.AbstractFavoriteItem;
33 import com.sslexplorer.navigation.Favorite;
34 import com.sslexplorer.navigation.FavoriteResourceType;
35 import com.sslexplorer.navigation.WrappedFavoriteItem;
36 import com.sslexplorer.policyframework.NoPermissionException;
37 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
38 import com.sslexplorer.policyframework.ResourceType;
39 import com.sslexplorer.policyframework.ResourceUtil;
40 import com.sslexplorer.security.Constants;
41 import com.sslexplorer.security.LogonControllerFactory;
42 import com.sslexplorer.security.SessionInfo;
43 import com.sslexplorer.security.SystemDatabaseFactory;
44 import com.sslexplorer.security.User;
45 import com.sslexplorer.table.AbstractTableItemTableModel;
46 import com.sslexplorer.table.forms.AbstractPagerForm;
47 import com.sslexplorer.util.TicketGenerator;
48
49 /**
50  * Implementation of a {@link com.sslexplorer.table.actions.AbstractPagerAction}
51  * that provides a paged list of all the users configured favorites including
52  * both <i>User Favorites</i> and <i>Policy Favorites</i>.
53  *
54  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
55  */

56 public class FavoritesForm extends AbstractPagerForm {
57
58     final static Log log = LogFactory.getLog(FavoritesForm.class);
59
60     private String JavaDoc selectedView;
61
62     /**
63      * Constructor
64      */

65     public FavoritesForm() {
66         super(new FavoritesModel());
67     }
68
69     /**
70      * Initialise the form by loading all the favorites.
71      *
72      * @param request request
73      * @param defaultSortColumnId default sort column
74      */

75     public void initialize(HttpServletRequest JavaDoc request, String JavaDoc defaultSortColumnId) {
76         super.initialize(request.getSession(), defaultSortColumnId);
77         SessionInfo info = LogonControllerFactory.getInstance().getSessionInfo(request);
78         getModel().clear();
79         try {
80             User user = LogonControllerFactory.getInstance().getUser(request);
81             for (Iterator JavaDoc i = PolicyDatabaseFactory.getInstance().getResourceTypes(null).iterator(); i.hasNext();) {
82                 ResourceType rt = (ResourceType) i.next();
83                 if (rt instanceof FavoriteResourceType) {
84                     FavoriteResourceType frt = (FavoriteResourceType) rt;
85                     try {
86                         List JavaDoc fl = SystemDatabaseFactory.getInstance().getFavorites(frt.getResourceTypeId(), user);
87                         for (Iterator JavaDoc j = fl.iterator(); j.hasNext();) {
88                             Favorite f = (Favorite) j.next();
89                             try {
90                                 WrappedFavoriteItem wfi = frt.createWrappedFavoriteItem(f.getFavoriteKey(), request,
91                                                 AbstractFavoriteItem.USER_FAVORITE);
92                                 if (wfi == null) {
93                                     SystemDatabaseFactory.getInstance().removeFavorite(f.getType(), f.getFavoriteKey(),
94                                                     f.getUsername());
95                                 }
96                                 try {
97                                     ResourceUtil.checkResourceAccessRights(wfi.getFavoriteItem().getResource(), info);
98                                     getModel().addItem(wfi);
99                                 } catch (NoPermissionException npe) {
100                                     // Skip
101
}
102                             } catch (Exception JavaDoc e) {
103                                 log.error("Failed to add user favorite " + f.getFavoriteKey() + ".", e);
104                             }
105                         }
106                     } catch (Exception JavaDoc e) {
107                         log.error("Failed to create user favorites for resource type " + frt.getResourceTypeId());
108                     }
109                     try {
110                         for (Iterator JavaDoc iter = ResourceUtil.filterResourceIdsForGlobalFavorites(
111                                         PolicyDatabaseFactory.getInstance().getGrantedResourcesOfType(user, frt), rt).iterator(); iter
112                                         .hasNext();) {
113                             Integer JavaDoc element = (Integer JavaDoc) iter.next();
114                             try {
115                                 WrappedFavoriteItem wfi = frt.createWrappedFavoriteItem(element.intValue(), request,
116                                                 AbstractFavoriteItem.GLOBAL_FAVORITE);
117                                 if (wfi == null) {
118                                     // TODO ? sahould we do soemthoing
119
// here?????????
120
} else {
121                                     if (!getModel().contains(wfi)) {
122                                         try {
123                                             ResourceUtil.checkResourceAccessRights(wfi.getFavoriteItem().getResource(), info);
124                                             getModel().addItem(wfi);
125                                         } catch (NoPermissionException npe) {
126                                             // Skip
127
}
128                                     }
129                                 }
130                             } catch (Exception JavaDoc e) {
131                                 log.error("Failed to add policy favorite for resource " + element.intValue() + ".", e);
132                             }
133                         }
134                     } catch (Exception JavaDoc e) {
135                         log.error("Failed to create policy favorites for resource type " + frt.getResourceTypeId());
136                     }
137
138                 }
139             }
140         } catch (Exception JavaDoc e) {
141             log.error("Failed to create favorite items.", e);
142         }
143         checkSort();
144         getPager().rebuild(getFilterText());
145         /*
146          * Store a ticket for use with launching a web folder without
147          * authenticating. This ticket will only be available until it is used
148          */

149         if (request.getSession().getAttribute(Constants.WEB_FOLDER_LAUNCH_TICKET) == null) {
150             request.getSession().setAttribute(Constants.WEB_FOLDER_LAUNCH_TICKET,
151                             TicketGenerator.getInstance().generateUniqueTicket("W", 6));
152         }
153
154     }
155
156     /**
157      * Check the selected view
158      *
159      * @param request
160      * @param response
161      */

162     public void checkSelectedView(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
163         SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute(Constants.SESSION_INFO);
164         String JavaDoc defaultView = CoreUtil.getUsersProfileProperty(request.getSession(), "ui.defaultUserConsoleViewType", sessionInfo
165                         .getUser());
166         selectedView = CoreUtil.getCookieValue("ui_view_" + getModel().getId() + "_"
167                         + ((SessionInfo) request.getSession().getAttribute(Constants.SESSION_INFO)).getNavigationContext(),
168                         request, defaultView);
169     }
170
171     /**
172      * Get the selected view. This will be one of
173      * {@link com.sslexplorer.policyframework.forms.AbstractResourcesForm#ICONS_VIEW}
174      * or
175      * {@link com.sslexplorer.policyframework.forms.AbstractResourcesForm#LIST_VIEW}.
176      *
177      * @return selected view
178      */

179     public String JavaDoc getSelectedView() {
180         return selectedView;
181     }
182
183     /**
184      * Set the selected view. This will be one of
185      * {@link com.sslexplorer.policyframework.forms.AbstractResourcesForm#ICONS_VIEW}
186      * or
187      * {@link com.sslexplorer.policyframework.forms.AbstractResourcesForm#LIST_VIEW}.
188      *
189      * @param selectedView selected view
190      */

191     public void setSelectedView(String JavaDoc selectedView) {
192         this.selectedView = selectedView;
193     }
194
195     // Supporting classes
196

197     static class FavoritesModel extends AbstractTableItemTableModel {
198
199         public int getColumnWidth(int col) {
200             return 0;
201         }
202
203         public String JavaDoc getId() {
204             return "favorites";
205         }
206
207         public int getColumnCount() {
208             return 1;
209         }
210
211         public String JavaDoc getColumnName(int col) {
212             return "name";
213         }
214
215         public Class JavaDoc getColumnClass(int col) {
216             return String JavaDoc.class;
217         }
218
219     }
220
221 }
Popular Tags