Search in sources :

Example 6 with MRole

use of org.compiere.model.MRole in project adempiere by adempiere.

the class AbstractDocumentSearch method getID.

/**
	 * search for id's that fit the searchString
	 *
	 * @param transactionCode
	 * @param searchString
	 */
private void getID(String transactionCode, String searchString) {
    ResultSet rsSO = null;
    ResultSet rsPO = null;
    PreparedStatement pstmtSO = null;
    PreparedStatement pstmtPO = null;
    String sqlSO = null;
    String sqlPO = null;
    final Properties ctx = Env.getCtx();
    final MRole role = MRole.get(ctx, Env.getAD_Role_ID(ctx), Env.getAD_User_ID(ctx), true);
    try {
        for (MSearchDefinition msd : MSearchDefinition.getForCode(transactionCode)) {
            MTable table = new MTable(Env.getCtx(), msd.getAD_Table_ID(), null);
            // SearchDefinition with a given table and column
            if (msd.getSearchType().equals(MSearchDefinition.SEARCHTYPE_TABLE)) {
                MColumn column = new MColumn(Env.getCtx(), msd.getAD_Column_ID(), null);
                sqlSO = "SELECT " + table.getTableName() + "_ID FROM " + table.getTableName() + " ";
                // search for an Integer
                if (msd.getDataType().equals(MSearchDefinition.DATATYPE_INTEGER)) {
                    sqlSO += "WHERE " + column.getColumnName() + "=?";
                // search for a String
                } else {
                    sqlSO += "WHERE UPPER(" + column.getColumnName() + ") LIKE UPPER(?)";
                }
                if (msd.getPO_Window_ID() != 0) {
                    sqlPO = sqlSO + " AND IsSOTrx='N'";
                    sqlSO += " AND IsSOTrx='Y'";
                }
                pstmtSO = DB.prepareStatement(sqlSO, null);
                pstmtPO = DB.prepareStatement(sqlPO, null);
                // search for a Integer
                if (msd.getDataType().equals(MSearchDefinition.DATATYPE_INTEGER)) {
                    pstmtSO.setInt(1, Integer.valueOf(searchString.replaceAll("\\D", "")));
                    if (msd.getPO_Window_ID() != 0) {
                        pstmtPO.setInt(1, Integer.valueOf(searchString.replaceAll("\\D", "")));
                    }
                // search for a String
                } else if (msd.getDataType().equals(MSearchDefinition.DATATYPE_STRING)) {
                    pstmtSO.setString(1, searchString);
                    if (msd.getPO_Window_ID() != 0) {
                        pstmtPO.setString(1, searchString);
                    }
                }
            // SearchDefinition with a special query
            } else if (msd.getSearchType().equals(MSearchDefinition.SEARCHTYPE_QUERY)) {
                sqlSO = msd.getQuery();
                pstmtSO = DB.prepareStatement(sqlSO, null);
                // count '?' in statement
                int count = 1;
                for (char c : sqlSO.toCharArray()) {
                    if (c == '?') {
                        count++;
                    }
                }
                for (int i = 1; i < count; i++) {
                    if (msd.getDataType().equals(MSearchDefinition.DATATYPE_INTEGER)) {
                        pstmtSO.setInt(i, Integer.valueOf(searchString.replaceAll("\\D", "")));
                    } else if (msd.getDataType().equals(MSearchDefinition.DATATYPE_STRING)) {
                        pstmtSO.setString(i, searchString);
                    }
                }
            }
            if (pstmtSO != null) {
                log.fine("SQL Sales: " + sqlSO);
                rsSO = pstmtSO.executeQuery();
                Vector<Integer> idSO = new Vector<Integer>();
                while (rsSO.next()) {
                    idSO.add(new Integer(rsSO.getInt(1)));
                }
                if (role.getWindowAccess(msd.getAD_Window_ID()) != null) {
                    log.fine("Open Window: " + msd.getAD_Window_ID() + " / Table: " + table.getTableName() + " / Number of Results: " + idSO.size());
                    if (idSO.size() == 0 && (searchString == null || searchString.trim().length() == 0)) {
                        // No search string - open the window with new record
                        idSO.add(new Integer(0));
                    }
                    openWindow(idSO, table.getTableName(), msd.getAD_Window_ID());
                } else {
                    log.warning("Role is not allowed to view this window");
                }
            }
            if (pstmtPO != null) {
                log.fine("SQL Purchase: " + sqlPO);
                rsPO = pstmtPO.executeQuery();
                Vector<Integer> idPO = new Vector<Integer>();
                while (rsPO.next()) {
                    idPO.add(new Integer(rsPO.getInt(1)));
                }
                if (role.getWindowAccess(msd.getPO_Window_ID()) != null) {
                    log.fine("Open Window: " + msd.getPO_Window_ID() + " / Table: " + table.getTableName() + " / Number of Results: " + idPO.size());
                    openWindow(idPO, table.getTableName(), msd.getPO_Window_ID());
                } else {
                    log.warning("Role is not allowed to view this window");
                }
            }
            DB.close(rsSO, pstmtSO);
            DB.close(rsPO, pstmtPO);
            pstmtSO = null;
            pstmtPO = null;
            rsSO = null;
            rsPO = null;
        }
    } catch (Exception e) {
        log.severe(e.toString());
        e.printStackTrace();
    } finally {
        DB.close(rsSO, pstmtSO);
        DB.close(rsPO, pstmtPO);
        rsSO = null;
        rsPO = null;
        pstmtSO = null;
        pstmtPO = null;
    }
}
Also used : MColumn(org.compiere.model.MColumn) MTable(org.compiere.model.MTable) MSearchDefinition(org.compiere.model.MSearchDefinition) MRole(org.compiere.model.MRole) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) Vector(java.util.Vector) SQLException(java.sql.SQLException)

Example 7 with MRole

use of org.compiere.model.MRole in project adempiere by adempiere.

the class MWFActivity method sendEMail.

//	fillParameter
/*********************************
	 * 	Send EMail
	 */
private void sendEMail() {
    DocAction doc = (DocAction) m_po;
    MMailText text = new MMailText(getCtx(), m_node.getR_MailText_ID(), null);
    text.setPO(m_po, true);
    //
    String subject = doc.getDocumentInfo() + ": " + text.getMailHeader();
    String message = text.getMailText(true) + "\n-----\n" + doc.getDocumentInfo() + "\n" + doc.getSummary();
    File pdf = doc.createPDF();
    //
    MClient client = MClient.get(doc.getCtx(), doc.getAD_Client_ID());
    //	Explicit EMail
    sendEMail(client, 0, m_node.getEMail(), subject, message, pdf, text.isHtml());
    //	Recipient Type
    String recipient = m_node.getEMailRecipient();
    //	email to document user
    if (recipient == null || recipient.length() == 0)
        sendEMail(client, doc.getDoc_User_ID(), null, subject, message, pdf, text.isHtml());
    else if (recipient.equals(MWFNode.EMAILRECIPIENT_DocumentBusinessPartner)) {
        int index = m_po.get_ColumnIndex("AD_User_ID");
        if (index > 0) {
            Object oo = m_po.get_Value(index);
            if (oo instanceof Integer) {
                int AD_User_ID = ((Integer) oo).intValue();
                if (AD_User_ID != 0)
                    sendEMail(client, AD_User_ID, null, subject, message, pdf, text.isHtml());
                else
                    log.fine("No User in Document");
            } else
                log.fine("Empty User in Document");
        } else
            log.fine("No User Field in Document");
    } else if (recipient.equals(MWFNode.EMAILRECIPIENT_DocumentOwner))
        sendEMail(client, doc.getDoc_User_ID(), null, subject, message, pdf, text.isHtml());
    else if (recipient.equals(MWFNode.EMAILRECIPIENT_WFResponsible)) {
        MWFResponsible resp = getResponsible();
        if (resp.isInvoker())
            sendEMail(client, doc.getDoc_User_ID(), null, subject, message, pdf, text.isHtml());
        else if (resp.isHuman())
            sendEMail(client, resp.getAD_User_ID(), null, subject, message, pdf, text.isHtml());
        else if (resp.isRole()) {
            MRole role = resp.getRole();
            if (role != null) {
                MUser[] users = MUser.getWithRole(role);
                for (int i = 0; i < users.length; i++) sendEMail(client, users[i].getAD_User_ID(), null, subject, message, pdf, text.isHtml());
            }
        } else if (resp.isOrganization()) {
            MOrgInfo org = MOrgInfo.get(getCtx(), m_po.getAD_Org_ID(), get_TrxName());
            if (org.getSupervisor_ID() == 0)
                log.fine("No Supervisor for AD_Org_ID=" + m_po.getAD_Org_ID());
            else
                sendEMail(client, org.getSupervisor_ID(), null, subject, message, pdf, text.isHtml());
        }
    }
}
Also used : DocAction(org.compiere.process.DocAction) MMailText(org.compiere.model.MMailText) MRole(org.compiere.model.MRole) MOrgInfo(org.compiere.model.MOrgInfo) File(java.io.File) MClient(org.compiere.model.MClient)

Example 8 with MRole

use of org.compiere.model.MRole in project adempiere by adempiere.

the class MWFActivity method getApprovalUser.

//	isInvoker
/**
	 * 	Get Approval User.
	 * 	If the returned user is the same, the document is approved.
	 *	@param AD_User_ID starting User
	 *	@param C_Currency_ID currency
	 *	@param amount amount
	 *	@param AD_Org_ID document organization
	 *	@param ownDocument the document is owned by AD_User_ID
	 *	@return AD_User_ID - if -1 no Approver
	 */
public int getApprovalUser(int AD_User_ID, int C_Currency_ID, BigDecimal amount, int AD_Org_ID, boolean ownDocument) {
    //	Nothing to approve
    if (amount == null || amount.signum() == 0)
        return AD_User_ID;
    //	Starting user
    MUser user = MUser.get(getCtx(), AD_User_ID);
    log.info("For User=" + user + ", Amt=" + amount + ", Own=" + ownDocument);
    MUser oldUser = null;
    while (user != null) {
        if (user.equals(oldUser)) {
            log.info("Loop - " + user.getName());
            return -1;
        }
        oldUser = user;
        log.fine("User=" + user.getName());
        //	Get Roles of User
        MRole[] roles = user.getRoles(AD_Org_ID);
        for (int i = 0; i < roles.length; i++) {
            MRole role = roles[i];
            if (ownDocument && !role.isCanApproveOwnDoc())
                //	find a role with allows them to approve own
                continue;
            BigDecimal roleAmt = role.getAmtApproval();
            if (roleAmt == null || roleAmt.signum() == 0)
                continue;
            if (C_Currency_ID != role.getC_Currency_ID() && //	No currency = amt only
            role.getC_Currency_ID() != 0) {
                roleAmt = //	today & default rate 
                MConversionRate.convert(//	today & default rate 
                getCtx(), roleAmt, role.getC_Currency_ID(), C_Currency_ID, getAD_Client_ID(), AD_Org_ID);
                if (roleAmt == null || roleAmt.signum() == 0)
                    continue;
            }
            boolean approved = amount.compareTo(roleAmt) <= 0;
            log.fine("Approved=" + approved + " - User=" + user.getName() + ", Role=" + role.getName() + ", ApprovalAmt=" + roleAmt);
            if (approved)
                return user.getAD_User_ID();
        }
        //	Get Supervisor
        if (user.getSupervisor_ID() != 0) {
            user = MUser.get(getCtx(), user.getSupervisor_ID());
            log.fine("Supervisor: " + user.getName());
        } else {
            log.fine("No Supervisor");
            MOrg org = MOrg.get(getCtx(), AD_Org_ID);
            MOrgInfo orgInfo = org.getInfo();
            //	Get Org Supervisor
            if (orgInfo.getSupervisor_ID() != 0) {
                user = MUser.get(getCtx(), orgInfo.getSupervisor_ID());
                log.fine("Org=" + org.getName() + ",Supervisor: " + user.getName());
            } else {
                log.fine("No Org Supervisor");
                //	Get Parent Org Supervisor
                if (orgInfo.getParent_Org_ID() != 0) {
                    org = MOrg.get(getCtx(), orgInfo.getParent_Org_ID());
                    orgInfo = org.getInfo();
                    if (orgInfo.getSupervisor_ID() != 0) {
                        user = MUser.get(getCtx(), orgInfo.getSupervisor_ID());
                        log.fine("Parent Org Supervisor: " + user.getName());
                    }
                }
            }
        }
        //	No Supervisor
        //ownDocument should always be false for the next user
        ownDocument = false;
    }
    //	while there is a user to approve
    log.fine("No user found");
    return -1;
}
Also used : MOrg(org.compiere.model.MOrg) MRole(org.compiere.model.MRole) MOrgInfo(org.compiere.model.MOrgInfo) MUser(org.compiere.model.MUser) Savepoint(java.sql.Savepoint) BigDecimal(java.math.BigDecimal)

Example 9 with MRole

use of org.compiere.model.MRole in project adempiere by adempiere.

the class Browser method testCount.

/**
	 * Test Row Count
	 * 
	 * @return > 0 if display
	 */
public int testCount() {
    int no = -1;
    no = getCount();
    // log.fine("#" + no + " - " + (System.currentTimeMillis()-start) +
    // "ms");
    MRole role = MRole.getDefault();
    if (role.isQueryMax(no))
        return no;
    //	Default
    return -1;
}
Also used : MRole(org.compiere.model.MRole)

Example 10 with MRole

use of org.compiere.model.MRole in project adempiere by adempiere.

the class FindWindow method getNoOfRecords.

//  cmd_ok_Advanced
/**
     *  Get the number of records of target tab
     *  @param query where clause for target tab
     *  @param alertZeroRecords show dialog if there are no records
     *  @return number of selected records;
     *          if the results are more then allowed this method will return 0
    **/
private int getNoOfRecords(MQuery query, boolean alertZeroRecords) {
    log.config("" + query);
    StringBuffer sql = new StringBuffer("SELECT COUNT(*) FROM ");
    sql.append(m_tableName);
    boolean hasWhere = false;
    if (m_whereExtended != null && m_whereExtended.length() > 0) {
        sql.append(" WHERE ").append(m_whereExtended);
        hasWhere = true;
    }
    if (query != null && query.isActive()) {
        if (hasWhere)
            sql.append(" AND ");
        else
            sql.append(" WHERE ");
        sql.append(query.getWhereClause());
    }
    //  Add Access
    String finalSQL = MRole.getDefault().addAccessSQL(sql.toString(), m_tableName, MRole.SQL_NOTQUALIFIED, MRole.SQL_RO);
    finalSQL = Env.parseContext(Env.getCtx(), m_targetWindowNo, finalSQL, false);
    Env.setContext(Env.getCtx(), m_targetWindowNo, TABNO, GridTab.CTX_FindSQL, finalSQL);
    //  Execute Qusery
    m_total = 999999;
    try {
        Statement stmt = DB.createStatement();
        ResultSet rs = stmt.executeQuery(finalSQL);
        if (rs.next())
            m_total = rs.getInt(1);
        rs.close();
        stmt.close();
    } catch (SQLException e) {
        log.log(Level.SEVERE, finalSQL, e);
    }
    MRole role = MRole.getDefault();
    //  More then allowed
    if (query != null && role.isQueryMax(m_total)) {
        FDialog.error(m_targetWindowNo, this, "FindOverMax", m_total + " > " + role.getMaxQueryRecords());
        // return 0 if more then allowed - teo_sarca [ 1708717 ]
        m_total = 0;
    } else
        log.config("#" + m_total);
    /*if (query != null)
            statusBar.setStatusToolTip (query.getWhereClause());*/
    return m_total;
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) MRole(org.compiere.model.MRole) ResultSet(java.sql.ResultSet)

Aggregations

MRole (org.compiere.model.MRole)42 PreparedStatement (java.sql.PreparedStatement)14 ResultSet (java.sql.ResultSet)14 SQLException (java.sql.SQLException)11 ArrayList (java.util.ArrayList)7 Point (java.awt.Point)4 Properties (java.util.Properties)4 BigDecimal (java.math.BigDecimal)3 Statement (java.sql.Statement)3 GridField (org.compiere.model.GridField)3 GridTab (org.compiere.model.GridTab)3 QName (javax.xml.namespace.QName)2 MBrowse (org.adempiere.model.MBrowse)2 XFireFault (org.codehaus.xfire.fault.XFireFault)2 ICreateFrom (org.compiere.grid.ICreateFrom)2 MOrgInfo (org.compiere.model.MOrgInfo)2 MProcess (org.compiere.model.MProcess)2 MTable (org.compiere.model.MTable)2 MWFActivity (org.compiere.wf.MWFActivity)2 ADLoginRequest (pl.x3E.adInterface.ADLoginRequest)2