001: /**
002: * Copyright 2005 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.admin.console.communities;
013:
014: import java.util.*;
015: import java.util.logging.Level;
016: import java.io.*;
017: import java.text.*;
018:
019: import javax.faces.context.FacesContext;
020: import javax.faces.application.FacesMessage;
021: import javax.faces.component.UIComponent;
022: import javax.faces.validator.*;
023: import javax.faces.el.ValueBinding;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.management.*;
026:
027: import com.sun.web.ui.model.*;
028: import com.sun.web.ui.event.*;
029: import com.sun.web.ui.component.*;
030:
031: import com.sun.data.provider.*;
032: import com.sun.data.provider.impl.ObjectListDataProvider;
033:
034: import com.sun.cacao.agent.JmxClient;
035:
036: import com.sun.portal.admin.common.util.AdminClientUtil;
037: import com.sun.portal.admin.console.common.PSBaseBean;
038:
039: public class CreateCommunityBean extends PSBaseBean {
040:
041: public String name = "";
042: public String description = "";
043: public String owner = "";
044: public String category = "";
045: public String template = "";
046: public String unlisted = "false";
047: public String membershipRestricted = "false";
048: public String secured = "false";
049: private Locale locale = null;
050: private Option[] availableTemplates = null;
051:
052: public CreateCommunityBean() {
053: setSessionAttribute("createcommunitycategorypopup.mode",
054: "browse");
055: setSessionAttribute("createcommunitycategorypopup.category",
056: null);
057:
058: FacesContext context = FacesContext.getCurrentInstance();
059: HttpServletRequest httpsr = (HttpServletRequest) context
060: .getExternalContext().getRequest();
061: locale = httpsr.getLocale();
062: if (locale == null) {
063: locale = Locale.getDefault();
064: }
065:
066: retrieveAvailableTemplates();
067: }
068:
069: public boolean getShowError() {
070: String value = (String) getSessionAttribute("createcommunity.showerror");
071: if (value == null || value.equals("false")) {
072: return false;
073: } else {
074: return true;
075: }
076: }
077:
078: public String getName() {
079: return name;
080: }
081:
082: public void setName(String name) {
083: this .name = name;
084: }
085:
086: public String getDescription() {
087: return description;
088: }
089:
090: public void setDescription(String description) {
091: this .description = description;
092: }
093:
094: public String getOwner() {
095: return owner;
096: }
097:
098: public void setOwner(String owner) {
099: this .owner = owner;
100: }
101:
102: public String getCategory() {
103: return category;
104: }
105:
106: public void setCategory(String category) {
107: this .category = category;
108: }
109:
110: public Option[] getAvailableTemplates() {
111: return availableTemplates;
112: }
113:
114: public String getTemplate() {
115: return template;
116: }
117:
118: public void setTemplate(String template) {
119: this .template = template;
120: }
121:
122: public Option[] getAvailableListing() {
123: Option[] options = new Option[2];
124: options[0] = new Option("false", getLocalizedString(
125: "communities", "createcommunity.unlisted.false"));
126: options[1] = new Option("true", getLocalizedString(
127: "communities", "createcommunity.unlisted.true"));
128: return options;
129: }
130:
131: public String getListing() {
132: return unlisted;
133: }
134:
135: public void setListing(String unlisted) {
136: this .unlisted = unlisted;
137: }
138:
139: public Option[] getAvailableMembership() {
140: Option[] options = new Option[2];
141: options[0] = new Option("false", getLocalizedString(
142: "communities",
143: "createcommunity.membershiprestricted.false"));
144: options[1] = new Option("true", getLocalizedString(
145: "communities",
146: "createcommunity.membershiprestricted.true"));
147: return options;
148: }
149:
150: public String getMembership() {
151: return membershipRestricted;
152: }
153:
154: public void setMembership(String membershipRestricted) {
155: this .membershipRestricted = membershipRestricted;
156: }
157:
158: public Option[] getAvailableAccess() {
159: Option[] options = new Option[2];
160: options[0] = new Option("false", getLocalizedString(
161: "communities", "createcommunity.secured.false"));
162: options[1] = new Option("true", getLocalizedString(
163: "communities", "createcommunity.secured.true"));
164: return options;
165: }
166:
167: public String getAccess() {
168: return secured;
169: }
170:
171: public void setAccess(String secured) {
172: this .secured = secured;
173: }
174:
175: public void validate(FacesContext fc, UIComponent component,
176: Object value) throws ValidatorException {
177: String id = component.getId();
178: if (id.equals("name")) {
179: String s = (String) value;
180: if (!isAlphaNumeric(s)) {
181: throw new ValidatorException(new FacesMessage(
182: FacesMessage.SEVERITY_ERROR,
183: getLocalizedString("communities",
184: "communities.error.error"),
185: getLocalizedString("communities",
186: "communities.error.alphanumeric")));
187: }
188: if (s.length() > 20) {
189: throw new ValidatorException(new FacesMessage(
190: FacesMessage.SEVERITY_ERROR,
191: getLocalizedString("communities",
192: "communities.error.error"),
193: getLocalizedString("communities",
194: "communities.error.nametoolong")));
195: }
196: String message = null;
197: try {
198: LinkedList path = new LinkedList();
199: path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
200: path
201: .addFirst((String) getSessionAttribute(ATTR_SELECTED_PORTAL));
202: path.addFirst("CommunityManager");
203: ObjectName on = AdminClientUtil
204: .getResourceMBeanObjectName(
205: AdminClientUtil.COMMUNITYMANAGER_MBEAN_TYPE,
206: path);
207: Object[] params = { s };
208: String[] signatures = { "java.lang.String" };
209: ArrayList data = (ArrayList) getMBeanServerConnection()
210: .invoke(on, "searchForCommunities", params,
211: signatures);
212: if (data.size() > 0) {
213: message = getLocalizedString("communities",
214: "communities.error.duplicatecommunity");
215: }
216: } catch (Exception e) {
217: log(Level.SEVERE, "CreateCommunityBean.validate()", e);
218: message = getLocalizedString("communities",
219: "communities.error.generalfault");
220: }
221: if (message != null) {
222: throw new ValidatorException(new FacesMessage(
223: FacesMessage.SEVERITY_ERROR,
224: getLocalizedString("communities",
225: "communities.error.error"), message));
226: }
227: } else if (id.equals("description")) {
228: String s = (String) value;
229: if (s.length() > 256) {
230: throw new ValidatorException(
231: new FacesMessage(
232: FacesMessage.SEVERITY_ERROR,
233: getLocalizedString("communities",
234: "communities.error.error"),
235: getLocalizedString("communities",
236: "communities.error.descriptiontoolong")));
237: }
238: } else if (id.equals("owner")) {
239: String message = null;
240: try {
241: LinkedList path = new LinkedList();
242: path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
243: path.addFirst("amobjsearch");
244: ObjectName on = AdminClientUtil
245: .getResourceMBeanObjectName(
246: AdminClientUtil.AMOBJECTSEARCH_MBEAN_TYPE,
247: path);
248: Object[] params = { value };
249: String[] signatures = { "java.lang.String" };
250: Boolean result = (Boolean) getMBeanServerConnection()
251: .invoke(on, "isValidDN", params, signatures);
252: if (result.booleanValue() == false) {
253: message = getLocalizedString("communities",
254: "communities.error.invaliduserdn");
255: }
256: } catch (Exception e) {
257: log(Level.SEVERE,
258: "EditCommunityBean.validate() : Exception ", e);
259: message = getLocalizedString("communities",
260: "communities.error.generalfault");
261: }
262: if (message != null) {
263: throw new ValidatorException(new FacesMessage(
264: FacesMessage.SEVERITY_ERROR,
265: getLocalizedString("communities",
266: "communities.error.error"), message));
267: }
268: } else if (id.equals("category")) {
269: } else if (id.equals("template")) {
270: }
271: }
272:
273: public String create() {
274: try {
275: LinkedList path = new LinkedList();
276: path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
277: path
278: .addFirst((String) getSessionAttribute(ATTR_SELECTED_PORTAL));
279: path.addFirst("CommunityManager");
280: ObjectName on = AdminClientUtil.getResourceMBeanObjectName(
281: AdminClientUtil.COMMUNITYMANAGER_MBEAN_TYPE, path);
282: Object[] params = { name, description, owner, category,
283: template, unlisted, membershipRestricted, secured };
284: String[] signatures = { "java.lang.String",
285: "java.lang.String", "java.lang.String",
286: "java.lang.String", "java.lang.String",
287: "java.lang.String", "java.lang.String",
288: "java.lang.String" };
289: getMBeanServerConnection().invoke(on, "createCommunity",
290: params, signatures);
291: setSessionAttribute("createcommunity.showerror", "false");
292: return "gotoCommunitiesHome";
293: } catch (Exception e) {
294: log(Level.SEVERE,
295: "CreateCommunityBean.create() : Exception ", e);
296: setSessionAttribute("createcommunity.showerror", "true");
297: return null;
298: }
299: }
300:
301: public String cancel() {
302: return "gotoCommunitiesHome";
303: }
304:
305: private void retrieveAvailableTemplates() {
306: try {
307: LinkedList path = new LinkedList();
308: path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
309: path
310: .addFirst((String) getSessionAttribute(ATTR_SELECTED_PORTAL));
311: path.addFirst("CommunityManager");
312: ObjectName on = AdminClientUtil.getResourceMBeanObjectName(
313: AdminClientUtil.COMMUNITYMANAGER_MBEAN_TYPE, path);
314:
315: Object[] params = { locale.toString() };
316: String[] signatures = { "java.lang.String" };
317: ArrayList data = (ArrayList) getMBeanServerConnection()
318: .invoke(on, "getAvailableCommunityTemplates",
319: params, signatures);
320: availableTemplates = new Option[data.size() + 1];
321: availableTemplates[0] = new Option("", getLocalizedString(
322: "communities", "communities.general.select"));
323: for (int index = 0; index < data.size(); index++) {
324: Properties p = (Properties) data.get(index);
325: availableTemplates[index + 1] = new Option(p
326: .getProperty("id"), p.getProperty("name"));
327: }
328:
329: setSessionAttribute("createcommunity.showerror", "false");
330: } catch (Exception e) {
331: log(
332: Level.SEVERE,
333: "CreateCommunityBean.retrieveAvailableTemplates() : Exception ",
334: e);
335: setSessionAttribute("createcommunity.showerror", "true");
336: }
337: }
338:
339: public static boolean isAlphaNumeric(String value) {
340: for (int i = 0; i < value.length(); i++) {
341: char c[] = { value.charAt(i) };
342: String s = new String(c);
343: if (s.matches("[^a-zA-Z0-9]")) {
344: return false;
345: }
346: }
347: return true;
348: }
349:
350: }
|