You are on page 1of 8

MIDDLEWARE

ADF
// print the roles of the current user 1. for ( String role : ADFContext.getCurrent().getSecurityContext().getUserRoles() ) { 2. System.out.println("role "+role); 3. } 4. 5. 6. // get the ADF security context and test if the user has the role users 7. SecurityContext sec = ADFContext.getCurrent().getSecurityContext(); 8. if ( sec.isUserInRole("users") ) { 9. } 10. // is the user valid 11. public boolean isAuthenticated() { 12. return ADFContext.getCurrent().getSecurityContext().isAuthenticated(); 13. } 14. // return the user 15. public String getCurrentUser() { 16. return ADFContext.getCurrent().getSecurityContext().getUserName(); 17. } 18. 19. 20. // get the binding container 21. BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry(); 22. 23. // get an ADF attributevalue from the ADF page definitions 24. AttributeBinding attr = (AttributeBinding)bindings.getControlBinding("test"); 25. attr.setInputValue("test"); 26. 27. // get an Action or MethodAction 28. OperationBinding method = bindings.getOperationBinding("methodAction"); 29. method.execute(); 30. List errors = method.getErrors(); 31. 32. method = bindings.getOperationBinding("methodAction"); 33. Map paramsMap = method.getParamsMap(); 34. paramsMap.put("param","value") ; 35. method.execute();

36. 37. 38. // Get the data from an ADF tree or table 39. DCBindingContainer dcBindings = (DCBindingContainer)BindingContext.getCurrent().getC urrentBindingsEntry(); 40. 41. FacesCtrlHierBinding treeData = (FacesCtrlHierBinding)bc.getControlBinding("tree"); 42. Row[] rows = treeData.getAllRowsInRange(); 43. 44. // Get a attribute value of the current row of iterator 45. DCIteratorBinding iterBind= (DCIteratorBinding)dcBindings.get("testIterator"); 46. String attribute = (String)iterBind.getCurrentRow().getAttribute("field1"); 47. 48. // Get the error 49. String error = iterBind.getError().getMessage(); 50. 51. 52. // refresh the iterator 53. bindings.refreshControl(); 54. iterBind.executeQuery(); 55. iterBind.refresh(DCIteratorBinding.RANGESIZE_UNLIMITED); 56. 57. // Get all the rows of a iterator 58. Row[] rows = iterBind.getAllRowsInRange(); 59. TestData dataRow = null; 60. for (Row row : rows) { 61. dataRow = (TestData)((DCDataRow)row).getDataProvider(); 62. } 63. 64. // Get the current row of a iterator , a different way 65. FacesContext ctx = FacesContext.getCurrentInstance(); 66. ExpressionFactory ef = ctx.getApplication().getExpressionFactory(); 67. ValueExpression ve = ef.createValueExpression(ctx.getELContext(), "#{bindings.testIter.cur rentRow.dataProvider}", TestHead.class); 68. TestHead test = (TestHead)ve.getValue(ctx.getELContext()); 69. 70. // Get a session bean 71. FacesContext ctx = FacesContext.getCurrentInstance(); 72. ExpressionFactory ef = ctx.getApplication().getExpressionFactory(); 73. ValueExpression ve = ef.createValueExpression(ctx.getELContext(), "#{testSessionBean}", TestSession.class); 74. TestSession test = (TestSession)ve.getValue(ctx.getELContext()); 75. 76. // main jsf page 77. DCBindingContainer dc = (DCBindingContainer)BindingContext.getCurrent().getCurrentBi ndingsEntry();

78. // taskflow binding 79. DCTaskFlowBinding tf = (DCTaskFlowBinding)dc.findExecutableBinding("dynamicRegion 1"); 80. // pagedef of a page fragment 81. JUFormBinding form = (JUFormBinding) tf.findExecutableBinding("regions_employee_regi onPageDef"); 82. // handle to binding container of the region. 83. DCBindingContainer dcRegion = form; 84. 85. 86. 87. // return a methodexpression like a control flow case action or ADF pagedef action 88. private MethodExpression getMethodExpression(String name) { 89. Class [] argtypes = new Class[1]; 90. argtypes[0] = ActionEvent.class; 91. FacesContext facesCtx = FacesContext.getCurrentInstance(); 92. Application app = facesCtx.getApplication(); 93. ExpressionFactory elFactory = app.getExpressionFactory(); 94. ELContext elContext = facesCtx.getELContext(); 95. return elFactory.createMethodExpression(elContext,name,null,argtypes); 96. } 97. 98. // 99. RichCommandMenuItem menuPage1 = new RichCommandMenuItem(); 100. menuPage1.setId("page1"); 101. menuPage1.setText("Page 1"); 102. menuPage1.setActionExpression(getMethodExpression("page1")); 103. 104. RichCommandButton button = new RichCommandButton(); 105. button.setValueExpression("disabled",getValueExpression("#{!bindings."+item+".enable d}")); 106. button.setId(item); 107. button.setText(item); 108. MethodExpression me = getMethodExpression("#{bindings."+item+".execute}"); 109. button.addActionListener(new MethodExpressionActionListener(me)); 110. footer.getChildren().add(button); 111. 112. 113. // get a value 114. private ValueExpression getValueExpression(String name) { 115. FacesContext facesCtx = FacesContext.getCurrentInstance(); 116. Application app = facesCtx.getApplication(); 117. ExpressionFactory elFactory = app.getExpressionFactory(); 118. ELContext elContext = facesCtx.getELContext(); 119. return elFactory.createValueExpression(elContext, name, Object.class); 120. }

121. // an example how to use this 122. RichInputText input = new RichInputText(); 123. input.setValueExpression("value",getValueExpression("#{bindings."+item+".inputValue }")); 124. input.setValueExpression("label",getValueExpression("#{bindings."+item+".hints.label} ")); 125. input.setId(item); 126. panelForm.getChildren().add(input); 127. 128. 129. 130. // catch an exception and show it in the jsf page 131. catch(Exception e) { 132. FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMes sage(), ""); 133. FacesContext.getCurrentInstance().addMessage(null, msg); 134. } 135. 136. 137. FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN, msgHead , msgDetail); 138. facesContext.addMessage(uiComponent.getClientId(facesContext), msg); 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161.

// reset all the child uicomponents private void resetValueInputItems(AdfFacesContext adfFacesContext, UIComponent component){ List<UIComponent> items = component.getChildren(); for ( UIComponent item : items ) { resetValueInputItems(adfFacesContext,item); if ( item instanceof RichInputText ) { RichInputText input = (RichInputText)item; if ( !input.isDisabled() ) { input.resetValue() ; adfFacesContext.addPartialTarget(input); }; } else if ( item instanceof RichInputDate ) { RichInputDate input = (RichInputDate)item; if ( !input.isDisabled() ) { input.resetValue() ; adfFacesContext.addPartialTarget(input); }; }

162. } 163. } 164. 165. // redirect to a other url 166. ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext(); 167. HttpServletResponse response = (HttpServletResponse)ectx.getResponse(); 168. String url = ectx.getRequestContextPath()+"/adfAuthentication?logout=true&end_url=/fa ces/start.jspx"; 169. 170. try { 171. response.sendRedirect(url); 172. } catch (Exception ex) { 173. ex.printStackTrace(); 174. } 175. 176. // PPR refresh a jsf component 177. AdfFacesContext.getCurrentInstance().addPartialTarget(UIComponent); 178. 179. 180. // find a jsf component 181. private UIComponent getUIComponent(String name) { 182. FacesContext facesCtx = FacesContext.getCurrentInstance(); 183. return facesCtx.getViewRoot().findComponent(name) ; 184. } 185. 186. 187. // get the adf bc application module 188. private OEServiceImpl getAm(){ 189. FacesContext fc = FacesContext.getCurrentInstance(); 190. Application app = fc.getApplication(); 191. ExpressionFactory elFactory = app.getExpressionFactory(); 192. ELContext elContext = fc.getELContext(); 193. ValueExpression valueExp = 194. elFactory.createValueExpression(elContext, "#{data.OEServiceDataControl.dataProvid er}", 195. Object.class); 196. return (OEServiceImpl)valueExp.getValue(elContext); 197. } 198. 199. 200. // change the locale 201. Locale newLocale = new Locale(this.language); 202. FacesContext context = FacesContext.getCurrentInstance(); 203. context.getViewRoot().setLocale(newLocale); 204. 205.

206. 207. 208. 209. 210. 211. 212. 213. 214. 215. 216. 217. 218. 219. 220. 221. 222. 223. 224. 225. 226. 227. 228. 229. 230. 231. 232. 233. 234. 235. 236. 237. 238.

// get the stacktrace of a not handled exception private ControllerContext cc = ControllerContext.getInstance(); public String getStacktrace() { if ( cc.getCurrentViewPort().getExceptionData()!=null ) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); cc.getCurrentViewPort().getExceptionData().printStackTrace(pw); return sw.toString(); } return null; }

// get the selected rows from a table component RowKeySet selection = resultTable.getSelectedRowKeys(); Object[] keys = selection.toArray(); List<Long> receivers = new ArrayList<Long>(keys.length); for ( Object key : keys ) { User user = modelFriends.get((Integer)key); } // get selected Rows of a table 2 for (Object facesRowKey : table.getSelectedRowKeys()) { table.setRowKey(facesRowKey); Object o = table.getRowData(); JUCtrlHierNodeBinding rowData = (JUCtrlHierNodeBinding)o; Row row = rowData.getRow(); Test testRow = (Test)((DCDataRow)row).getDataProvider() ; } Method Binding for Action for Button 1 Getting a Single Selected Value

To select only one row in table 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1

public String cbSelectOne_action() { // For learning purposes - show Select One button clicked System.out.println("Select One Button has been Clicked"); // Get bindings associated with the current scope, then access the one that we have assigned to our table - e.g. OpenSupportItemsIterator DCBindingContainer bindings = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEn try(); DCIteratorBinding dcItteratorBindings = bindings.findIteratorBinding("OpenSupportItemsIterator"); // Get an object representing the table and what may be selected within it ViewObject voTableData = dcItteratorBindings.getViewObject(); // Get selected row Row rowSelected = voTableData.getCurrentRow(); // Display attribute of row in console output - would generally be bound to a UI component like a Label and or used to call another proces System.out.println(rowSelected.getAttribute("IssueID")); return null; }

To select many row in table


public String printSelectedEmpNames() { RowKeySet selectedEmps = getEmpTable().getSelectedRowKeys(); Iterator selectedEmpIter = selectedEmps.iterator(); DCBindingContainer bindings = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();

DCIteratorBinding empIter = bindings.findIteratorBinding("EmpView1Iterator"); RowSetIterator empRSIter = empIter.getRowSetIterator(); while(selectedEmpIter.hasNext()){ Key key = (Key)((List)selectedEmpIter.next()).get(0); Row currentRow = empRSIter.getRow(key); System.out.println(currentRow.getAttribute("Ename")); } return null; }

#{bindings.MahasiswaView1.collectionModel.makeCurrent}