KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > web > admin > services > ListServicesManagedBean


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13 package org.ejbca.ui.web.admin.services;
14
15 import java.io.IOException JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.faces.application.Application;
23 import javax.faces.context.FacesContext;
24 import javax.faces.el.ValueBinding;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.ejbca.core.model.services.ServiceConfiguration;
28 import org.ejbca.core.model.services.ServiceExistsException;
29 import org.ejbca.ui.web.admin.BaseManagedBean;
30 import org.ejbca.ui.web.admin.configuration.EjbcaJSFHelper;
31 import org.ejbca.ui.web.admin.configuration.SortableSelectItem;
32
33 /**
34  * Class used to manage the listservices.jsp page
35  * Contains and manages the available services
36  *
37  * @author Philip Vendil 2006 sep 29
38  *
39  * @version $Id: ListServicesManagedBean.java,v 1.4 2007/01/16 11:46:14 anatom Exp $
40  */

41 public class ListServicesManagedBean extends BaseManagedBean {
42     
43
44
45     private String JavaDoc selectedServiceName;
46     
47     private String JavaDoc newServiceName = "";
48
49     
50     public ListServicesManagedBean(){
51         
52
53     }
54
55     public String JavaDoc getSelectedServiceName() {
56         return selectedServiceName;
57     }
58
59     public void setSelectedServiceName(String JavaDoc string) {
60         selectedServiceName = string;
61     }
62
63     public List JavaDoc getAvailableServices() {
64         List JavaDoc availableServices = new ArrayList JavaDoc();
65         Collection JavaDoc availableServicesIds = EjbcaJSFHelper.getBean().getServiceSession().getAuthorizedServiceIds(getAdmin());
66         Iterator JavaDoc iter = availableServicesIds.iterator();
67         while(iter.hasNext()){
68             Integer JavaDoc id = (Integer JavaDoc) iter.next();
69             ServiceConfiguration serviceConfig = EjbcaJSFHelper.getBean().getServiceSession().getServiceConfiguration(getAdmin(), id.intValue());
70             String JavaDoc serviceName = EjbcaJSFHelper.getBean().getServiceSession().getServiceName(getAdmin(), id.intValue());
71             if(serviceConfig.isActive()){
72                 availableServices.add(new SortableSelectItem(serviceName, serviceName+ " (" + EjbcaJSFHelper.getBean().getText().get("ACTIVE") + ")"));
73             }else{
74                 availableServices.add(new SortableSelectItem(serviceName, serviceName + " (" + EjbcaJSFHelper.getBean().getText().get("INACTIVE") + ")"));
75             }
76         }
77         
78         Collections.sort(availableServices);
79      
80         return availableServices;
81     }
82
83
84     
85     public String JavaDoc editService(){
86         String JavaDoc retval = "editservice";
87         if(selectedServiceName != null){
88             try {
89                 getEditServiceBean().setServiceName(selectedServiceName);
90                 ServiceConfiguration serviceConf = EjbcaJSFHelper.getBean().getServiceSession().getService(getAdmin(), selectedServiceName);
91                 getEditServiceBean().setServiceConfiguration(serviceConf);
92             } catch (IOException JavaDoc e) {
93                 addNonTranslatedErrorMessage((String JavaDoc) EjbcaJSFHelper.getBean().getText().get("ERROREDITINGSERVICE") + " " + e.getMessage());
94             }
95         }else{
96             addErrorMessage("YOUHAVETOSELECTASERVICE");
97             retval = "listservices";
98         }
99         
100         newServiceName = "";
101         return retval;
102     }
103     
104     public String JavaDoc deleteService(){
105         if(selectedServiceName != null){
106           EjbcaJSFHelper.getBean().getServiceSession().removeService(getAdmin(), selectedServiceName);
107         }else{
108             addErrorMessage("YOUHAVETOSELECTASERVICE");
109         }
110         
111         newServiceName = "";
112         return "listservices";
113     }
114     
115     public String JavaDoc renameService(){
116         if(selectedServiceName == null){
117             addErrorMessage("YOUHAVETOSELECTASERVICE");
118         }else if (newServiceName.trim().equals("")) {
119             addErrorMessage("YOUHAVETOENTERASERVICE");
120         }else if (errorInServiceName(newServiceName)) {
121             addErrorMessage("THECHARACTERSARENTALLOWED");
122         }else{
123             try {
124                 EjbcaJSFHelper.getBean().getServiceSession().renameService(getAdmin(), selectedServiceName, newServiceName);
125             } catch (ServiceExistsException e) {
126                 addNonTranslatedErrorMessage((String JavaDoc) EjbcaJSFHelper.getBean().getText().get("SERVICENAMEALREADYEXISTS"));
127             }
128         }
129
130         newServiceName = "";
131         return "listservices";
132     }
133     
134
135
136     public String JavaDoc addService(){
137         if (newServiceName.trim().equals("")) {
138             addErrorMessage("YOUHAVETOENTERASERVICE");
139         }else if (errorInServiceName(newServiceName)) {
140             addErrorMessage("THECHARACTERSARENTALLOWED");
141         }else{
142             try {
143                 ServiceConfiguration serviceConfig = new ServiceConfiguration();
144                 EjbcaJSFHelper.getBean().getServiceSession().addService(getAdmin(), newServiceName, serviceConfig);
145                 getEditServiceBean().setServiceConfiguration(serviceConfig);
146                 getEditServiceBean().setServiceName(newServiceName);
147             } catch (ServiceExistsException e) {
148                 addNonTranslatedErrorMessage((String JavaDoc) EjbcaJSFHelper.getBean().getText().get("SERVICENAMEALREADYEXISTS"));
149             } catch (IOException JavaDoc e) {
150                 addNonTranslatedErrorMessage((String JavaDoc) EjbcaJSFHelper.getBean().getText().get("ERRORADDINGSERVICE") + e.getMessage());
151             }
152         }
153         
154         newServiceName = "";
155         return "listservices";
156     }
157     
158     public String JavaDoc cloneService(){
159         if(selectedServiceName == null){
160             addErrorMessage("YOUHAVETOSELECTASERVICE");
161         }else if (newServiceName.trim().equals("")) {
162             addErrorMessage("YOUHAVETOENTERASERVICE");
163         }else if (errorInServiceName(newServiceName)) {
164             addErrorMessage("THECHARACTERSARENTALLOWED");
165         }else{
166             try {
167                 EjbcaJSFHelper.getBean().getServiceSession().cloneService(getAdmin(), selectedServiceName, newServiceName);
168             } catch (ServiceExistsException e) {
169                 addErrorMessage("SERVICENAMEALREADYEXISTS");
170             }
171         }
172         
173         newServiceName = "";
174         return "listservices";
175     }
176
177     /**
178      * @return the newServiceName
179      */

180     public String JavaDoc getNewServiceName() {
181         return newServiceName;
182     }
183
184     /**
185      * @param newServiceName the newServiceName to set
186      */

187     public void setNewServiceName(String JavaDoc newServiceName) {
188         this.newServiceName = newServiceName;
189     }
190
191     /**
192      * returns true if the is a faulty service name.
193      * @param newServiceName
194      */

195     private boolean errorInServiceName(String JavaDoc newServiceName) {
196         return StringUtils.contains(newServiceName, ";");
197     }
198
199     private EditServiceManagedBean getEditServiceBean(){
200         FacesContext context = FacesContext.getCurrentInstance();
201         Application app = context.getApplication();
202         ValueBinding binding = app.createValueBinding("#{editService}");
203         Object JavaDoc value = binding.getValue(context);
204         return (EditServiceManagedBean) value;
205     }
206     
207 }
208
Popular Tags