Sunday 30 November 2014

Logout Implement in Oracle ADF

The below code is required for logout in ADF applications.
public String logOut()  throws IOException{
        HttpSession session = null;
          FacesContext fctx = FacesContext.getCurrentInstance();
          ExternalContext ectx = fctx.getExternalContext();
          session = (HttpSession)ectx.getSession(false);
          HttpServletResponse response = (HttpServletResponse)ectx.getResponse();                      String currentPage = "faces/login.jspx";
          String url = ectx.getRequestContextPath()+"/adfAuthentication?logout=true&end_url=" + currentPage;
         try {
             response.sendRedirect(url);
              session.invalidate();
         } catch (Exception ex) {
             ex.printStackTrace();
          }
          fctx.responseComplete(); 
          return null;
      }

Thanks...

Pagination in ADF

We Need to set the ScrollPolicy attribute to Page and autoHeightRow=”0”.
And if you want to display 5 rows in a page then set the RangeSize = 5 in iterator.



The screen will looks like

Thanks...

Monday 24 November 2014

Default editor of page to source code in Jdeveloper


Sometime Jdeveloper will be slow and it will take long time to open the files (ex –jspx, jsff etc) into design mode. If you want to open a jspx/jsff page directly into soure tab then you can follow the below steps.

Go to Tool > Preference > File Types and open default editor

 Thanks...

Sunday 23 November 2014

Print Functionality in Oracle ADF

To implement print functionality using Oracle ADF, we need to follow the below steps.
Approach -1:
Step-1:- Add <af:showPrintablePageBehavior/> component inside the button.

<af:commandButton text="Print1" id="cb1" binding="#{backingBeanScope.backing_PrintPage.cb1}">

<af:showPrintablePageBehavior/>

</af:commandButton>

Step-2:- Add before phase method inside view tag. Code show looks like the below


Step-3:- Add the below code inside backing/managed bean.

public void beforePhaseMethod(PhaseEvent phaseEvent) {

        if (phaseEvent.getPhaseId() == PhaseId.RENDER_RESPONSE) {

            FacesContext fctx = FacesContext.getCurrentInstance();

            Map requestMap = fctx.getExternalContext().getRequestMap();

            Object showPrintableBehavior = requestMap.get("oracle.adfinternal.view.faces.el.PrintablePage");

            if (showPrintableBehavior != null) {

                if (Boolean.TRUE == showPrintableBehavior) {

                    ExtendedRenderKitService erks = null;

                    erks = Service.getRenderKitService(fctx, ExtendedRenderKitService.class);

                    //invoke JavaScript from the server

                    erks.addScript(fctx, "window.print();");

                }

            }

        }

    }

NOTE:- if you don’t want to display some component in your print page then add
rendered="#{adfFacesContext.outputMode != 'printable'}" to that component.


Approach-2: Add the below Code on click of button 
public String printPage() {
        // Add event code here...
        FacesContext facesContext = FacesContext.getCurrentInstance();

        org.apache.myfaces.trinidad.render.ExtendedRenderKitService service =
                   org.apache.myfaces.trinidad.util.Service.getRenderKitService(facesContext,   ExtendedRenderKitService.class);

               service.addScript(facesContext, "window.print();");
        return null;
    }


In this approach-2 it will not open the new window with content but the same page the print window will open.

Thanks...

Export to Excel with user customization (Style Class)

This blog we will see how to export excel sheet using default component and user customization using oracle ADF.

Export to excel using ADF component:

Export to excel using ADF component:Export to excel from any table using af:exportCollectionActionListener component.

<af:commandImageLink text="Export to Excel"
                                           icon="/images/export-to.png">
          <af:exportCollectionActionListener exportedId="tab1"
                                                                       type="excelHTML"
                                                                       title="ExportToExcel"
                                                                       filename="TestTable.xls"/>
 </af:commandImageLink>

But this type of functionality is not enough for user. Through this we can’t achieve the export to excel as per the user expectation.

Export to excel using style class:
 
The below code is required export to excel using style class.
The jsff/jspx you can add the below code:

<af:commandImageLink text="Export to Excel"
                                           icon="/images/export-to.png">
    <af:setActionListener from="tab1" to="#{viewScope['exportId']}"/>
    <af:setActionListener from="border:1px solid black" to="#{viewScope['tableHeaderStyle']}"/>
    <af:setActionListener from="border:1px solid black" to="#{viewScope['tableDataStyle']}"/>
    <af:fileDownloadActionListener filename="TestTable.xls"
                                                           contentType="excelHTML;chatset=UTF-8"
                                                           method="#{testBean.exportToExcel}"/>
 </af:commandImageLink>

The below code needs to add your backing/managed bean:

public void exportToExcel(FacesContext facesContext, OutputStream os)
  throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException
{
  Map<String, Object> varDetails = ADFContext.getCurrent().getViewScope();
  String expId = (String)varDetails.get("exportId"); // export table id
  String thStyle = (String)varDetails.get("tableHeaderStyle"); // style class for <th>
  String tdStyle = (String)varDetails.get("tableDataStyle"); // style class for  <td>
  boolean showHiddenCols = "true".equals(varDetails.get("exporter.showHiddenColumns")); // define if hidden columns should be shown
  boolean forceColWidth = "true".equals(varDetails.get("exporter.forceColumnWidth")); // force width of columns

  // get the table from absoulte path
  UIViewRoot vr = facesContext.getViewRoot();
  RichTable table = (RichTable)vr.findComponent(expId);

  // get columns of table
  List<RichColumn> cols = new ArrayList<RichColumn>();
  for (UIComponent co : table.getChildren())
  {
    if (co instanceof RichColumn)
    {
      RichColumn col = (RichColumn)co;
      // if showHiddenCols is true shows all columns, otherwise shows only rendered and visible columns
      if (showHiddenCols || (col.isRendered() && col.isVisible()))
        cols.add(col);
    }
  }

  OutputStreamWriter expData = new OutputStreamWriter(os);
  expData.append("<table>");
  expData.append("<tr>");
  for (RichColumn col : cols)
  {
    // render <th> with style, align, width and nowrap attributes
    expData.append("<th");
    if (StringUtils.isNotEmpty(thStyle))
      expData.append(String.format(" style='%s'", thStyle));
    expData.append(String.format(" align='%s'", StringUtils.defaultString(col.getAlign(), "left")));
    if (forceColWidth && StringUtils.isNotEmpty(col.getWidth()))
      expData.append(String.format(" width='%s'", col.getWidth()));
    if (col.isHeaderNoWrap())
      expData.append(" nowrap");
    expData.append(">");
    expData.append(StringUtils.defaultString(col.getHeaderText()));
    expData.append("</th>");
  }
  expData.append("</tr>");

  ELContext elContext = facesContext.getELContext();
  ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory();
  if (StringUtils.isNotEmpty(table.getVarStatus()))
  {
    // create varStatusMap
    Method m = UIXIterator.class.getDeclaredMethod("createVarStatusMap", new Class[0]);
    m.setAccessible(true);
    Object varStatus = m.invoke(table, new Object[0]);
    String el = String.format("#{%s}", table.getVarStatus());
    ValueExpression exp = expressionFactory.createValueExpression(elContext, el, Object.class);
    exp.setValue(elContext, varStatus);
  }

  CollectionModel model = (CollectionModel)table.getValue();
  int rowcount = model.getRowCount();
  for (int i = 0; i < rowcount; i++)
  {
    model.setRowIndex(i);
    JUCtrlHierNodeBinding row = (JUCtrlHierNodeBinding)model.getRowData();
    if (StringUtils.isNotEmpty(table.getVar()))
    {
      String el = String.format("#{%s}", table.getVar());
      ValueExpression exp = expressionFactory.createValueExpression(elContext, el, Object.class);
      exp.setValue(elContext, row);
    }

    expData.append("<tr>");
    for (RichColumn col : cols)
    {
      // get value from some column attributes
      ValueExpression inlineStyleVE = col.getValueExpression("inlineStyle");
      ValueExpression alignVE = col.getValueExpression("align");
      String style = inlineStyleVE == null ? "" : (String)inlineStyleVE.getValue(facesContext.getELContext());
      String align = alignVE == null ? "" : (String)alignVE.getValue(facesContext.getELContext());

      // render <td> with style, align, width and nowrap attributes
      expData.append("<td");
      if (StringUtils.isNotEmpty(tdStyle) || StringUtils.isNotEmpty(style))
        expData.append(String.format(" style='%s;%s'", StringUtils.defaultString(tdStyle), StringUtils.defaultString(style)));
      if (StringUtils.isNotEmpty(align))
        expData.append(String.format(" align='%s'", align));
      if (forceColWidth && StringUtils.isNotEmpty(col.getWidth()))
        expData.append(String.format(" width='%s'", col.getWidth()));
      if (col.isNoWrap())
        expData.append(" nowrap");
      expData.append(">");

      // render the content of <td> using the value or text of its children (depending on the type of component)
      // note that you can add other UIComponent types and render using different criteria, and also you can convert value (Date, Double, etc)
      for (UIComponent co : col.getChildren())
      {
        if (co instanceof UIXValue)
        {
          UIXValue uixValue = (UIXValue)co;
          if (uixValue.getValue() != null)
            expData.append(uixValue.getValue().toString());
        }
        else if (co instanceof RichCommandLink)
        {
          RichCommandLink commandLink = (RichCommandLink)co;
          if (commandLink.getText() != null)
            expData.append(commandLink.getText());
        }
        else if (co instanceof RichGoLink)
        {
          RichGoLink goLink = (RichGoLink)co;
          if (goLink.getText() != null)
            expData.append(String.format("<a href='%s'>%s</a>", goLink.getDestination(), goLink.getText()));
        }
      }
      expData.append("</td>");
    }
    expData.append("</tr>");
  }
  expData.append("</table>");
  expData.close();
}

Thanks...