Advertisement
tko_pb

VariablesSecureApp

Aug 2nd, 2018
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.93 KB | None | 0 0
  1. /*
  2.  ************************************************************************************
  3.  * Copyright (C) 2001-2017 Openbravo S.L.U.
  4.  * Licensed under the Apache Software License version 2.0
  5.  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  6.  * Unless required by applicable law or agreed to  in writing,  software  distributed
  7.  * under the License is distributed  on  an  "AS IS"  BASIS,  WITHOUT  WARRANTIES  OR
  8.  * CONDITIONS OF ANY KIND, either  express  or  implied.  See  the  License  for  the
  9.  * specific language governing permissions and limitations under the License.
  10.  ************************************************************************************
  11.  */
  12. package org.openbravo.base.secureApp;
  13.  
  14. import java.util.Arrays;
  15. import java.util.Enumeration;
  16.  
  17. import javax.servlet.http.HttpServletRequest;
  18.  
  19. import org.apache.commons.lang.StringUtils;
  20. import org.apache.log4j.Logger;
  21. import org.openbravo.base.VariablesBase;
  22. import org.openbravo.dal.core.OBContext;
  23. import org.openbravo.dal.service.OBDal;
  24. import org.openbravo.data.FieldProvider;
  25. import org.openbravo.erpCommon.utility.OBError;
  26. import org.openbravo.model.ad.system.Client;
  27. import org.openbravo.scheduling.OBScheduler;
  28. import org.quartz.SchedulerException;
  29.  
  30. /**
  31.  * This class is used to provide the coder with friendly methods to retrieve certain environment,
  32.  * session and servlet call variables.
  33.  *
  34.  * @author Openbravo
  35.  *
  36.  */
  37. public class VariablesSecureApp extends VariablesBase {
  38.   private static final Logger log4j = Logger.getLogger(VariablesSecureApp.class);
  39.   private String user;
  40.   private String role;
  41.   private String language;
  42.   private String theme;
  43.   private String client;
  44.   private String organization;
  45.   private String warehouse;
  46.   private String command;
  47.   private String userClient;
  48.   private String dbSessionID;
  49.   private String javaDateFormat;
  50.   private String javaDataTimeFormat;
  51.   private String jsDateFormat;
  52.   private String sqlDateFormat;
  53.   private String accessLevel;
  54.  
  55.   /**
  56.    * Constructor used to make an empty/manual instance of this class.
  57.    *
  58.    * @param strUser
  59.    *          ID of the user as specified by the AD_USER_ID column within the AD_USER database
  60.    *          table.
  61.    * @param strClient
  62.    *          ID of the client as specified by the AD_CLIENT_ID column within the AD_CLIENT database
  63.    *          table.
  64.    * @param strOrganization
  65.    *          ID of the organization as specified by the AD_ORG_ID column within the AD_ORG database
  66.    *          table.
  67.    */
  68.   public VariablesSecureApp(String strUser, String strClient, String strOrganization) {
  69.     this.user = strUser;
  70.     this.role = "";
  71.     this.language = System.getProperty("user.language") + "_" + System.getProperty("user.country");
  72.     this.theme = "";
  73.     this.client = strClient;
  74.     this.organization = strOrganization;
  75.     this.userClient = "";
  76.     this.warehouse = "";
  77.     this.dbSessionID = "";
  78.     this.command = "DEFAULT";
  79.     this.javaDateFormat = "";
  80.     this.jsDateFormat = "";
  81.     this.sqlDateFormat = "";
  82.     this.accessLevel = "";
  83.   }
  84.  
  85.   /**
  86.    * Constructor used to make an empty/manual instance of this class.
  87.    *
  88.    * @param strUser
  89.    *          ID of the user as specified by the AD_USER_ID column within the AD_USER database
  90.    *          table.
  91.    * @param strClient
  92.    *          ID of the client as specified by the AD_CLIENT_ID column within the AD_CLIENT database
  93.    *          table.
  94.    * @param strOrganization
  95.    *          ID of the organization as specified by the AD_ORG_ID column within the AD_ORG database
  96.    *          table.
  97.    * @param strRole
  98.    *          ID of the role as specified by the AD_ROLE_ID column within the AD_ROLE database
  99.    *          table.
  100.    */
  101.   public VariablesSecureApp(String strUser, String strClient, String strOrganization, String strRole) {
  102.     this(strUser, strClient, strOrganization, strRole, null);
  103.   }
  104.  
  105.   /**
  106.    * Constructor used to make an empty/manual instance of this class.
  107.    *
  108.    * @param strUser
  109.    *          ID of the user as specified by the AD_USER_ID column within the AD_USER database
  110.    *          table.
  111.    * @param strClient
  112.    *          ID of the client as specified by the AD_CLIENT_ID column within the AD_CLIENT database
  113.    *          table.
  114.    * @param strOrganization
  115.    *          ID of the organization as specified by the AD_ORG_ID column within the AD_ORG database
  116.    *          table.
  117.    * @param strLanguage
  118.    *          Language
  119.    */
  120.   public VariablesSecureApp(String strUser, String strClient, String strOrganization,
  121.       String strRole, String strLanguage) {
  122.     this.user = strUser;
  123.     this.role = strRole;
  124.     if (StringUtils.isEmpty(strLanguage)) {
  125.       this.language = System.getProperty("user.language") + "_"
  126.           + System.getProperty("user.country");
  127.     } else {
  128.       this.language = strLanguage;
  129.     }
  130.     this.theme = "";
  131.     this.client = strClient;
  132.     this.organization = strOrganization;
  133.     this.userClient = "";
  134.     this.warehouse = "";
  135.     this.dbSessionID = "";
  136.     this.command = "DEFAULT";
  137.     this.javaDateFormat = "";
  138.     this.jsDateFormat = "";
  139.     this.sqlDateFormat = "";
  140.     this.accessLevel = "";
  141.   }
  142.  
  143.   /**
  144.    * Constructor that parses the session variables to initialize the class' internal variables.
  145.    *
  146.    * @param request
  147.    *          HttpServletRequest object originating from the user request.
  148.    */
  149.   public VariablesSecureApp(HttpServletRequest request) {
  150.     super(request);
  151.     setValues();
  152.   }
  153.  
  154.   /**
  155.    * Constructor that parses the session variables to initialize the class' internal variables for
  156.    * multipart requests.
  157.    *
  158.    * @param request
  159.    *          HttpServletRequest object originating from the user request.
  160.    */
  161.   public VariablesSecureApp(HttpServletRequest request, boolean f) {
  162.     super(request, f);
  163.     setValues();
  164.   }
  165.  
  166.   /**
  167.    * Internal method used to parse the session variables and store them into private variables of
  168.    * this class. These variables can them be retrieved through various get methods (e.g. getUser(),
  169.    * getRole(), etc.).
  170.    */
  171.   private void setValues() {
  172.     this.user = getSessionValue("#AD_User_ID");
  173.     this.role = getSessionValue("#AD_Role_ID");
  174.     this.language = getSessionValue("#AD_Language");
  175.     this.theme = getSessionValue("#Theme");
  176.     this.client = getSessionValue("#AD_Client_ID");
  177.     this.organization = getSessionValue("#AD_Org_ID");
  178.     this.userClient = getSessionValue("#User_Client");
  179.     this.warehouse = getSessionValue("#M_Warehouse_ID");
  180.     this.dbSessionID = getSessionValue("#AD_Session_ID");
  181.     this.command = getStringParameter("Command", "DEFAULT");
  182.     this.javaDateFormat = getSessionValue("#AD_JavaDateFormat");
  183.     this.javaDataTimeFormat = getSessionValue("#AD_JavaDateTimeFormat");
  184.     this.jsDateFormat = getSessionValue("#AD_JsDateFormat");
  185.     this.sqlDateFormat = getSessionValue("#AD_SqlDateFormat");
  186.     this.accessLevel = getSessionValue("#CurrentAccessLevel");
  187.   }
  188.  
  189.   /**
  190.    * Returns the primary key (AD_USER_ID) of the authenticated user deriving from the AD_USER table.
  191.    *
  192.    * @return AD_USER_ID primary key number formatted as string
  193.    */
  194.   public String getUser() {
  195.     return user;
  196.   }
  197.  
  198.   /**
  199.    * Returns the primary key (AD_ROLE_ID) of the role of the authenticated user deriving as entered
  200.    * in the AD_ROLE table.
  201.    *
  202.    * @return AD_ROLE_ID primary key number formatted as string
  203.    */
  204.   public String getRole() {
  205.     return role;
  206.   }
  207.  
  208.   /**
  209.    * Returns the code of the language currently selected by the authenticated user according to the
  210.    * RFC 4646 format LANG_REGION, e.g. es_ES for Spanish language from Spain or en_GB for English
  211.    * language from Great Britain.
  212.    *
  213.    * @return The language code formatted as a string according to RFC 4646.
  214.    */
  215.   public String getLanguage() {
  216.     return language;
  217.   }
  218.  
  219.   /**
  220.    * Returns the unique name of the theme currently selected for the session. This usually
  221.    * corresponds to the theme's folder name in the web/skins. Default theme's value is 'Default'.
  222.    *
  223.    * @return String with the unique name of the theme.
  224.    */
  225.   public String getTheme() {
  226.     if (!theme.equals("")) {
  227.       return theme;
  228.     } else {
  229.       String strTheme = "";
  230.       try {
  231.         if (OBScheduler.getInstance().getScheduler() != null
  232.             && OBScheduler.getInstance().getScheduler().isStarted()) {
  233.           Client systemClient = OBDal.getInstance().get(Client.class, "0");
  234.  
  235.           // Get theme (skin)
  236.           OBContext.setAdminMode();
  237.           try {
  238.             org.openbravo.model.ad.system.System sys = OBDal.getInstance().get(
  239.                 org.openbravo.model.ad.system.System.class, "0");
  240.             if (sys != null && !sys.getTADTheme().isEmpty()) {
  241.               strTheme = (systemClient.getLanguage().isRTLLanguage() ? "rtl/" : "ltr/")
  242.                   + sys.getTADTheme();
  243.             }
  244.           } catch (Exception e) {
  245.             log4j.error("Error getting theme", e);
  246.             // set default theme and ignore exception
  247.             strTheme = "";
  248.           } finally {
  249.             OBContext.restorePreviousMode();
  250.           }
  251.         }
  252.       } catch (SchedulerException e) {
  253.         log4j.error("Error while checking if the scheduler is started.", e);
  254.       } finally {
  255.         if (strTheme.isEmpty()) {
  256.           strTheme = "ltr/org.openbravo.userinterface.skin.250to300Comp/250to300Comp";
  257.         }
  258.       }
  259.       return strTheme;
  260.     }
  261.   }
  262.  
  263.   /**
  264.    * Returns the ID of the client (AD_CLIENT_ID) as defined by the role of the user's current
  265.    * session.
  266.    *
  267.    * @return String with the AD_CLIENT_ID primary key value.
  268.    */
  269.   public String getClient() {
  270.     return client;
  271.   }
  272.  
  273.   /**
  274.    * Returns the ID of the organization (AD_ORG_ID) selected by the user among the ones available
  275.    * within the role of the current session.
  276.    *
  277.    * @return String with the AD_ORG_ID primary key value.
  278.    */
  279.   public String getOrg() {
  280.     return organization;
  281.   }
  282.  
  283.   public String getUserClient() {
  284.     return userClient;
  285.   }
  286.  
  287.   public String getUserOrg() {
  288.     return getSessionValue("#AccessibleOrgTree");
  289.   }
  290.  
  291.   /**
  292.    * Returns the ID of the current default warehouse that will be used with transactions that
  293.    * require a warehouse selected. This is selected using the Role change window and the user can
  294.    * only select warehouses he or she has access to.
  295.    *
  296.    * @return String with the M_WAREHOUSE primary key value.
  297.    */
  298.   public String getWarehouse() {
  299.     return warehouse;
  300.   }
  301.  
  302.   /**
  303.    * Returns the ID of the session stored within the AD_SESSION database table.
  304.    *
  305.    * @return String with the AD_SESSION primary key value.
  306.    */
  307.   public String getDBSession() {
  308.     return dbSessionID;
  309.   }
  310.  
  311.   /**
  312.    * Returns the command that was passed to the servlet through the Command parameter of the HTTP
  313.    * POST/GET. Normally used by the java controllers so that one controller can support various
  314.    * actions/functions.
  315.    *
  316.    * @return String containing the value of the Command parameter.
  317.    */
  318.   public String getCommand() {
  319.     return command;
  320.   }
  321.  
  322.   public String getAccessLevel() {
  323.     return accessLevel;
  324.   }
  325.  
  326.   /**
  327.    * Returns true if the Command parameter of the HTTP POST/GET request to the servlet is one of the
  328.    * specified values, false if not.
  329.    *
  330.    * @param commands
  331.    *          The list of strings to compare Command parameter to.
  332.    * @return {@code true} in case current command is any of the ones passed as parameter
  333.    * @see #getCommand()
  334.    */
  335.   public boolean commandIn(String... commands) {
  336.     return command != null && Arrays.asList(commands).contains(command);
  337.   }
  338.  
  339.   /**
  340.    * Returns the date format used in Java formatting as defined by the dateFormat.java variable
  341.    * within the config/Openbravo.properties configuration file.
  342.    *
  343.    * @return Formatting string, for example 'dd-MM-yyyy'.
  344.    */
  345.   public String getJavaDateFormat() {
  346.     return javaDateFormat;
  347.   }
  348.  
  349.   /**
  350.    * @return the javaDataTimeFormat
  351.    */
  352.   public String getJavaDataTimeFormat() {
  353.     return javaDataTimeFormat;
  354.   }
  355.  
  356.   /**
  357.    * Returns the date format used in Javascript formatting as defined by the dateFormat.java
  358.    * variable within the config/Openbravo.properties configuration file.
  359.    *
  360.    * @return Formatting string, for example '%d-%m-%Y'.
  361.    */
  362.   public String getJsDateFormat() {
  363.     return jsDateFormat;
  364.   }
  365.  
  366.   /**
  367.    * Returns the date format used in SQL formatting as defined by the dateFormat.sql variable within
  368.    * the config/Openbravo.properties configuration file.
  369.    *
  370.    * @return Formatting string, for example 'DD-MM-YYYY'.
  371.    */
  372.   public String getSqlDateFormat() {
  373.     return sqlDateFormat;
  374.   }
  375.  
  376.   /**
  377.    * Returns a deserialized OBError object retrieved from the session data that might contain the
  378.    * error information for the specified tab. This error would normally be generated by the
  379.    * controller servlet of that tab.
  380.    *
  381.    * @param AD_Tab_ID
  382.    *          String with the primary key (ID) of the tab as entered within the AD_TAB database
  383.    *          table.
  384.    * @return Deserialized OBError object retrieved from the session. Null if no error message exists
  385.    *         for this tab.
  386.    * @see #setMessage(String, OBError)
  387.    * @see #removeMessage(String)
  388.    */
  389.   public OBError getMessage(String AD_Tab_ID) {
  390.     return ((OBError) getSessionObject(AD_Tab_ID + "|message"));
  391.   }
  392.  
  393.   /**
  394.    * Serializes and saves the error object to a session variable, specific to the tab which ID is
  395.    * being passed.
  396.    *
  397.    * @param AD_Tab_ID
  398.    *          String with the primary key (ID) of the tab as entered within the AD_TAB database
  399.    *          table.
  400.    * @param error
  401.    *          The OBError object that needs to be set.
  402.    * @see #setMessage(String, OBError)
  403.    * @see #removeMessage(String)
  404.    */
  405.   public void setMessage(String AD_Tab_ID, OBError error) {
  406.     setSessionObject(AD_Tab_ID + "|message", error);
  407.   }
  408.  
  409.   /**
  410.    * Removes the error object for the specified tab from the session data. This needs to be done in
  411.    * order for the message not to appear every time the tab is reloaded.
  412.    *
  413.    * @param AD_Tab_ID
  414.    *          String with the primary key (ID) of the tab as entered within the AD_TAB database
  415.    *          table.
  416.    * @see #setMessage(String, OBError)
  417.    * @see #getMessage(String)
  418.    */
  419.   public void removeMessage(String AD_Tab_ID) {
  420.     removeSessionValue(AD_Tab_ID + "|message");
  421.   }
  422.  
  423.   public FieldProvider getEditionData(String AD_Tab_ID) {
  424.     return ((FieldProvider) getSessionObject(AD_Tab_ID + "|editionData"));
  425.   }
  426.  
  427.   public void setEditionData(String AD_Tab_ID, FieldProvider data) {
  428.     setSessionObject(AD_Tab_ID + "|editionData", data);
  429.   }
  430.  
  431.   public void removeEditionData(String AD_Tab_ID) {
  432.     removeSessionValue(AD_Tab_ID + "|editionData");
  433.   }
  434.  
  435.   public Enumeration<String> getEnumerationString ()  {
  436.       return getParameterNames();
  437.   }
  438.  
  439. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement