KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > policyframework > actions > AbstractFavoriteResourcesDispatchAction


1 package com.sslexplorer.policyframework.actions;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import javax.servlet.http.HttpServletResponse JavaDoc;
9
10 import org.apache.struts.Globals;
11 import org.apache.struts.action.ActionForm;
12 import org.apache.struts.action.ActionForward;
13 import org.apache.struts.action.ActionMapping;
14 import org.apache.struts.action.ActionMessages;
15
16 import com.sslexplorer.core.BundleActionMessage;
17 import com.sslexplorer.navigation.Favorite;
18 import com.sslexplorer.navigation.FavoriteResourceType;
19 import com.sslexplorer.policyframework.Permission;
20 import com.sslexplorer.policyframework.PolicyConstants;
21 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
22 import com.sslexplorer.policyframework.PolicyUtil;
23 import com.sslexplorer.policyframework.Resource;
24 import com.sslexplorer.policyframework.ResourceType;
25 import com.sslexplorer.policyframework.ResourceUtil;
26 import com.sslexplorer.policyframework.forms.AbstractResourcesForm;
27 import com.sslexplorer.security.LogonControllerFactory;
28 import com.sslexplorer.security.SessionInfo;
29 import com.sslexplorer.security.SystemDatabase;
30 import com.sslexplorer.security.SystemDatabaseFactory;
31 import com.sslexplorer.security.User;
32
33 /**
34  * <p>
35  * Abstract class for favorites.
36  *
37  * @author James D Robinson <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
38  *
39  */

40 public abstract class AbstractFavoriteResourcesDispatchAction extends AbstractResourcesDispatchAction {
41
42
43     /**
44      * Construtor
45      */

46     public AbstractFavoriteResourcesDispatchAction() {
47         super();
48     }
49
50     /**
51      * Constructor for normal resource types that have the standard,
52      * Create / Edit / Assign, Edit / Assign, Delete and Assign permissions
53      *
54      * @param resourceType resource type
55      * @param requiresResources requires actual resources of type
56      */

57     public AbstractFavoriteResourcesDispatchAction(ResourceType resourceType, ResourceType requiresResources) {
58         this(resourceType, new Permission[] { PolicyConstants.PERM_EDIT_AND_ASSIGN, PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, PolicyConstants.PERM_DELETE, PolicyConstants.PERM_ASSIGN },
59                         PolicyConstants.PERM_EDIT_AND_ASSIGN, PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, PolicyConstants.PERM_DELETE,
60                         PolicyConstants.PERM_ASSIGN, requiresResources);
61     }
62
63     /**
64      * @param resourceType The resource type.
65      * @param requiredPermissions The required permissions
66      * @param editPermission The edit permission
67      * @param createPermission The create permission
68      * @param removePermission The remove permission
69      * @param assignPermission The assign permission
70      */

71     public AbstractFavoriteResourcesDispatchAction(ResourceType resourceType, Permission[] requiredPermissions,
72                                                    Permission editPermission, Permission createPermission,
73                                                    Permission removePermission, Permission assignPermission) {
74         super(resourceType, requiredPermissions, editPermission, createPermission, removePermission, assignPermission);
75     }
76
77     /**
78      * @param resourceType The resource type.
79      * @param requiredPermissions The required permissions.
80      * @param editPermission The edit permission.
81      * @param createPermission The create permission.
82      * @param removePermission The remove permission.
83      * @param assignPermission The assign permission.
84      * @param requiresResources The resource type required.
85      */

86     public AbstractFavoriteResourcesDispatchAction(ResourceType resourceType, Permission[] requiredPermissions,
87                                                    Permission editPermission, Permission createPermission,
88                                                    Permission removePermission,
89                                                    Permission assignPermission, ResourceType requiresResources) {
90         super(resourceType, requiredPermissions, editPermission, createPermission, removePermission, assignPermission, requiresResources);
91     }
92
93     /**
94      * Add the item as a <i>User Favorite</i>.
95      *
96      * @param mapping mapping
97      * @param form form
98      * @param request request
99      * @param response response
100      * @return forward
101      * @throws Exception
102      */

103     public ActionForward favorite(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
104                     throws Exception JavaDoc {
105         AbstractResourcesForm f = (AbstractResourcesForm) form;
106         ActionMessages msgs = new ActionMessages();
107         User user = LogonControllerFactory.getInstance().getUser(request);
108         SystemDatabase sdb = SystemDatabaseFactory.getInstance();
109         if (getSessionInfo(request).getNavigationContext() != SessionInfo.USER_CONSOLE_CONTEXT) {
110             throw new Exception JavaDoc("Favorites may only be set in the user console.");
111         }
112         PolicyUtil.checkPermission(PolicyConstants.FAVORITES_RESOURCE_TYPE, PolicyConstants.PERM_USE, request);
113         Resource resource = getResourceById(f.getSelectedResource());
114         if(ResourceUtil.filterResourceIdsForGlobalFavorites(PolicyDatabaseFactory.getInstance().getGrantedResourcesOfType(getSessionInfo(request).getUser(),
115             resource.getResourceType()), resource.getResourceType()).contains(new Integer JavaDoc(resource.getResourceId()))) {
116             throw new Exception JavaDoc("Cannot set a user favorite for items that have a policy favorite.");
117         }
118         if (sdb.getFavorite(getResourceType().getResourceTypeId(), user, f.getSelectedResource()) != null) {
119             msgs.add(Globals.ERROR_KEY, new BundleActionMessage("navigation", "addToFavorites.error.alreadyFavorite", resource
120                 .getResourceName()));
121             saveErrors(request, msgs);
122             return mapping.findForward("refresh");
123         }
124         sdb.addFavorite(getResourceType().getResourceTypeId(), f.getSelectedResource(), user.getPrincipalName());
125         msgs.add(Globals.MESSAGES_KEY, new BundleActionMessage("navigation", "addToFavorites.message.favoriteAdded", resource
126             .getResourceName()));
127         saveMessages(request, msgs);
128         return mapping.findForward("refresh");
129     }
130
131     /**
132      * Remove the item from the users favorites.
133      *
134      * @param mapping mapping
135      * @param form form
136      * @param request request
137      * @param response response
138      * @return forward
139      * @throws Exception
140      */

141     public ActionForward removeFavorite(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
142                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
143         AbstractResourcesForm f = (AbstractResourcesForm) form;
144         ActionMessages msgs = new ActionMessages();
145         User user = LogonControllerFactory.getInstance().getUser(request);
146         SystemDatabase sdb = SystemDatabaseFactory.getInstance();
147         if (getSessionInfo(request).getNavigationContext() != SessionInfo.USER_CONSOLE_CONTEXT) {
148             throw new Exception JavaDoc("Favorites may only be set in the user console.");
149         }
150         PolicyUtil.checkPermission(PolicyConstants.FAVORITES_RESOURCE_TYPE, PolicyConstants.PERM_USE, request);
151         Resource resource = getResourceById(f.getSelectedResource());
152         if(ResourceUtil.filterResourceIdsForGlobalFavorites(PolicyDatabaseFactory.getInstance().getGrantedResourcesOfType(getSessionInfo(request).getUser(),
153             resource.getResourceType()), resource.getResourceType()).contains(new Integer JavaDoc(resource.getResourceId()))) {
154             throw new Exception JavaDoc("Cannot remove a user favorite from items that have a policy favorite.");
155         }
156         if (sdb.getFavorite(getResourceType().getResourceTypeId(), user, f.getSelectedResource()) == null) {
157             msgs.add(Globals.ERROR_KEY, new BundleActionMessage("navigation", "removeFromFavorites.error.notFavorite", resource
158                 .getResourceName()));
159             saveErrors(request, msgs);
160             return mapping.findForward("refresh");
161         }
162         sdb.removeFavorite(getResourceType().getResourceTypeId(), f.getSelectedResource(), user.getPrincipalName());
163         msgs.add(Globals.MESSAGES_KEY, new BundleActionMessage("navigation", "removeFromFavorites.message.favoriteAdded", resource
164             .getResourceName()));
165         saveMessages(request, msgs);
166         return mapping.findForward("refresh");
167     }
168
169     /*
170      * (non-Javadoc)
171      *
172      * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping,
173      * org.apache.struts.action.ActionForm,
174      * javax.servlet.http.HttpServletRequest,
175      * javax.servlet.http.HttpServletResponse)
176      */

177     public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
178                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
179         // Get the favorites for this resource type and store the list in the
180
// form, this may then be used to determine if an item is a favorite
181
List JavaDoc<Integer JavaDoc> l = new ArrayList JavaDoc<Integer JavaDoc>();
182         List JavaDoc<Favorite> favorites = SystemDatabaseFactory.getInstance().getFavorites(getResourceType().getResourceTypeId(),
183             getSessionInfo(request).getUser());
184         for (Iterator JavaDoc i = favorites.iterator(); i.hasNext();) {
185             Favorite f = (Favorite) i.next();
186             l.add(new Integer JavaDoc(f.getFavoriteKey()));
187         }
188         ((AbstractResourcesForm) form).setUserFavorites(l);
189         ((AbstractResourcesForm) form).setGlobalFavorites(ResourceUtil.filterResourceIdsForGlobalFavorites(PolicyDatabaseFactory.getInstance()
190             .getGrantedResourcesOfType(getSessionInfo(request).getUser(), getResourceType()), getResourceType()));
191         return super.unspecified(mapping, form, request, response);
192     }
193     
194     /* (non-Javadoc)
195      * @see com.sslexplorer.policyframework.actions.AbstractResourcesDispatchAction#getResourceById(int)
196      */

197     public Resource getResourceById(int id) throws Exception JavaDoc {
198         return ((FavoriteResourceType)getResourceType()).getResourceById(id);
199     }
200
201 }
202
Popular Tags