/** * Get Accessible Goals * * @param ctx context * @return array of goals */ public static MGoal[] getGoals(Ctx ctx) { ArrayList<MGoal> list = new ArrayList<MGoal>(); String sql = "SELECT * FROM PA_Goal WHERE IsActive='Y' " + "ORDER BY SeqNo"; sql = MRole.getDefault(ctx, false) .addAccessSQL(sql, "PA_Goal", false, true); // RW to restrict Access PreparedStatement pstmt = null; ResultSet rs = null; try { pstmt = DB.prepareStatement(sql, (Trx) null); rs = pstmt.executeQuery(); while (rs.next()) { MGoal goal = new MGoal(ctx, rs, null); goal.updateGoal(false); list.add(goal); } } catch (Exception e) { s_log.log(Level.SEVERE, sql, e); } finally { DB.closeStatement(pstmt); DB.closeResultSet(rs); } MGoal[] retValue = new MGoal[list.size()]; list.toArray(retValue); return retValue; } // getGoals
public static MInfoWindow get(String tableName, String trxName) { Query query = new Query( Env.getCtx(), MTable.get(Env.getCtx(), MInfoWindow.Table_ID), MInfoWindow.COLUMNNAME_AD_Table_ID + "=? AND IsValid='Y' ", null); MTable table = MTable.get(Env.getCtx(), tableName); if (table != null) { List<MInfoWindow> iws = query .setParameters(table.getAD_Table_ID()) .setOrderBy( "AD_Client_ID Desc, AD_Org_ID Desc, IsDefault Desc, AD_InfoWindow_ID Desc") .setOnlyActiveRecords(true) .setApplyAccessFilter(true) .list(); // verify role has access and return the first with access / IDEMPIERE-893 for (MInfoWindow iw : iws) { Boolean access = MRole.getDefault().getInfoAccess(iw.getAD_InfoWindow_ID()); if (access != null && access.booleanValue()) return iw; } } return null; }
/** * After Save * * @param newRecord new Record * @param success save success * @return */ protected boolean afterSave(boolean newRecord, boolean success) { if (!success) { return success; } if (newRecord) { // Info MOrgInfo oInfo = new MOrgInfo(this); oInfo.save(); // Access MRoleOrgAccess.createForOrg(this); MRole.getDefault(getCtx(), true); // reload // TreeNode insert_Tree(MTree_Base.TREETYPE_Organization); } // Value/Name change if (!newRecord && (is_ValueChanged("Value") || is_ValueChanged("Name"))) { MAccount.updateValueDescription(getCtx(), "AD_Org_ID=" + getAD_Org_ID(), get_TrxName()); if ("Y".equals(Env.getContext(getCtx(), "$Element_OT"))) { MAccount.updateValueDescription(getCtx(), "AD_OrgTrx_ID=" + getAD_Org_ID(), get_TrxName()); } } return true; } // afterSave
public boolean validateSql() { PreparedStatement pstmt = null; ResultSet rs = null; String sql = getSql(); try { String countSql = Msg.parseTranslation(Env.getCtx(), sql.toString()); // Variables countSql = MRole.getDefault() .addAccessSQL( countSql, MTable.getTableName(Env.getCtx(), getAD_Table_ID()), // getTableName(), MRole.SQL_FULLYQUALIFIED, MRole.SQL_RO); pstmt = DB.prepareStatement(countSql, null); // pstmt.setString(1, p_tableName); rs = pstmt.executeQuery(); while (rs.next()) { break; } } catch (SQLException e) { // ADialog.error(WindowNo, c, AD_Message) // Env.getUi().showError(0, null, sql.toString() + "<br> " + e.getMessage()); log.log(Level.SEVERE, sql, e); // String error = e.toString(); // show error to user and return: TODO return false; } finally { DB.close(rs, pstmt); rs = null; pstmt = null; } return true; } // validate sql
@Override protected boolean afterSave(boolean newRecord, boolean success) { if (!success) return success; if (newRecord) // Add to all automatic roles { MRole[] roles = MRole.getOf(getCtx(), "IsManual='N'"); for (int i = 0; i < roles.length; i++) { MInfoWindowAccess wa = new MInfoWindowAccess(this, roles[i].getAD_Role_ID()); wa.saveEx(); } } // Menu else if (is_ValueChanged("IsActive") || is_ValueChanged("Name") || is_ValueChanged("Description")) { MMenu[] menues = MMenu.get(getCtx(), "AD_InfoWindow_ID=" + getAD_InfoWindow_ID(), get_TrxName()); for (int i = 0; i < menues.length; i++) { menues[i].setName(getName()); menues[i].setDescription(getDescription()); menues[i].setIsActive(isActive()); menues[i].saveEx(); } // } return super.afterSave(newRecord, success); }
/** Finalize. Remove Role */ @Override protected void finalize() throws Throwable { if (m_context != null) { int gwtServerID = m_context.getContextAsInt(MRole.GWTSERVERID); if (gwtServerID > 0) MRole.resetGwt(gwtServerID); } super.finalize(); } // finalize
/** * if user haven't right to run a process, set infoProcess to null * * @param lsInfoProcess */ protected void checkProcessRight(List<MInfoProcess> lsInfoProcess) { Iterator<MInfoProcess> iterator = lsInfoProcess.iterator(); while (iterator.hasNext()) { MInfoProcess testInfoProcess = iterator.next(); Boolean access = MRole.getDefault().getProcessAccess(testInfoProcess.getAD_Process_ID()); if (access == null || !access.booleanValue()) { iterator.remove(); } } }
/** * Before Save * * @param newRecord new * @return true */ @Override protected boolean beforeSave(boolean newRecord) { // if (getMultiplier(this) == null) // error // setMeasureDisplay(getMeasureScope()); // Measure required if nor Summary if (!isSummary() && getPA_Measure_ID() == 0) { log.saveError("FillMandatory", Msg.getElement(getCtx(), "PA_Measure_ID")); return false; } if (isSummary() && getPA_Measure_ID() != 0) setPA_Measure_ID(0); // User/Role Check if ((newRecord || is_ValueChanged("AD_User_ID") || is_ValueChanged("AD_Role_ID")) && getAD_User_ID() != 0) { MUser user = MUser.get(getCtx(), getAD_User_ID()); MRole[] roles = user.getRoles(getAD_Org_ID()); if (roles.length == 0) // No Role setAD_Role_ID(0); else if (roles.length == 1) // One setAD_Role_ID(roles[0].getAD_Role_ID()); else { int AD_Role_ID = getAD_Role_ID(); if (AD_Role_ID != 0) // validate { boolean found = false; for (MRole element : roles) { if (AD_Role_ID == element.getAD_Role_ID()) { found = true; break; } } if (!found) AD_Role_ID = 0; } if (AD_Role_ID == 0) // set to first one setAD_Role_ID(roles[0].getAD_Role_ID()); } // multiple roles } // user check return true; } // beforeSave
/** * Get User Goals * * @param ctx context * @param AD_User_ID user * @return array of goals */ public static MGoal[] getUserGoals(Ctx ctx) { int AD_Role_ID = ctx.getAD_Role_ID(); MRole role = MRole.get(ctx, AD_Role_ID); int AD_User_ID = ctx.getAD_User_ID(); if (AD_User_ID < 0) return getTestGoals(ctx); ArrayList<MGoal> list = new ArrayList<MGoal>(); String sql = "SELECT * FROM PA_Goal g " + "WHERE IsActive='Y'" + " AND AD_Client_ID=?" // #1 + " AND ("; if (!role.isWebStoreRole()) sql += " (AD_User_ID IS NULL AND AD_Role_ID IS NULL) OR "; sql += " AD_User_ID=?" // #2 + " OR EXISTS (SELECT * FROM AD_User_Roles ur " + "WHERE ?=ur.AD_User_ID AND g.AD_Role_ID=ur.AD_Role_ID AND ur.IsActive='Y')) " + "ORDER BY SeqNo"; PreparedStatement pstmt = null; ResultSet rs = null; try { pstmt = DB.prepareStatement(sql, (Trx) null); pstmt.setInt(1, ctx.getAD_Client_ID()); pstmt.setInt(2, AD_User_ID); pstmt.setInt(3, AD_User_ID); rs = pstmt.executeQuery(); while (rs.next()) { MGoal goal = new MGoal(ctx, rs, null); goal.updateGoal(false); list.add(goal); } } catch (Exception e) { s_log.log(Level.SEVERE, sql, e); } finally { DB.closeResultSet(rs); DB.closeStatement(pstmt); } MGoal[] retValue = new MGoal[list.size()]; list.toArray(retValue); return retValue; } // getUserGoals
/** * Execute Query for Tab * * @param AD_Tab_ID tab * @param queryVO optional query * @param context record context for link columns and other variables * @param queryResultID stored query identifier provided by client * @return number of records or -1 if error */ public int executeQuery( int AD_Tab_ID, QueryVO queryVO, HashMap<String, String> context, int queryResultID) { UITab tab = getTab(AD_Tab_ID); if (tab == null) { log.config("Not found AD_Tab_ID=" + AD_Tab_ID); return -1; } ArrayList<String[]> result = tab.executeQueryString(queryVO, context, m_context); if (result == null) { log.config("Not Result for AD_Tab_ID=" + AD_Tab_ID); return -1; } MRole role = getRole(); // return -1 to indicate query exceeds if (role.isQueryMax(result.size())) { m_results.put(queryResultID, new ArrayList<String[]>()); return -1; } m_results.put(queryResultID, result); return result.size(); } // executeQuery
/** * Logout * * @param expired expire */ public void logout(boolean expired) { // End Session MSession session = MSession.get(m_context); // finish if (session != null) { if (expired) { if (session.getDescription() == null) session.setDescription("Expired"); else session.setDescription(session.getDescription() + " Expired"); } session.logout(); // saves } if (m_context != null) { int gwtServerID = m_context.getContextAsInt(MRole.GWTSERVERID); if (gwtServerID > 0) MRole.resetGwt(gwtServerID); } // Clear Cache m_tabs.clear(); m_fields.clear(); // m_windows.clear(); m_context.clear(); m_results.clear(); // } // logout
/** * return true if the current role can access to the specified info window ; otherwise return null */ public static MInfoWindow get(int infoWindowID, String trxName) { MInfoWindow iw = new MInfoWindow(Env.getCtx(), infoWindowID, null); Boolean access = MRole.getDefault().getInfoAccess(iw.getAD_InfoWindow_ID()); if (access != null && access.booleanValue()) return iw; return null; }
/** * Invoice - Amount. - called from QtyInvoiced, PriceActual - calculates LineNetAmt * * @param ctx context * @param WindowNo window no * @param mTab tab * @param mField field * @param value value * @return null or error message */ public String amt(Properties ctx, int WindowNo, GridTab mTab, GridField mField, Object value) { if (isCalloutActive() || value == null) return ""; // log.log(Level.WARNING,"amt - init"); int C_UOM_To_ID = Env.getContextAsInt(ctx, WindowNo, "C_UOM_ID"); int M_Product_ID = Env.getContextAsInt(ctx, WindowNo, "M_Product_ID"); int M_PriceList_ID = Env.getContextAsInt(ctx, WindowNo, "M_PriceList_ID"); int StdPrecision = MPriceList.getStandardPrecision(ctx, M_PriceList_ID); BigDecimal QtyEntered, QtyInvoiced, PriceEntered, PriceActual, PriceLimit, Discount, PriceList; // get values QtyEntered = (BigDecimal) mTab.getValue("QtyEntered"); QtyInvoiced = (BigDecimal) mTab.getValue("QtyInvoiced"); log.fine("QtyEntered=" + QtyEntered + ", Invoiced=" + QtyInvoiced + ", UOM=" + C_UOM_To_ID); // PriceEntered = (BigDecimal) mTab.getValue("PriceEntered"); PriceActual = (BigDecimal) mTab.getValue("PriceActual"); // Discount = (BigDecimal)mTab.getValue("Discount"); PriceLimit = (BigDecimal) mTab.getValue("PriceLimit"); PriceList = (BigDecimal) mTab.getValue("PriceList"); log.fine("PriceList=" + PriceList + ", Limit=" + PriceLimit + ", Precision=" + StdPrecision); log.fine( "PriceEntered=" + PriceEntered + ", Actual=" + PriceActual); // + ", Discount=" + Discount); // No Product if (M_Product_ID == 0) { // if price change sync price actual and entered // else ignore if (mField.getColumnName().equals("PriceActual")) { PriceEntered = (BigDecimal) value; mTab.setValue("PriceEntered", value); } else if (mField.getColumnName().equals("PriceEntered")) { PriceActual = (BigDecimal) value; mTab.setValue("PriceActual", value); } } // Product Qty changed - recalc price else if ((mField.getColumnName().equals("QtyInvoiced") || mField.getColumnName().equals("QtyEntered") || mField.getColumnName().equals("C_UOM_ID") || mField.getColumnName().equals("M_Product_ID")) && !"N".equals(Env.getContext(ctx, WindowNo, "DiscountSchema"))) { int C_BPartner_ID = Env.getContextAsInt(ctx, WindowNo, "C_BPartner_ID"); if (mField.getColumnName().equals("QtyEntered")) QtyInvoiced = MUOMConversion.convertProductFrom(ctx, M_Product_ID, C_UOM_To_ID, QtyEntered); if (QtyInvoiced == null) QtyInvoiced = QtyEntered; boolean IsSOTrx = Env.getContext(ctx, WindowNo, "IsSOTrx").equals("Y"); MProductPricing pp = new MProductPricing(M_Product_ID, C_BPartner_ID, QtyInvoiced, IsSOTrx); pp.setM_PriceList_ID(M_PriceList_ID); int M_PriceList_Version_ID = Env.getContextAsInt(ctx, WindowNo, "M_PriceList_Version_ID"); pp.setM_PriceList_Version_ID(M_PriceList_Version_ID); Timestamp date = (Timestamp) mTab.getValue("DateInvoiced"); pp.setPriceDate(date); // PriceEntered = MUOMConversion.convertProductFrom(ctx, M_Product_ID, C_UOM_To_ID, pp.getPriceStd()); if (PriceEntered == null) PriceEntered = pp.getPriceStd(); // log.fine( "amt - QtyChanged -> PriceActual=" + pp.getPriceStd() + ", PriceEntered=" + PriceEntered + ", Discount=" + pp.getDiscount()); PriceActual = pp.getPriceStd(); mTab.setValue("PriceActual", pp.getPriceStd()); // mTab.setValue("Discount", pp.getDiscount()); mTab.setValue("PriceEntered", PriceEntered); Env.setContext(ctx, WindowNo, "DiscountSchema", pp.isDiscountSchema() ? "Y" : "N"); } else if (mField.getColumnName().equals("PriceActual")) { PriceActual = (BigDecimal) value; PriceEntered = MUOMConversion.convertProductFrom(ctx, M_Product_ID, C_UOM_To_ID, PriceActual); if (PriceEntered == null) PriceEntered = PriceActual; // log.fine("amt - PriceActual=" + PriceActual + " -> PriceEntered=" + PriceEntered); mTab.setValue("PriceEntered", PriceEntered); } else if (mField.getColumnName().equals("PriceEntered")) { PriceEntered = (BigDecimal) value; PriceActual = MUOMConversion.convertProductTo(ctx, M_Product_ID, C_UOM_To_ID, PriceEntered); if (PriceActual == null) PriceActual = PriceEntered; // log.fine("amt - PriceEntered=" + PriceEntered + " -> PriceActual=" + PriceActual); mTab.setValue("PriceActual", PriceActual); } /** * Discount entered - Calculate Actual/Entered if (mField.getColumnName().equals("Discount")) { * PriceActual = new BigDecimal ((100.0 - Discount.doubleValue()) / 100.0 * * PriceList.doubleValue()); if (PriceActual.scale() > StdPrecision) PriceActual = * PriceActual.setScale(StdPrecision, BigDecimal.ROUND_HALF_UP); PriceEntered = * MUOMConversion.convertProductFrom (ctx, M_Product_ID, C_UOM_To_ID, PriceActual); if * (PriceEntered == null) PriceEntered = PriceActual; mTab.setValue("PriceActual", PriceActual); * mTab.setValue("PriceEntered", PriceEntered); } // calculate Discount else { if * (PriceList.intValue() == 0) Discount = Env.ZERO; else Discount = new BigDecimal * ((PriceList.doubleValue() - PriceActual.doubleValue()) / PriceList.doubleValue() * 100.0); if * (Discount.scale() > 2) Discount = Discount.setScale(2, BigDecimal.ROUND_HALF_UP); * mTab.setValue("Discount", Discount); } log.fine("amt = PriceEntered=" + PriceEntered + ", * Actual" + PriceActual + ", Discount=" + Discount); /* */ // Check PriceLimit String epl = Env.getContext(ctx, WindowNo, "EnforcePriceLimit"); boolean enforce = Env.isSOTrx(ctx, WindowNo) && epl != null && epl.equals("Y"); if (enforce && MRole.getDefault().isOverwritePriceLimit()) enforce = false; // Check Price Limit? if (enforce && PriceLimit.doubleValue() != 0.0 && PriceActual.compareTo(PriceLimit) < 0) { PriceActual = PriceLimit; PriceEntered = MUOMConversion.convertProductFrom(ctx, M_Product_ID, C_UOM_To_ID, PriceLimit); if (PriceEntered == null) PriceEntered = PriceLimit; log.fine("amt =(under) PriceEntered=" + PriceEntered + ", Actual" + PriceLimit); mTab.setValue("PriceActual", PriceLimit); mTab.setValue("PriceEntered", PriceEntered); mTab.fireDataStatusEEvent("UnderLimitPrice", "", false); // Repeat Discount calc if (PriceList.intValue() != 0) { Discount = new BigDecimal( (PriceList.doubleValue() - PriceActual.doubleValue()) / PriceList.doubleValue() * 100.0); if (Discount.scale() > 2) Discount = Discount.setScale(2, BigDecimal.ROUND_HALF_UP); // mTab.setValue ("Discount", Discount); } } // Line Net Amt BigDecimal LineNetAmt = QtyInvoiced.multiply(PriceActual); if (LineNetAmt.scale() > StdPrecision) LineNetAmt = LineNetAmt.setScale(StdPrecision, BigDecimal.ROUND_HALF_UP); log.info("amt = LineNetAmt=" + LineNetAmt); mTab.setValue("LineNetAmt", LineNetAmt); // Calculate Tax Amount for PO boolean IsSOTrx = "Y".equals(Env.getContext(Env.getCtx(), WindowNo, "IsSOTrx")); if (!IsSOTrx) { BigDecimal TaxAmt = Env .ZERO; // teo_sarca: [ 1656829 ] Problem when there is not tax selected in vendor // invoice if (mField.getColumnName().equals("TaxAmt")) { TaxAmt = (BigDecimal) mTab.getValue("TaxAmt"); } else { Integer taxID = (Integer) mTab.getValue("C_Tax_ID"); if (taxID != null) { int C_Tax_ID = taxID.intValue(); MTax tax = new MTax(ctx, C_Tax_ID, null); TaxAmt = tax.calculateTax(LineNetAmt, isTaxIncluded(WindowNo), StdPrecision); mTab.setValue("TaxAmt", TaxAmt); } } // Add it up mTab.setValue("LineTotalAmt", LineNetAmt.add(TaxAmt)); } return ""; } // amt
/** * Create Window Value Object * * @param ctx context * @param WindowNo window no for ctx * @param AD_Window_ID window id * @param AD_Menu_ID menu id * @return MWindowVO */ public static GridWindowVO create( Properties ctx, int WindowNo, int AD_Window_ID, int AD_Menu_ID) { CLogger.get() .config("#" + WindowNo + " - AD_Window_ID=" + AD_Window_ID + "; AD_Menu_ID=" + AD_Menu_ID); GridWindowVO vo = new GridWindowVO(ctx, WindowNo); vo.AD_Window_ID = AD_Window_ID; // Get Window_ID if required - (used by HTML UI) if (vo.AD_Window_ID == 0 && AD_Menu_ID != 0) { String sql = "SELECT AD_Window_ID, IsSOTrx, IsReadOnly FROM AD_Menu " + "WHERE AD_Menu_ID=? AND Action='W'"; PreparedStatement pstmt = null; ResultSet rs = null; try { pstmt = DB.prepareStatement(sql, null); pstmt.setInt(1, AD_Menu_ID); rs = pstmt.executeQuery(); if (rs.next()) { vo.AD_Window_ID = rs.getInt(1); String IsSOTrx = rs.getString(2); Env.setContext(ctx, WindowNo, "IsSOTrx", (IsSOTrx != null && IsSOTrx.equals("Y"))); // String IsReadOnly = rs.getString(3); if (IsReadOnly != null && IsReadOnly.equals("Y")) vo.IsReadWrite = "Y"; else vo.IsReadWrite = "N"; } } catch (SQLException e) { CLogger.get().log(Level.SEVERE, "Menu", e); return null; } finally { DB.close(rs, pstmt); rs = null; pstmt = null; } CLogger.get().config("AD_Window_ID=" + vo.AD_Window_ID); } // -- Get Window StringBuffer sql = new StringBuffer( "SELECT Name,Description,Help,WindowType, " + "AD_Color_ID,AD_Image_ID,WinHeight,WinWidth, " + "IsSOTrx, AD_Window_UU "); if (Env.isBaseLanguage(vo.ctx, "AD_Window")) sql.append("FROM AD_Window w WHERE w.AD_Window_ID=? AND w.IsActive='Y'"); else sql.append("FROM AD_Window_vt w WHERE w.AD_Window_ID=?") .append(" AND AD_Language='") .append(Env.getAD_Language(vo.ctx)) .append("'"); PreparedStatement pstmt = null; ResultSet rs = null; try { // create statement pstmt = DB.prepareStatement(sql.toString(), null); pstmt.setInt(1, vo.AD_Window_ID); // get data rs = pstmt.executeQuery(); if (rs.next()) { vo.Name = rs.getString(1); vo.Description = rs.getString(2); if (vo.Description == null) vo.Description = ""; vo.Help = rs.getString(3); if (vo.Help == null) vo.Help = ""; vo.WindowType = rs.getString(4); // vo.AD_Color_ID = rs.getInt(5); vo.AD_Image_ID = rs.getInt(6); // vo.IsReadWrite = rs.getString(7); // vo.WinHeight = rs.getInt(7); vo.WinWidth = rs.getInt(8); // vo.IsSOTrx = "Y".equals(rs.getString(9)); vo.AD_Window_UU = rs.getString(10); } else vo = null; } catch (SQLException ex) { CLogger.get().log(Level.SEVERE, sql.toString(), ex); return null; } finally { DB.close(rs, pstmt); rs = null; pstmt = null; } // Ensure ASP exceptions MRole role = MRole.getDefault(ctx, false); final Boolean windowAccess = vo != null ? role.getWindowAccess(vo.AD_Window_ID) : null; if (vo != null && windowAccess == null) vo = null; // Not found if (vo != null && windowAccess != null) vo.IsReadWrite = (windowAccess.booleanValue() ? "Y" : "N"); if (vo == null) { CLogger.get() .log( Level.SEVERE, "No Window - AD_Window_ID=" + AD_Window_ID + ", AD_Role_ID=" + role + " - " + sql); CLogger.get().saveError("AccessTableNoView", "(Not found)"); return null; } // Read Write if (vo.IsReadWrite == null) { CLogger.get().saveError("AccessTableNoView", "(found)"); return null; } // FR IDEMPIERE-177 // User Customization MUserDefWin userDef = MUserDefWin.getBestMatch(ctx, AD_Window_ID); if (userDef != null) { if (userDef.getName() != null) vo.Name = userDef.getName(); if (userDef.getDescription() != null) vo.Description = userDef.getDescription(); if (userDef.getHelp() != null) vo.Help = userDef.getHelp(); // ToDo userDef.isDefault, userDef.isReadOnly, userDef.isUserUpdateable } // Create Tabs createTabs(vo); if (vo.Tabs == null || vo.Tabs.size() == 0) return null; return vo; } // create