Search in sources :

Example 1 with MReference

use of org.compiere.model.MReference in project idempiere by idempiere.

the class AEnv method actionZoom.

/**
 * @param lookup
 * @param value
 */
public static void actionZoom(Lookup lookup, Object value) {
    if (lookup == null)
        return;
    // 
    MQuery zoomQuery = lookup.getZoomQuery();
    // still null means the field is empty or not selected item
    if (value == null)
        value = -1;
    // If not already exist or exact value
    if (zoomQuery == null || value != null) {
        // ColumnName might be changed in MTab.validateQuery
        zoomQuery = new MQuery();
        String column = lookup.getColumnName();
        // Check if it is a List Reference
        if (lookup instanceof MLookup) {
            int AD_Reference_ID = ((MLookup) lookup).getAD_Reference_Value_ID();
            if (AD_Reference_ID > 0) {
                MReference reference = MReference.get(AD_Reference_ID);
                if (reference.getValidationType().equals(MReference.VALIDATIONTYPE_ListValidation)) {
                    column = "AD_Ref_List_ID";
                    value = DB.getSQLValue(null, "SELECT AD_Ref_List_ID FROM AD_Ref_List WHERE AD_Reference_ID=? AND Value=?", AD_Reference_ID, value);
                }
            }
        }
        // strip off table name, fully qualify name doesn't work when zoom into detail tab
        if (column.indexOf(".") > 0) {
            int p = column.indexOf(".");
            String tableName = column.substring(0, p);
            column = column.substring(column.indexOf(".") + 1);
            zoomQuery.setZoomTableName(tableName);
            zoomQuery.setZoomColumnName(column);
        } else {
            zoomQuery.setZoomColumnName(column);
            // remove _ID to get table name
            zoomQuery.setZoomTableName(column.substring(0, column.length() - 3));
        }
        zoomQuery.setZoomValue(value);
        zoomQuery.addRestriction(column, MQuery.EQUAL, value);
        // guess
        zoomQuery.setRecordCount(1);
    }
    if (value instanceof Integer && ((Integer) value).intValue() >= 0 && zoomQuery != null && zoomQuery.getZoomTableName() != null) {
        int tableId = MTable.getTable_ID(zoomQuery.getZoomTableName());
        zoom(tableId, ((Integer) value).intValue(), zoomQuery, lookup.getWindowNo());
    } else {
        int windowId = lookup.getZoom(zoomQuery);
        zoom(windowId, zoomQuery, lookup.getWindowNo());
    }
}
Also used : MLookup(org.compiere.model.MLookup) MQuery(org.compiere.model.MQuery) MReference(org.compiere.model.MReference)

Example 2 with MReference

use of org.compiere.model.MReference in project idempiere by idempiere.

the class ReferenceElementHandler method startElement.

public void startElement(PIPOContext ctx, Element element) throws SAXException {
    String entitytype = getStringValue(element, "EntityType");
    if (isProcessElement(ctx.ctx, entitytype)) {
        MReference mReference = findPO(ctx, element);
        if (mReference == null) {
            mReference = new MReference(ctx.ctx, 0, getTrxName(ctx));
        }
        List<String> excludes = defaultExcludeList(MReference.Table_Name);
        PoFiller filler = new PoFiller(ctx, mReference, element, this);
        List<String> notfounds = filler.autoFill(excludes);
        if (notfounds.size() > 0) {
            element.defer = true;
            element.unresolved = notfounds.toString();
            return;
        }
        element.recordId = mReference.getAD_Reference_ID();
        if (mReference.is_new() || mReference.is_Changed()) {
            X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, MReference.Table_Name, MReference.Table_ID);
            String action = null;
            if (!mReference.is_new()) {
                if (references.contains(mReference.getAD_Reference_ID())) {
                    element.skip = true;
                    return;
                }
                backupRecord(ctx, impDetail.getAD_Package_Imp_ID(), MReference.Table_Name, mReference);
                action = "Update";
            } else {
                action = "New";
            }
            if (mReference.save(getTrxName(ctx)) == true) {
                logImportDetail(ctx, impDetail, 1, mReference.getName(), mReference.get_ID(), action);
                references.add(mReference.getAD_Reference_ID());
                element.recordId = mReference.getAD_Reference_ID();
            } else {
                logImportDetail(ctx, impDetail, 0, mReference.getName(), mReference.get_ID(), action);
                throw new POSaveFailedException("Failed to save Reference " + mReference.getName());
            }
        }
    } else {
        element.skip = true;
    }
}
Also used : PoFiller(org.adempiere.pipo2.PoFiller) POSaveFailedException(org.adempiere.pipo2.exception.POSaveFailedException) MReference(org.compiere.model.MReference) X_AD_Package_Imp_Detail(org.compiere.model.X_AD_Package_Imp_Detail)

Example 3 with MReference

use of org.compiere.model.MReference in project idempiere by idempiere.

the class ReferenceElementHandler method create.

public void create(PIPOContext ctx, TransformerHandler document) throws SAXException {
    int Reference_id = Env.getContextAsInt(ctx.ctx, MReference.COLUMNNAME_AD_Reference_ID);
    if (ctx.packOut.isExported(MReference.COLUMNNAME_AD_Reference_ID + "|" + Reference_id))
        return;
    AttributesImpl atts = new AttributesImpl();
    MReference m_Reference = MReference.get(ctx.ctx, Reference_id);
    boolean createElement = isPackOutElement(ctx, m_Reference);
    PackOut packOut = ctx.packOut;
    packOut.getCtx().ctx.put("Table_Name", MReference.Table_Name);
    if (createElement) {
        verifyPackOutRequirement(m_Reference);
        addTypeName(atts, "table");
        document.startElement("", "", I_AD_Reference.Table_Name, atts);
        createReferenceBinding(ctx, document, m_Reference);
        try {
            new CommonTranslationHandler().packOut(packOut, document, null, m_Reference.get_ID());
        } catch (Exception e) {
            if (log.isLoggable(Level.INFO))
                log.info(e.toString());
        }
    }
    if (MReference.VALIDATIONTYPE_ListValidation.equals(m_Reference.getValidationType())) {
        int[] rls = DB.getIDsEx(getTrxName(ctx), "SELECT AD_Ref_List_ID FROM AD_Ref_List WHERE AD_Reference_ID=?", Reference_id);
        for (int rl : rls) {
            createReferenceList(ctx, document, rl);
        }
    } else if (MReference.VALIDATIONTYPE_TableValidation.equals(m_Reference.getValidationType())) {
        createReferenceTable(ctx, document, Reference_id);
    }
    if (createElement) {
        document.endElement("", "", MReference.Table_Name);
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) PackOut(org.adempiere.pipo2.PackOut) MReference(org.compiere.model.MReference) SAXException(org.xml.sax.SAXException) POSaveFailedException(org.adempiere.pipo2.exception.POSaveFailedException)

Example 4 with MReference

use of org.compiere.model.MReference in project idempiere by idempiere.

the class ReferenceTableElementHandler method startElement.

public void startElement(PIPOContext ctx, Element element) throws SAXException {
    List<String> excludes = defaultExcludeList(X_AD_Ref_Table.Table_Name);
    String entitytype = getStringValue(element, "EntityType");
    if (isProcessElement(ctx.ctx, entitytype)) {
        if (isParentSkip(element, null)) {
            element.skip = true;
            return;
        }
        X_AD_Ref_Table refTable = findPO(ctx, element);
        if (refTable == null) {
            refTable = new X_AD_Ref_Table(ctx.ctx, 0, getTrxName(ctx));
        }
        String action = refTable.is_new() ? "New" : "Update";
        PoFiller filler = new PoFiller(ctx, refTable, element, this);
        List<String> notfounds = filler.autoFill(excludes);
        if (notfounds.size() > 0) {
            element.defer = true;
            element.unresolved = notfounds.toString();
            return;
        }
        Element displayElement = element.properties.get("AD_Display");
        int displayColumnId = ReferenceUtils.resolveReference(ctx.ctx, displayElement, getTrxName(ctx));
        refTable.setAD_Display(displayColumnId);
        Element keyElement = element.properties.get("AD_Key");
        int keyColumnId = ReferenceUtils.resolveReference(ctx.ctx, keyElement, getTrxName(ctx));
        refTable.setAD_Key(keyColumnId);
        if (refTable.is_new() || refTable.is_Changed()) {
            refTable.saveEx();
            X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, X_AD_Ref_Table.Table_Name, X_AD_Ref_Table.Table_ID);
            int AD_Reference_ID = refTable.getAD_Reference_ID();
            MReference adReference = new MReference(ctx.ctx, AD_Reference_ID, getTrxName(ctx));
            logImportDetail(ctx, impDetail, 1, adReference.getName(), refTable.getAD_Reference_ID(), action);
        }
    } else {
        element.skip = true;
    }
}
Also used : PoFiller(org.adempiere.pipo2.PoFiller) X_AD_Ref_Table(org.compiere.model.X_AD_Ref_Table) Element(org.adempiere.pipo2.Element) MReference(org.compiere.model.MReference) X_AD_Package_Imp_Detail(org.compiere.model.X_AD_Package_Imp_Detail)

Example 5 with MReference

use of org.compiere.model.MReference in project idempiere by idempiere.

the class ModelADServiceImpl method getList.

public WindowTabDataDocument getList(ModelGetListRequestDocument req) {
    try {
        getCompiereService().connect();
        WindowTabDataDocument resdoc = WindowTabDataDocument.Factory.newInstance();
        WindowTabData res = resdoc.addNewWindowTabData();
        DataSet ds = res.addNewDataSet();
        ModelGetList modelGetList = req.getModelGetListRequest().getModelGetList();
        String serviceType = modelGetList.getServiceType();
        int cnt = 0;
        ADLoginRequest reqlogin = req.getModelGetListRequest().getADLoginRequest();
        String err = login(reqlogin, webServiceName, "getList", serviceType);
        if (err != null && err.length() > 0) {
            res.setError(err);
            res.setErrorInfo(err);
            res.setSuccess(false);
            return resdoc;
        }
        int roleid = reqlogin.getRoleID();
        // Validate parameters
        try {
            modelGetList.setADReferenceID(validateParameter("AD_Reference_ID", modelGetList.getADReferenceID()));
        } catch (XmlValueOutOfRangeException e) {
            // Catch the exception when the Reference ID is not an Integer
            String refUU = getUUIDValue(modelGetList.xgetADReferenceID());
            if (refUU == null) {
                throw e;
            }
            modelGetList.setADReferenceID(validateParameter("AD_Reference_ID", 0, refUU));
        }
        modelGetList.setFilter(validateParameter("Filter", modelGetList.getFilter()));
        int ref_id = modelGetList.getADReferenceID();
        String filter = modelGetList.getFilter();
        if (filter == null || filter.length() == 0)
            filter = "";
        else
            filter = " AND " + filter;
        CompiereService m_cs = getCompiereService();
        Properties ctx = m_cs.getCtx();
        MReference ref = MReference.get(ctx, ref_id);
        String sql = null;
        ArrayList<String> listColumnNames = new ArrayList<String>();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        MWebServiceType m_webservicetype = getWebServiceType();
        if (MReference.VALIDATIONTYPE_ListValidation.equals(ref.getValidationType())) {
            // Fill List Reference
            String ad_language = Env.getAD_Language(ctx);
            boolean isBaseLanguage = Env.isBaseLanguage(ad_language, "AD_Ref_List");
            sql = isBaseLanguage ? "SELECT AD_Ref_List.AD_Ref_List_ID, AD_Ref_List.Value, AD_Ref_List.Name, AD_Ref_List.Description " + "FROM AD_Ref_List " + "WHERE AD_Ref_List.AD_Reference_ID=? AND AD_Ref_List.IsActive='Y' " + filter + " ORDER BY AD_Ref_List.Name" : "SELECT AD_Ref_List.AD_Ref_List_ID, AD_Ref_List.Value, AD_Ref_List_Trl.Name, AD_Ref_List_Trl.Description " + "FROM AD_Ref_List, AD_Ref_List_Trl " + "WHERE AD_Ref_List.AD_Reference_ID=? AND AD_Ref_List.IsActive='Y' AND AD_Ref_List_Trl.AD_Language=? AND AD_Ref_List.AD_Ref_List_ID=AD_Ref_List_Trl.AD_Ref_List_ID " + filter + " ORDER BY AD_Ref_List_Trl.Name";
            listColumnNames.add("AD_Ref_List_ID");
            listColumnNames.add("Value");
            listColumnNames.add("Name");
            listColumnNames.add("Description");
            try {
                pstmt = DB.prepareStatement(sql, null);
                pstmt.setInt(1, ref_id);
                if (!isBaseLanguage)
                    pstmt.setString(2, ad_language);
                rs = pstmt.executeQuery();
            } catch (SQLException e) {
                res.setError(e.getMessage());
                res.setErrorInfo(sql);
                res.setSuccess(false);
                DB.close(rs, pstmt);
                rs = null;
                pstmt = null;
                throw new IdempiereServiceFault(e.getClass().toString() + " " + e.getMessage() + " sql=" + sql, e.getCause(), new QName("getList"));
            }
        } else if (MReference.VALIDATIONTYPE_TableValidation.equals(ref.getValidationType())) {
            // Fill values from a reference table
            MRole role = MRole.get(ctx, roleid);
            MRefTable rt = MRefTable.get(ctx, ref_id);
            if (rt == null || rt.get_ID() == 0)
                throw new IdempiereServiceFault("Web service type " + m_webservicetype.getValue() + ": reference table " + ref_id + " not found", new QName("getList"));
            MTable table = new MTable(ctx, rt.getAD_Table_ID(), null);
            MColumn column = new MColumn(ctx, rt.getAD_Key(), null);
            // TODO: if any value or identifier column is translated, then get them from trl table (and client has multilanguage documents enabled)
            sql = "SELECT " + column.getColumnName();
            listColumnNames.add(column.getColumnName());
            if (rt.isValueDisplayed()) {
                sql += ",Value";
                listColumnNames.add("Value");
            }
            String sqlident = "SELECT ColumnName FROM AD_Column WHERE AD_Table_ID=? AND IsActive='Y' AND IsIdentifier='Y' ORDER BY SeqNo";
            PreparedStatement pstmtident = null;
            ResultSet rsident = null;
            try {
                pstmtident = DB.prepareStatement(sqlident, null);
                pstmtident.setInt(1, rt.getAD_Table_ID());
                rsident = pstmtident.executeQuery();
                while (rsident.next()) {
                    String colnameident = rsident.getString("ColumnName");
                    if (rt.isValueDisplayed() && colnameident.equalsIgnoreCase("Value")) {
                    // Value already added
                    } else {
                        sql += "," + colnameident;
                        listColumnNames.add(colnameident);
                    }
                }
            } catch (Exception e) {
            // ignore this exception
            } finally {
                DB.close(rsident, pstmtident);
                rsident = null;
                pstmtident = null;
            }
            sql += " FROM " + table.getTableName() + " WHERE IsActive='Y'";
            sql = role.addAccessSQL(sql, table.getTableName(), true, true);
            sql += filter;
            if (rt.getWhereClause() != null && rt.getWhereClause().length() > 0)
                sql += " AND " + rt.getWhereClause();
            if (rt.getOrderByClause() != null && rt.getOrderByClause().length() > 0)
                sql += " ORDER BY " + rt.getOrderByClause();
            try {
                pstmt = DB.prepareStatement(sql, null);
                rs = pstmt.executeQuery();
            } catch (SQLException e) {
                res.setError(e.getMessage());
                res.setErrorInfo(sql);
                res.setSuccess(false);
                DB.close(rs, pstmt);
                rs = null;
                pstmt = null;
                throw new IdempiereServiceFault(e.getClass().toString() + " " + e.getMessage() + " sql=" + sql, e.getCause(), new QName("getList"));
            }
        } else {
        // Don't fill - wrong type
        }
        if (rs != null) {
            try {
                while (rs.next()) {
                    cnt++;
                    // Add values to the dataset
                    DataRow dr = ds.addNewDataRow();
                    for (String listColumnName : listColumnNames) {
                        if (m_webservicetype.isOutputColumnNameAllowed(listColumnName)) {
                            DataField dfid = dr.addNewField();
                            dfid.setColumn(listColumnName);
                            dfid.setVal(rs.getString(listColumnName));
                        }
                    }
                }
                res.setSuccess(true);
            } catch (SQLException e) {
                res.setError(e.getMessage());
                res.setErrorInfo(sql);
                res.setSuccess(false);
                throw new IdempiereServiceFault(e.getClass().toString() + " " + e.getMessage() + " sql=" + sql, e.getCause(), new QName("getList"));
            } finally {
                DB.close(rs, pstmt);
                rs = null;
                pstmt = null;
            }
        }
        res.setRowCount(cnt);
        res.setNumRows(cnt);
        res.setTotalRows(cnt);
        res.setStartRow(1);
        return resdoc;
    } finally {
        getCompiereService().disconnect();
    }
}
Also used : ADLoginRequest(org.idempiere.adInterface.x10.ADLoginRequest) MWebServiceType(org.idempiere.webservices.model.MWebServiceType) DataSet(org.idempiere.adInterface.x10.DataSet) SQLException(java.sql.SQLException) MRole(org.compiere.model.MRole) ArrayList(java.util.ArrayList) ModelGetList(org.idempiere.adInterface.x10.ModelGetList) Properties(java.util.Properties) DataRow(org.idempiere.adInterface.x10.DataRow) MRefTable(org.compiere.model.MRefTable) WindowTabDataDocument(org.idempiere.adInterface.x10.WindowTabDataDocument) ResultSet(java.sql.ResultSet) MReference(org.compiere.model.MReference) MColumn(org.compiere.model.MColumn) IdempiereServiceFault(org.idempiere.webservices.fault.IdempiereServiceFault) QName(javax.xml.namespace.QName) WindowTabData(org.idempiere.adInterface.x10.WindowTabData) PreparedStatement(java.sql.PreparedStatement) XmlValueOutOfRangeException(org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException) SQLException(java.sql.SQLException) XmlValueOutOfRangeException(org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException) MTable(org.compiere.model.MTable) DataField(org.idempiere.adInterface.x10.DataField)

Aggregations

MReference (org.compiere.model.MReference)5 PoFiller (org.adempiere.pipo2.PoFiller)2 POSaveFailedException (org.adempiere.pipo2.exception.POSaveFailedException)2 X_AD_Package_Imp_Detail (org.compiere.model.X_AD_Package_Imp_Detail)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 QName (javax.xml.namespace.QName)1 Element (org.adempiere.pipo2.Element)1 PackOut (org.adempiere.pipo2.PackOut)1 XmlValueOutOfRangeException (org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException)1 MColumn (org.compiere.model.MColumn)1 MLookup (org.compiere.model.MLookup)1 MQuery (org.compiere.model.MQuery)1 MRefTable (org.compiere.model.MRefTable)1 MRole (org.compiere.model.MRole)1 MTable (org.compiere.model.MTable)1 X_AD_Ref_Table (org.compiere.model.X_AD_Ref_Table)1