1 5 package org.exoplatform.portlets.user.component; 6 7 import java.util.ArrayList ; 8 import java.util.Collection ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 import org.exoplatform.faces.application.ExoFacesMessage; 12 import org.exoplatform.faces.core.component.*; 13 import org.exoplatform.faces.core.component.UISelectBox; 14 import org.exoplatform.faces.core.component.UISimpleForm; 15 import org.exoplatform.faces.core.component.UIStringInput; 16 import org.exoplatform.faces.core.component.UITextArea; 17 import org.exoplatform.faces.core.component.model.*; 18 import org.exoplatform.faces.core.event.ExoActionEvent; 19 import org.exoplatform.faces.core.event.ExoActionListener; 20 import org.exoplatform.faces.user.validator.ValidUserValidator; 21 import org.exoplatform.services.organization.MembershipType; 22 import org.exoplatform.services.organization.Group; 23 import org.exoplatform.services.organization.OrganizationService; 24 import org.exoplatform.services.portal.community.CommunityConfigService; 25 import org.exoplatform.services.portal.community.CommunityPortal; 26 32 public class UICommunityPortalForm extends UISimpleForm { 33 static public List PRIORITIES ; 34 static { 35 PRIORITIES = new ArrayList () ; 36 for(int i = 1; i <= 10; i++) { 37 String num = Integer.toString(i) ; 38 PRIORITIES.add(new SelectItem(num, num)) ; 39 } 40 } 41 42 private UISelectBox membershipInput_ ; 43 private UIStringInput portalInput_ ; 44 private UISelectBox priorityInput_ ; 45 private UITextArea descriptionInput_ ; 46 private CommunityConfigService service_ ; 47 private CommunityPortal communityPortal_ ; 48 49 public UICommunityPortalForm(CommunityConfigService service, 50 OrganizationService orgService) throws Exception { 51 super("communityPortalForm", "post", null) ; 52 service_ = service ; 53 Collection memberships = orgService.findMembershipTypes() ; 54 Iterator i = memberships.iterator() ; 55 List mtypes = new ArrayList (10) ; 56 while(i.hasNext()) { 57 MembershipType mt = (MembershipType) i.next(); 58 String name = mt.getName() ; 59 mtypes.add(new SelectItem(name, name)) ; 60 } 61 membershipInput_ = new UISelectBox("membership", "", mtypes) ; 62 portalInput_ = new UIStringInput("portal", ""). 63 addValidator(ValidUserValidator.class) ; 64 priorityInput_ = new UISelectBox("priority", "1", PRIORITIES) ; 65 descriptionInput_ = new UITextArea("description", "") ; 66 add(new HeaderRow(). 67 add(new Cell("#{UICommunityPortalForm.header.community-portal}"). 68 addColspan("2"))); 69 add(new Row(). 70 add(new LabelCell("#{UICommunityPortalForm.label.membership}")). 71 add(new ComponentCell(this, membershipInput_))); 72 add(new Row(). 73 add(new LabelCell("#{UICommunityPortalForm.label.portal}")). 74 add(new ComponentCell(this, portalInput_))); 75 add(new Row(). 76 add(new LabelCell("#{UICommunityPortalForm.label.priority}")). 77 add(new ComponentCell(this, priorityInput_))); 78 add(new Row(). 79 add(new LabelCell("#{UICommunityPortalForm.label.description}")). 80 add(new ComponentCell(this, descriptionInput_))); 81 add(new Row(). 82 add(new ListComponentCell(). 83 add(new FormButton("#{UICommunityPortalForm.button.save}", SAVE_ACTION)). 84 add(new FormButton("#{UICommunityPortalForm.button.cancel}", CANCEL_ACTION)). 85 addColspan("2").addAlign("center"))) ; 86 87 addActionListener(SaveUpdateListener.class, SAVE_ACTION) ; 88 addActionListener(CancelActionListener.class, CANCEL_ACTION) ; 89 } 90 91 public void setCommunityPortal(CommunityPortal cp) { 92 communityPortal_ = cp; 93 if(cp == null) { 94 membershipInput_.setValue("") ; 95 portalInput_.setValue("") ; 96 priorityInput_.setValue("5") ; 97 descriptionInput_.setValue("") ; 98 } else { 99 membershipInput_.setValue(cp.getMembership()) ; 100 portalInput_.setValue(cp.getPortal()) ; 101 priorityInput_.setValue(Integer.toString(cp.getPriority())) ; 102 descriptionInput_.setValue(cp.getDescription()) ; 103 } 104 } 105 106 static public class SaveUpdateListener extends ExoActionListener { 107 public void execute(ExoActionEvent event) throws Exception { 108 UICommunityPortalForm uiForm = (UICommunityPortalForm) event.getComponent() ; 109 CommunityPortal cp = uiForm.communityPortal_ ; 110 if(cp == null) { 111 UIGroupExplorer uiExplorer = 112 (UIGroupExplorer) uiForm.getAncestorOfType(UIGroupExplorer.class) ; 113 Group currentGroup = uiExplorer.getCurrentGroup() ; 114 if(currentGroup == null) { 115 InformationProvider iprovider = findInformationProvider(uiForm); 116 iprovider.addMessage(new ExoFacesMessage("#{UICommunityPortalForm.msg.no-group}")) ; 117 uiForm.setRenderedSibling(UIGroupCommunityInfo.class) ; 118 return ; 119 } 120 cp = new CommunityPortal() ; 121 cp.setGroupId(currentGroup.getId()) ; 122 } 123 cp.setMembership(uiForm.membershipInput_.getValue()) ; 124 cp.setPortal(uiForm.portalInput_.getValue()) ; 125 cp.setPriority(Integer.parseInt(uiForm.priorityInput_.getValue())) ; 126 cp.setDescription(uiForm.descriptionInput_.getValue()) ; 127 uiForm.service_.addCommunityPortal(cp) ; 128 UIGroupCommunityInfo uiInfo = 129 (UIGroupCommunityInfo) uiForm.getSibling(UIGroupCommunityInfo.class); 130 uiInfo.setCommunityPortal(cp) ; 131 uiForm.setRenderedSibling(UIGroupCommunityInfo.class) ; 132 } 133 } 134 135 static public class CancelActionListener extends ExoActionListener { 136 public void execute(ExoActionEvent event) throws Exception { 137 UICommunityPortalForm uiForm = (UICommunityPortalForm) event.getComponent() ; 138 uiForm.setRenderedSibling(UIGroupCommunityInfo.class) ; 139 } 140 } 141 } | Popular Tags |