View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   
6   package com.rc.celeritas.impleo;
7   
8   import com.rc.celeritas.controller.CrudWMServlet;
9   import com.rc.celeritas.db.DBHelper;
10  import com.rc.celeritas.exception.CeleritasException;
11  import com.rc.celeritas.query.QueryHelper;
12  import java.util.ArrayList;
13  import java.util.Hashtable;
14  import javax.servlet.http.HttpServletRequest;
15  import org.apache.commons.collections.MapUtils;
16  import org.apache.commons.lang.StringUtils;
17  import org.apache.log4j.Logger;
18  import org.webmacro.servlet.WebContext;
19  
20  /**
21   *
22   * @author rchoudhary
23   */
24  public class ExportImpleo implements Impleo{
25  
26      private static Logger log = Logger.getLogger(ExportImpleo.class);
27      
28      /**
29       *
30       * @param context
31       * @return
32       * @throws java.lang.Exception
33       */
34      public String implied(WebContext context) throws Exception {
35          context.put("impleo", "export");
36          Hashtable rMap = (Hashtable)context.get("rMap");
37          if(rMap == null || rMap.size() == 0){
38              new Exception("No Request Object Found!! Cannot proceed with Insertion...");
39          }
40          String table = MapUtils.getString(rMap, ImpleoConstants.TABLE, ImpleoConstants.EMPTY_STRING);
41          String sqlRef = MapUtils.getString(rMap, ImpleoConstants.SQLREF, ImpleoConstants.EMPTY_STRING);
42          String allowedPrincipals = CrudWMServlet.getAllowedPrincipals(table);
43          if(allowedPrincipals == null || authorize(MapUtils.getString(context, "username"), allowedPrincipals, context.getRequest())){
44              try {
45                  ArrayList columns = null;
46                  ArrayList results = null;
47                  if(StringUtils.isNotEmpty(table)){
48                      columns = DBHelper.getColumns(table);
49                      results = DBHelper.getQueryResults(QueryHelper.buildQuerySQL(table));
50                  }else if(StringUtils.isNotEmpty(sqlRef)){
51                      String sql = CrudWMServlet.getSqlProperty(sqlRef);
52                      columns = DBHelper.getColumnsFromSQL(sql);
53                      results = DBHelper.getQueryResults(sql);
54                  }
55                  context.put(ImpleoConstants.COLUMNS, columns);
56                  context.put(ImpleoConstants.DATA, results);
57  
58              } catch (CeleritasException e) {
59                  log.error("Error in Export Impleo : " + e.toString());
60                  throw new Exception(e);
61              }
62          }else{
63              String error_message = "Sorry you are not authorize to view data from this table";
64              context.put("message", error_message);
65              return "auth_error.html";
66          }
67          return null;
68      }
69  
70      /**
71       * 
72       * @param user
73       * @param allowedPrincipals
74       * @param req
75       * @return
76       */
77      private boolean authorize(String user, String allowedPrincipals, HttpServletRequest req) {
78          boolean allowed = false;
79  
80          if(allowedPrincipals.indexOf(user) > -1){
81              allowed = true;
82          } 
83          
84          String principals[] = allowedPrincipals.split(",");
85          for(int i = 0;i < principals.length; i++){
86              allowed = req.isUserInRole(principals[i]);
87              if(allowed){
88                  break;
89              }
90          }
91          return allowed;
92      }
93  }