Example usage for java.sql Timestamp toString

List of usage examples for java.sql Timestamp toString

Introduction

In this page you can find the example usage for java.sql Timestamp toString.

Prototype

@SuppressWarnings("deprecation")
public String toString() 

Source Link

Document

Formats a timestamp in JDBC timestamp escape format.

Usage

From source file:com.krawler.esp.portalmsg.Mail.java

public static String insertMailMsg(Connection conn, String to_id, String poster_id, String post_subject,
        String post_text, String folder, Boolean readflag, String last_folder_id, String reply_to,
        String sendflag, String post_id1, int draft, String fid, String companyid)
        throws ServiceException, ParseException, JSONException {
    String query;//  w w  w.j a  v  a2s. c  o m
    String post_id = "";
    String temp = "";
    JSONObject jobj = new JSONObject();
    String usr = to_id;
    DbResults rs1 = null;
    DbResults rs2 = null;
    int numRows = 0;
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    java.util.Date d = new java.util.Date();
    java.sql.Timestamp sqlPostDate = new java.sql.Timestamp(d.getTime());
    to_id = StringUtil.serverHTMLStripper(to_id);
    poster_id = StringUtil.serverHTMLStripper(poster_id);
    reply_to = StringUtil.serverHTMLStripper(reply_to);
    post_subject = StringUtil.serverHTMLStripper(post_subject);
    folder = StringUtil.serverHTMLStripper(folder);
    last_folder_id = StringUtil.serverHTMLStripper(last_folder_id);
    sendflag = StringUtil.serverHTMLStripper(sendflag);
    fid = StringUtil.serverHTMLStripper(fid);
    post_id1 = StringUtil.serverHTMLStripper(post_id1);
    UserDAO userDao = new UserDAOImpl();
    if (sendflag.compareTo("reply") == 0) {
        if (draft == 1) {
            post_id = UUID.randomUUID().toString();
            folder = getDraftId(poster_id);//"3";
            last_folder_id = "0";
            readflag = false;
            reply_to = "";
            if (fid.compareTo("3") != 0) {
                if (StringUtil.isNullOrEmpty(to_id) || StringUtil.isNullOrEmpty(poster_id)) {
                    return KWLErrorMsgs.exSuccessFail;
                }
                query = "Insert into mailmessages (post_id, to_id, poster_id, post_subject, post_text, post_time, flag, folder, reply_to, last_folder_id, readflag) values ( ?, ?, ?, ?, ?,?,false, ?, ?, ?, ?)";
                post_id = UUID.randomUUID().toString();
                numRows = DbUtil.executeUpdate(conn, query, new Object[] { post_id, to_id, poster_id,
                        post_subject, post_text, sqlPostDate, folder, reply_to, last_folder_id, readflag });
            } else {
                if (StringUtil.isNullOrEmpty(post_id1)) {
                    return KWLErrorMsgs.exSuccessFail;
                }
                query = "Update mailmessages set post_subject=?, post_text=? where post_id=?";
                numRows = DbUtil.executeUpdate(conn, query, new Object[] { post_subject, post_text, post_id1 });
            }
        } else if (draft == 2) {
            if (StringUtil.isNullOrEmpty(post_id1) || StringUtil.isNullOrEmpty(to_id)
                    || StringUtil.isNullOrEmpty(poster_id)) {
                return KWLErrorMsgs.exSuccessFail;
            }
            //                query = "SELECT users.userid FROM users inner join userlogin on users.userid=userlogin.userid where userlogin.username=? and users.companyid = ?;";
            //                rs2 = DbUtil.executeQuery(conn, query, new Object[]{post_id1, companyid});
            //                if (rs2.next()) {
            to_id = userDao.getUserID(conn, post_id1, companyid);//(rs2.getString(1));
            //                }
            query = "Insert into mailmessages (post_id, to_id, poster_id, post_subject, post_text, post_time, flag, folder, reply_to, last_folder_id, readflag) values ( ?, ?, ?, ?, ?, ?,false, ?, ?, ?, ?)";
            post_id = UUID.randomUUID().toString();
            numRows = DbUtil.executeUpdate(conn, query, new Object[] { post_id, to_id, poster_id, post_subject,
                    post_text, sqlPostDate, folder, reply_to, last_folder_id, readflag });
            post_id = UUID.randomUUID().toString();
            folder = "0";
            last_folder_id = "0";
            readflag = false;
            reply_to = "";
            query = "Insert into mailmessages (post_id, to_id, poster_id, post_subject, post_text, post_time, flag, folder, reply_to, last_folder_id, readflag) values ( ?, ?, ?, ?, ?, ?,false, ?, ?, ?, ?)";
            numRows = DbUtil.executeUpdate(conn, query, new Object[] { post_id, to_id, poster_id, post_subject,
                    post_text, sqlPostDate, folder, reply_to, last_folder_id, readflag });
        } else {
            query = "SELECT poster_id FROM mailmessages where post_id=?";
            DbResults rs = DbUtil.executeQuery(conn, query, post_id1);
            while (rs.next()) {
                to_id = rs.getString(1);
            }
        }
    } else {
        if (draft != 1) {
            if (draft == 3) {
                if (post_id1.contains("@")) {
                    query = "SELECT userid FROM users where emailid=?;";
                    rs2 = DbUtil.executeQuery(conn, query, to_id);
                    if (rs2.next()) {
                        to_id = (rs2.getString(1));
                    } else {
                        to_id = "-1"; //for invalid username
                    }
                } else {
                    query = "SELECT users.userid FROM users inner join userlogin on users.userid =userlogin.userid where userlogin.username=? and users.companyid = ?;";
                    rs2 = DbUtil.executeQuery(conn, query, new Object[] { to_id, companyid });
                    if (rs2.next()) {
                        to_id = (rs2.getString(1));
                    } else {
                        to_id = "-1"; //for invalid username
                    }
                }
            }
            if (to_id.compareTo("-1") != 0) {
                if (StringUtil.isNullOrEmpty(to_id) || StringUtil.isNullOrEmpty(poster_id)) {
                    return KWLErrorMsgs.exSuccessFail;
                }
                query = "Insert into mailmessages (post_id, to_id, poster_id, post_subject, post_text, post_time, flag, folder, reply_to, last_folder_id, readflag) values ( ?, ?, ?, ?, ?, ?,false, ?, ?, ?, ?)";
                post_id = UUID.randomUUID().toString();
                numRows = DbUtil.executeUpdate(conn, query, new Object[] { post_id, to_id, poster_id,
                        post_subject, post_text, sqlPostDate, folder, reply_to, last_folder_id, readflag });
                post_id = UUID.randomUUID().toString();
                folder = "0";
                last_folder_id = "0";
                readflag = false;
                reply_to = "";
            }
        } else {
            post_id = UUID.randomUUID().toString();
            folder = getDraftId(poster_id);//"3";
            last_folder_id = getDraftId(poster_id);
            readflag = false;
            reply_to = "";
            query = "SELECT users.userid FROM users inner join userlogin on users.userid =userlogin.userid where userlogin.username=? and users.companyid = ?;";
            rs2 = DbUtil.executeQuery(conn, query, new Object[] { to_id, companyid });

            if (rs2.next()) {
                to_id = (rs2.getString(1));
            } else {
                to_id = poster_id;
            }
        }
        if (to_id.compareTo("-1") != 0) {
            if (StringUtil.isNullOrEmpty(to_id) || StringUtil.isNullOrEmpty(poster_id)) {
                return KWLErrorMsgs.exSuccessFail;
            }
            query = "Insert into mailmessages (post_id, to_id, poster_id, post_subject, post_text, post_time, flag, folder, reply_to, last_folder_id, readflag) values ( ?, ?, ?, ?, ?, ?,false, ?, ?, ?, ?)";
            numRows = DbUtil.executeUpdate(conn, query, new Object[] { post_id, to_id, poster_id, post_subject,
                    post_text, sqlPostDate, folder, reply_to, last_folder_id, readflag });
        }
    }
    if (numRows == 0) {
        if (to_id.compareTo("-1") == 0) {
            com.krawler.utils.json.base.JSONObject jtemp = new com.krawler.utils.json.base.JSONObject();
            jtemp.put("Success", "userfail");
            jtemp.put("Subject", usr);
            jobj.append("data", jtemp);
            return jobj.toString();
        } else {
            return KWLErrorMsgs.exSuccessFail;
        }
    } else {
        String dateTime = "";
        String UserName = "";
        String Image = "";
        query = "SELECT userlogin.username,image FROM users inner join userlogin on users.userid=userlogin.userid where users.userid=?;";
        rs2 = DbUtil.executeQuery(conn, query, poster_id);
        if (rs2.next()) {
            UserName = (rs2.getString(1));
            Image = (rs2.getString(2));
        }
        if (draft == 1 && sendflag.compareTo("reply") == 0) {
            query = "SELECT post_time FROM mailmessages where post_id=?;";
            rs1 = DbUtil.executeQuery(conn, query, post_id1);
            if (rs1.next()) {
                dateTime = (rs1.getObject(1).toString());
            }
        } else {
            query = "SELECT post_time FROM mailmessages where post_id=?;";
            rs1 = DbUtil.executeQuery(conn, query, post_id);
            if (rs1.next()) {
                dateTime = (rs1.getObject(1).toString());
            }
        }
        String userTime = Timezone.toCompanyTimezone(conn, sqlPostDate.toString(), companyid);
        java.util.Date tempdate = sdf.parse(userTime);
        java.text.SimpleDateFormat sdf1 = new java.text.SimpleDateFormat("yyyy-MM-dd h:mm a");
        JSONStringer j = new JSONStringer();
        com.krawler.utils.json.base.JSONObject jtemp = new com.krawler.utils.json.base.JSONObject();
        String success = "Success";
        if (folder.compareTo("3") == 0) {
            success = "Draft";
        }
        jtemp.put("Success", success);
        jtemp.put("post_time", sdf1.format(tempdate).toString());
        jtemp.put("flag", "false");
        jtemp.put("post_id", post_id);
        jtemp.put("post_subject", post_subject);
        jtemp.put("post_text", "");
        jtemp.put("poster_id", UserName);
        jtemp.put("readflag", "0");
        jtemp.put("imgsrc", Image);
        jtemp.put("senderid", poster_id);
        jobj.append("data", jtemp);
        /*temp = j.object().key("Success").value("Success").key("post_time")
        .value(sdf1.format(tempdate).toString()).key("flag").value(
        "false").key("post_id").value(post_id).key(
        "post_subject").value(post_subject)
        .key("post_text").value("").key("poster_id")
        .value(UserName).key("readflag").value("0").key("imgsrc")
        .value(Image).key("senderid").value(poster_id).endObject()
        .toString();*/
    }
    return jobj.toString();
}

From source file:org.ofbiz.tools.rest.FixOfcTools.java

/**
 *  ?Xls?/*from  w  ww  . j  ava 2 s .com*/
 * @param filePathAndName
 */

public static void xls2XmlForProduct(String filePathAndName) {

    long featureIdx = 2000;
    String featurePrefix = "FT-";
    long invetoryIdx = 4000;
    Timestamp now = new Timestamp(System.currentTimeMillis());
    //??
    String temp_Product = "<Product productId=\"#11#\" productName=\"#21#\" internalName=\"#41#\" primaryProductCategoryId=\"#31#\" productTypeId=\"FINISHED_GOOD\"  taxable=\"N\" chargeShipping=\"N\" autoCreateKeywords=\"Y\" isVirtual=\"N\" isVariant=\"N\" introductionDate=\"#51#\" salesDiscontinuationDate=\"#61#\" createdDate=\"#71#\" quantityUomId=\"#81#\" createdByUserLogin=\"admin\" />";

    String temp_ProductRole = "<ProductRole productId=\"#11#\" partyId=\"CompanyCq\" roleTypeId=\"SHIP_FROM_VENDOR\" fromDate=\"2001-05-13 12:00:00.0\"/> ";

    String temp_ProductFeature = "<ProductFeature productFeatureId=\"#11#\" productFeatureTypeId=\"#21#\" productFeatureCategoryId=\"#31#\" description=\"#41#\"/>";
    String temp_ProductFeatureAppl = "<ProductFeatureAppl productId=\"#11#\" productFeatureId=\"#21#\" productFeatureApplTypeId=\"STANDARD_FEATURE\" fromDate=\"#31#\" />";
    String temp_ProductCategoryMember = "<ProductCategoryMember productId=\"#11#\" productCategoryId=\"#21#\" fromDate=\"2001-05-13 12:00:00.000\"/>";
    String temp_ProductPrice = "<ProductPrice productId=\"#11#\" productPricePurposeId=\"#41#\" productPriceTypeId=\"#21#\" currencyUomId=\"CNY\" productStoreGroupId=\"_NA_\" fromDate=\"2001-05-13 12:00:00.0\" price=\"#31#\"/>";
    String temp_ProductAttribute = "<ProductAttribute productId=\"#11#\" attrName=\"#21#\"><attrValue><![CDATA[ #31# ]]></attrValue></ProductAttribute>";

    String temp_InventoryItem = "<InventoryItem facilityId=\"WebStoreWarehouse\" inventoryItemId=\"#11#\" inventoryItemTypeId=\"NON_SERIAL_INV_ITEM\" datetimeReceived=\"2008-08-01 08:00:00.000\" productId=\"#21#\" ownerPartyId=\"CompanyCq\" currencyUomId=\"CNY\" unitCost=\"200\"/>";
    String temp_InventoryItemDetail = "<InventoryItemDetail inventoryItemId=\"#11#\" inventoryItemDetailSeqId=\"0001\" effectiveDate=\"2001-05-13 12:00:00.0\" availableToPromiseDiff=\"228\" quantityOnHandDiff=\"228\" accountingQuantityDiff=\"228\"/>";

    Map<String, String> productMap = new HashMap<String, String>();
    Map<String, String> productRoleMap = new HashMap<String, String>();
    Map<String, String> inventoryItemMap = new HashMap<String, String>();
    Map<String, String> inventoryItemDetailMap = new HashMap<String, String>();
    Map<String, String> productPriceMap = new HashMap<String, String>();
    Map<String, String> productAttributeMap = new HashMap<String, String>();
    Map<String, String> productCategoryMemberMap = new HashMap<String, String>();
    Map<String, String> featureMap = new HashMap<String, String>();
    Map<String, String> featureIdMap = new HashMap<String, String>();
    Map<String, String> featureApplMap = new HashMap<String, String>();

    HSSFWorkbook workbook = null;
    HSSFSheet sheet = null;
    try {
        //
        workbook = new HSSFWorkbook(new FileInputStream(new File(filePathAndName)));
        sheet = workbook.getSheetAt(0);
        int rowEnd = sheet.getLastRowNum();

        //?
        int i = 1;
        for (; i < rowEnd + 1; i++) {
            System.out.println("?" + i + "");
            HSSFRow row = sheet.getRow(i);

            String nowTime = UtilDateTime.nowTimestamp().toString();
            if (UtilValidate.isEmpty(convertCell(row.getCell(2)))) {
                continue;
            }
            //?
            String productCategoryId1 = convertCell(row.getCell(1));
            String productId = convertCell(row.getCell(2));
            String productName = convertCell(row.getCell(3));
            String quantityUom = convertCell(row.getCell(4));
            String defaultPrice = convertCell(row.getCell(5));
            String listPrice = convertCell(row.getCell(6));
            String skuId = convertCell(row.getCell(8));

            String Product = temp_Product.replace("#11#", productId);

            Product = Product.replace("#21#", productName);
            Product = Product.replace("#31#", productCategoryId1);
            Product = Product.replace("#41#", skuId);
            Product = Product.replace("#51#", nowTime.substring(0, 19));
            Product = Product.replace("#61#", getOneYearsAgoTime().substring(0, 19));
            Product = Product.replace("#71#", nowTime.substring(0, 19));
            String quantityUomId = "";
            if (UtilValidate.isNotEmpty(quantityUom)) {
                quantityUom = quantityUom.trim();
                if (quantityUom.equals("")) {
                    quantityUomId = "WT_tao";
                } else if (quantityUom.equals("")) {
                    quantityUomId = "WT_pian";
                } else if (quantityUom.equals("?")) {
                    quantityUomId = "WT_tai";
                } else if (quantityUom.equals("")) {
                    quantityUomId = "WT_ge";
                } else if (quantityUom.equals("")) {
                    quantityUomId = "WT_yanmi";
                } else if (quantityUom.equals("")) {
                    quantityUomId = "WT_pingfangmi";
                } else if (quantityUom.equals("")) {
                    quantityUomId = "WT_pingfangmi";
                } else if (quantityUom.equals("?")) {
                    quantityUomId = "WT_kg";
                } else if (quantityUom.equals("")) {
                    quantityUomId = "WT_jian";
                } else if (quantityUom.equals("")) {
                    quantityUomId = "WT_tong";
                } else if (quantityUom.equals("")) {
                    quantityUomId = "WT_dui";
                } else if (quantityUom.equals("")) {
                    quantityUomId = "WT_fu";
                } else if (quantityUom.equals("")) {
                    quantityUomId = "WT_gen";
                } else if (quantityUom.equals("")) {
                    quantityUomId = "WT_he";
                } else if (quantityUom.equals("")) {
                    quantityUomId = "WT_zhi";
                } else if (quantityUom.equals("")) {
                    quantityUomId = "WT_ping";
                } else if (quantityUom.equals("?")) {
                    quantityUomId = "WT_ting";
                } else if (quantityUom.equals("")) {
                    quantityUomId = "WT_dai";
                } else if (quantityUom.equals("?")) {
                    quantityUomId = "WT_he";
                }
            }

            Product = Product.replace("#81#", quantityUomId);

            productMap.put(productId, Product);

            String ProductRole = temp_ProductRole.replace("#11#", productId);
            productRoleMap.put(productId, ProductRole);

            long inventoryId = invetoryIdx++;
            String InventoryItem = temp_InventoryItem.replace("#21#", productId);
            InventoryItem = InventoryItem.replace("#11#", String.valueOf(inventoryId));
            inventoryItemMap.put(productId, InventoryItem);

            String InventoryItemDetail = temp_InventoryItemDetail.replace("#11#", String.valueOf(inventoryId));
            inventoryItemDetailMap.put(productId, InventoryItemDetail);

            //??

            String ProductCategoryMember = temp_ProductCategoryMember.replace("#11#", productId);
            ProductCategoryMember = ProductCategoryMember.replace("#21#", productCategoryId1);
            productCategoryMemberMap.put(productId, ProductCategoryMember);

            //
            if (UtilValidate.isNotEmpty(convertCell(row.getCell(5)))) {
                String ProductPrice = temp_ProductPrice.replace("#11#", productId);
                ProductPrice = ProductPrice.replace("#21#", "DEFAULT_PRICE");
                ProductPrice = ProductPrice.replace("#31#", defaultPrice);
                ProductPrice = ProductPrice.replace("#41#", "PURCHASE");
                productPriceMap.put(productId + "default", ProductPrice);
            }

            //
            if (UtilValidate.isNotEmpty(convertCell(row.getCell(6)))) {
                String ProductPrice = temp_ProductPrice.replace("#11#", productId);
                ProductPrice = ProductPrice.replace("#21#", "LIST_PRICE");
                ProductPrice = ProductPrice.replace("#31#", listPrice);
                ProductPrice = ProductPrice.replace("#41#", "PURCHASE");
                productPriceMap.put(productId + "list", ProductPrice);
            }

            String productFeatureTypeId = "BRAND";
            String cellVal = convertCell(row.getCell(9));
            if (UtilValidate.isNotEmpty(cellVal)) {
                String featureId = featurePrefix + featureIdx++;
                //?
                if (featureMap.containsKey(cellVal)) {
                    String ProductFeatureAppl = temp_ProductFeatureAppl.replace("#11#", productId);
                    ProductFeatureAppl = ProductFeatureAppl.replace("#21#", featureIdMap.get(cellVal));
                    ProductFeatureAppl = ProductFeatureAppl.replace("#31#", now.toString());
                    featureApplMap.put(featureId, ProductFeatureAppl);
                } else {
                    //??
                    String ProductFeature = temp_ProductFeature.replace("#11#", featureId);
                    ProductFeature = ProductFeature.replace("#21#", productFeatureTypeId);//
                    ProductFeature = ProductFeature.replace("#31#", productCategoryId1);
                    ProductFeature = ProductFeature.replace("#41#", cellVal);
                    featureMap.put(cellVal, ProductFeature);
                    featureIdMap.put(cellVal, featureId);

                    String ProductFeatureAppl = temp_ProductFeatureAppl.replace("#11#", productId);
                    ProductFeatureAppl = ProductFeatureAppl.replace("#21#", featureId);
                    ProductFeatureAppl = ProductFeatureAppl.replace("#31#", now.toString().subSequence(0, 19));
                    featureApplMap.put(featureId, ProductFeatureAppl);
                }
            }

            //?? 
            for (int j = 10; j <= 45; j = j + 2) {
                String cellValInfo = convertCell(row.getCell(j));
                String cellValue = convertCell(row.getCell(j + 1));
                if (UtilValidate.isNotEmpty(cellValInfo) & UtilValidate.isNotEmpty(cellValue)) {
                    String ProductAttribute = temp_ProductAttribute.replace("#11#", productId);
                    ProductAttribute = ProductAttribute.replace("#21#", cellValInfo);
                    ProductAttribute = ProductAttribute.replace("#31#", cellValue);
                    productAttributeMap.put(productId + j, ProductAttribute);
                }
            }

        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    // xml
    String line = "\r\n";
    StringBuffer sb = new StringBuffer();
    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + line);
    sb.append("<entity-engine-xml>" + line);

    sb.append(StringUtils.join(productMap.values(), line));
    sb.append(line + line);
    sb.append(StringUtils.join(productRoleMap.values(), line));
    sb.append(line + line);
    sb.append(StringUtils.join(productPriceMap.values(), line));
    sb.append(line + line);
    sb.append(StringUtils.join(inventoryItemMap.values(), line));
    sb.append(line + line);
    sb.append(StringUtils.join(inventoryItemDetailMap.values(), line));
    sb.append(line + line);
    sb.append(StringUtils.join(productCategoryMemberMap.values(), line));
    sb.append(line + line);
    sb.append(StringUtils.join(featureMap.values(), line));
    sb.append(line + line);
    sb.append(StringUtils.join(featureApplMap.values(), line));
    sb.append(line + line);
    sb.append(StringUtils.join(productAttributeMap.values(), line));

    sb.append(line + "</entity-engine-xml>");

    //      System.out.println(sb.toString());

    try {
        String str = sb.toString();
        //       str = str.replace("\"", "");
        str = str.replace("'", "");
        String outPath = "C:/Users/Administrator/Desktop/productData.xml";
        PrintWriter pw = new PrintWriter(new FileWriter(outPath));
        pw.write(str.toString());
        pw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:gov.nih.nci.cadsr.sentinel.database.DBAlertOracle.java

/**
 * Build the SQL select to retrieve changes for an Administered Component.
 *
 * @param idseq_ The idseq of a speciifc record of interest.
 * @param type_ The AC type, one of _ACTYPE_...
 * @param dates_ The flag for how dates are compared, _DATECM, _DATECONLY, _DATEMONLY
 * @param start_ The start date for the query.
 * @param end_ The end date for the query.
 * @param creators_ The specific created_by if any.
 * @param modifiers_ The specific modified_by if any.
 * @param wstatus_ The specific Workflow Status if any.
 * @param rstatus_ The specific Registration Status if any.
 * @return The SQL select string.//  w w  w . j  a v  a  2 s. com
 */
private String selectBuild(String idseq_, int type_, int dates_, Timestamp start_, Timestamp end_,
        String creators_[], String modifiers_[], String wstatus_[], String rstatus_[]) {
    // For consistency of reporting and ease of maintenance the idseq parameter
    // is provided to ignore the date range and pull all information about a
    // specific record.
    if (idseq_ != null && idseq_.length() > 0) {
        dates_ = _DATECM;
        start_ = new Timestamp(0);
        end_ = start_;
        creators_ = null;
        modifiers_ = null;
        wstatus_ = null;
        rstatus_ = null;
    }

    // Due to the very conditional nature of this logic, the SQL SELECT is built
    // without the use of substitution arguments ('?').
    String prefix = _DBMAP3[type_]._key;

    // The 'de' type is the only one that doesn't use the same prefix for the public id
    // database column - ugh.
    String prefix2 = (type_ == _ACTYPE_DE) ? "cde" : prefix;

    // Build the basic select and from.
    String select = "select 'p', 1, '" + prefix + "', zz." + prefix + "_idseq as id, zz.version, zz." + prefix2
            + "_id, zz.long_name, zz.conte_idseq, "
            + "zz.date_modified, zz.date_created, zz.modified_by, zz.created_by, zz.change_note, "
            + "c.name, '', ach.changed_column, ach.old_value, ach.new_value, ach.change_datetimestamp, ach.changed_table, ach.changed_by "
            + "from sbrext.ac_change_history_ext ach, " + _DBMAP3[type_]._table + " zz, ";

    // If registration status is not used we only need to add the context
    // table.
    String reg_clause;
    if (rstatus_ == null || rstatus_.length == 0) {
        select = select + "sbr.contexts_view c ";
        reg_clause = "";
    }
    // If registration status is used we need to add the context and registration
    // status tables.
    else {
        select = select + "sbr.contexts_view c, sbr.ac_registrations_view ar ";
        reg_clause = "AND ar.ac_idseq = zz." + prefix + "_idseq AND NVL(ar.registration_status, '(none)') IN "
                + selectIN(rstatus_);
    }

    // If workflow status is not used we need to be sure and use an empty
    // string.
    String wfs_clause;
    if (wstatus_ == null || wstatus_.length == 0) {
        wfs_clause = "";
    }
    // If workflow status is used we need to qualify by the content of the list.
    else {
        wfs_clause = "AND zz.asl_name IN " + selectIN(wstatus_);
    }

    // Building the 'where' clause should be done to keep all qualifications together, e.g.
    // first qualify all for ACH then join to the primary table (ZZ) complete the qualifications
    // then join to the context table.

    // Build the start and end dates.
    String start = "to_date('" + start_.toString().substring(0, 10) + "', 'yyyy/mm/dd')";
    String end = "to_date('" + end_.toString().substring(0, 10) + "', 'yyyy/mm/dd')";

    // Always checking the date range first.
    if (idseq_ == null || idseq_.length() == 0)
        select = select + "where ach.change_datetimestamp >= " + start + " and ach.change_datetimestamp < "
                + end + " ";
    else
        select = select + "where ach.ac_idseq = '" + idseq_ + "' ";

    // If modifiers are provided be sure to get everything.
    if (modifiers_ != null && modifiers_.length > 0 && modifiers_[0].charAt(0) != '(')
        select = select + "AND ach.changed_by in " + selectIN(modifiers_);

    // Now qualify by the record type of interest and join to the primary table.
    select = select + whereACH(type_) + "AND zz." + prefix + "_idseq = ach.ac_idseq ";

    // If creators are provided they must be qualified by the primary table not the change table.
    if (creators_ != null && creators_.length > 0 && creators_[0].charAt(0) != '(')
        select = select + "AND zz.created_by in " + selectIN(creators_);

    // When looking for both create and modified dates no extra clause is needed. For create
    // date only qualify against the primary table.
    if (dates_ == _DATECONLY)
        select = select + "AND zz.date_created >= " + start + " and zz.date_created < " + end + " ";

    // For modify date only qualify the primary table. The actual date is not important because we
    // qualified the records from the history table by date already.
    else if (dates_ == _DATEMONLY)
        select = select + "AND zz.date_modified is not NULL ";

    // Put everything together including the join to the context table and the sort order clause.
    return select + wfs_clause + reg_clause + "AND c.conte_idseq = zz.conte_idseq " + _orderbyACH;
}

From source file:com.krawler.esp.portalmsg.Mail.java

public static String insertMailMsg(Connection conn, javax.servlet.http.HttpServletRequest request,
        String companyid) throws ServiceException, ParseException, JSONException {
    org.apache.commons.fileupload.DiskFileUpload fu = new org.apache.commons.fileupload.DiskFileUpload();
    java.util.List fileItems = null;
    org.apache.commons.fileupload.FileItem fi = null;
    int sizeinmb = forummsgcomm.getmaxfilesize(conn, companyid);
    long maxsize = sizeinmb * 1024 * 1024;
    fu.setSizeMax(maxsize);/*from www  . ja  va  2  s .  c  o m*/
    boolean fileupload = false;
    java.util.HashMap arrParam = new java.util.HashMap();
    JSONObject jobj = new JSONObject();
    try {
        fileItems = fu.parseRequest(request);
    } catch (org.apache.commons.fileupload.FileUploadException e) {
        com.krawler.utils.json.base.JSONObject jerrtemp = new com.krawler.utils.json.base.JSONObject();
        if (e.getClass().getSimpleName().equalsIgnoreCase("SizeLimitExceededException")) {
            jerrtemp.put("msg", "For attachments, maximum file size allowed is " + sizeinmb + "MB");
        } else {
            jerrtemp.put("msg", "Problem while uploading file.");
        }
        jerrtemp.put("Success", "Fail");
        jobj.append("data", jerrtemp);
        return jobj.toString();
    }
    for (java.util.Iterator k = fileItems.iterator(); k.hasNext();) {
        fi = (org.apache.commons.fileupload.FileItem) k.next();
        arrParam.put(fi.getFieldName(), fi.getString());
        if (!fi.isFormField()) {
            if (fi.getSize() > maxsize) {
                com.krawler.utils.json.base.JSONObject jerrtemp = new com.krawler.utils.json.base.JSONObject();
                jerrtemp.put("Success", "Fail");
                jerrtemp.put("msg", "Attachment size should be upto " + sizeinmb + "MB");
                jobj.append("data", jerrtemp);
                return jobj.toString();
            }
            fileupload = true;
        }
    }
    String poster_id = request.getParameter("userId");
    String post_subject = StringUtil
            .serverHTMLStripper(java.net.URLDecoder.decode(arrParam.get("title").toString()));
    String post_text = java.net.URLDecoder.decode(arrParam.get("ptxt").toString());
    String folder = "1";
    Boolean readflag = false;
    String last_folder_id = "1";
    String reply_to = "";
    String sendflag = StringUtil.serverHTMLStripper(request.getParameter("sendflag"));
    String post_id1 = "";
    String to_id = "";
    String msgFor = "";
    String fid = "";
    String sendefolderpostid = "";
    Boolean done = false;
    String[] tos = {};
    if (sendflag.compareTo("reply") == 0) {
        post_id1 = StringUtil.serverHTMLStripper(request.getParameter("repto"));
        msgFor = getUserFromPost(conn, post_id1);
        fid = StringUtil.serverHTMLStripper(request.getParameter("fid"));
        if (fid.compareTo("3") != 0) {
            to_id = msgFor;
        }
    } else if (sendflag.compareTo("newmsg") == 0) {
        tos = request.getParameter("repto").split(";");
        fid = StringUtil.serverHTMLStripper(request.getParameter("fid"));
        msgFor = to_id;
    }
    int draft = Integer.parseInt(request.getParameter("draft"));

    String query;
    String post_id = "";
    String temp = "";
    //                JSONObject jobj = new JSONObject();
    String usr = to_id;
    DbResults rs1 = null;
    DbResults rs2 = null;
    int numRows = 0;
    boolean uploaded = false;
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    java.util.Date d = new java.util.Date();
    java.sql.Timestamp sqlPostDate = new java.sql.Timestamp(d.getTime());
    UserDAO userDao = new UserDAOImpl();
    if (sendflag.compareTo("reply") == 0) {
        if (draft == 1) {
            post_id = UUID.randomUUID().toString();
            folder = getDraftId(poster_id);//"3";
            last_folder_id = "0";
            readflag = false;
            reply_to = "";
            if (fid.compareTo("3") != 0) {
                if (StringUtil.isNullOrEmpty(to_id) || StringUtil.isNullOrEmpty(poster_id)) {
                    return KWLErrorMsgs.exSuccessFail;
                }
                query = "Insert into mailmessages (post_id, to_id, poster_id, post_subject, post_text, post_time, flag, folder, reply_to, last_folder_id, readflag) values ( ?, ?, ?, ?, ?,?,false, ?, ?, ?, ?)";
                post_id = UUID.randomUUID().toString();
                numRows = DbUtil.executeUpdate(conn, query, new Object[] { post_id, to_id, poster_id,
                        post_subject, post_text, sqlPostDate, folder, reply_to, last_folder_id, readflag });

            } else {
                if (StringUtil.isNullOrEmpty(post_id1)) {
                    return KWLErrorMsgs.exSuccessFail;
                }
                query = "Update mailmessages set post_subject=?, post_text=? where post_id=?";

                numRows = DbUtil.executeUpdate(conn, query, new Object[] { post_subject, post_text, post_id1 });
            }
        } else if (draft == 2) {
            if (StringUtil.isNullOrEmpty(post_id1)
                    || /*StringUtil.isNullOrEmpty(subdomain) || StringUtil.isNullOrEmpty(post_id) ||*/ StringUtil
                            .isNullOrEmpty(to_id)
                    || StringUtil.isNullOrEmpty(poster_id) /*|| StringUtil.isNullOrEmpty(reply_to)*/) {
                return KWLErrorMsgs.exSuccessFail;
            }

            //                query = "SELECT users.userid FROM users inner join userlogin on users.userid=userlogin.userid where username=? and users.companyid = ?;";
            //                rs2 = DbUtil.executeQuery(conn, query, new Object[]{post_id1, companyid});
            //
            //                if (rs2.next()) {
            to_id = msgFor;
            //                }
            query = "Insert into mailmessages (post_id, to_id, poster_id, post_subject, post_text, post_time, flag, folder, reply_to, last_folder_id, readflag) values ( ?, ?, ?, ?, ?, ?,false, ?, ?, ?, ?)";
            post_id = UUID.randomUUID().toString();
            numRows = DbUtil.executeUpdate(conn, query, new Object[] { post_id, to_id, poster_id, post_subject,
                    post_text, sqlPostDate, folder, reply_to, last_folder_id, readflag });

            if (fileupload) {
                forummsgcomm.doPost(conn, fileItems, post_id, sqlPostDate, poster_id, post_id);
                uploaded = true;
            }
            post_id = UUID.randomUUID().toString();
            folder = "0";
            last_folder_id = "0";
            readflag = false;
            reply_to = "";
            query = "Insert into mailmessages (post_id, to_id, poster_id, post_subject, post_text, post_time, flag, folder, reply_to, last_folder_id, readflag) values ( ?, ?, ?, ?, ?, ?,false, ?, ?, ?, ?)";

            numRows = DbUtil.executeUpdate(conn, query, new Object[] { post_id, to_id, poster_id, post_subject,
                    post_text, sqlPostDate, folder, reply_to, last_folder_id, readflag });
            if (fileupload) {
                forummsgcomm.doPost(conn, fileItems, post_id, sqlPostDate, poster_id, post_id);
            }

        } else {
            query = "SELECT poster_id FROM mailmessages where post_id=?";
            DbResults rs = DbUtil.executeQuery(conn, query, post_id1);
            while (rs.next()) {
                to_id = rs.getString(1);
            }
        }
    } else {
        if (draft != 1) {
            if (draft == 3) {
                for (int t = 0; t < tos.length; t++) {
                    to_id = StringUtil.serverHTMLStripper(tos[t]);
                    if (to_id.contains("@")) {
                        query = "SELECT userid FROM users where emailid=?;";
                        rs2 = DbUtil.executeQuery(conn, query, to_id);

                        if (rs2.next()) {
                            to_id = (rs2.getString(1));
                        } else {
                            usr = to_id;
                            to_id = "-1"; //for invalid username
                        }
                    } else {
                        query = "SELECT users.userid FROM users inner join userlogin on users.userid = userlogin.userid where userlogin.username=? and users.companyid = ?;";
                        rs2 = DbUtil.executeQuery(conn, query, new Object[] { to_id, companyid });

                        if (rs2.next()) {
                            to_id = (rs2.getString(1));
                        } else {
                            usr = AuthHandler.getUserName(conn, to_id);
                            if (StringUtil.isNullOrEmpty(usr)) {
                                usr = to_id;
                                to_id = "-1"; //for invalid username
                            }
                        }
                    }
                    if (to_id.compareTo("-1") != 0) {
                        if (StringUtil.isNullOrEmpty(to_id) || StringUtil.isNullOrEmpty(poster_id)) {
                            return KWLErrorMsgs.exSuccessFail;
                        }
                        if (fid.compareTo("3") != 0 && t == tos.length - 1) {
                            folder = "1";
                            query = "Insert into mailmessages (post_id, to_id, poster_id, post_subject, post_text, post_time, flag, folder, reply_to, last_folder_id, readflag) values ( ?, ?, ?, ?, ?, ?,false, ?, ?, ?, ?)";
                            post_id = UUID.randomUUID().toString();
                            numRows = DbUtil.executeUpdate(conn, query,
                                    new Object[] { post_id, to_id, poster_id, post_subject, post_text,
                                            sqlPostDate, folder, reply_to, last_folder_id, readflag });
                        }
                        folder = "0";
                        query = "Insert into mailmessages (post_id, to_id, poster_id, post_subject, post_text, post_time, flag, folder, reply_to, last_folder_id, readflag) values ( ?, ?, ?, ?, ?, ?,false, ?, ?, ?, ?)";
                        post_id = UUID.randomUUID().toString();
                        sendefolderpostid = post_id;
                        numRows = DbUtil.executeUpdate(conn, query,
                                new Object[] { post_id, to_id, poster_id, post_subject, post_text, sqlPostDate,
                                        folder, reply_to, last_folder_id, readflag });

                        //post_id = UUID.randomUUID().toString();
                        if (fileupload) {
                            forummsgcomm.doPost(conn, fileItems, post_id, sqlPostDate, poster_id, post_id);
                            uploaded = true;
                        }
                        last_folder_id = "0";
                        readflag = false;
                        reply_to = "";
                        done = true;
                    }
                    if (fid.compareTo("3") == 0 && done == true) {//move to this users sent mails
                        post_id = request.getParameter("postid");
                        String tempfolder = "1";
                        DbResults rstemp = null;
                        query = "select poster_id from mailmessages where post_id=?";
                        rstemp = DbUtil.executeQuery(conn, query, new Object[] { post_id });
                        if (rstemp.next()) {

                            query = "update mailmessages set to_id=?, poster_id=?, post_subject=?, post_text=?, post_time=?, flag=false, folder=?, reply_to=?, last_folder_id=?, readflag=? where post_id=?";

                            int tempnumrows = DbUtil.executeUpdate(conn, query,
                                    new Object[] { to_id, poster_id, post_subject, post_text, sqlPostDate,
                                            tempfolder, reply_to, last_folder_id, readflag, post_id });
                            if (tempnumrows == 0) {
                                if (to_id.compareTo("-1") == 0) {
                                    com.krawler.utils.json.base.JSONObject jtemp = new com.krawler.utils.json.base.JSONObject();
                                    jtemp.put("Success", "userfail");
                                    jtemp.put("Subject", usr);
                                    jobj.append("data", jtemp);
                                    return jobj.toString();
                                } else {
                                    return KWLErrorMsgs.exSuccessFail;
                                }
                            }
                        }
                    }
                }
            }
        } else {
            for (int t = 0; t < tos.length; t++) {
                to_id = StringUtil.serverHTMLStripper(tos[t]);
                post_id = request.getParameter("postid");
                folder = getDraftId(poster_id);//"3";
                last_folder_id = getDraftId(poster_id);

                readflag = false;
                reply_to = "";
                query = "SELECT users.userid FROM users inner join userlogin on users.userid = userlogin.userid where userlogin.username=? and users.companyid = ?;";
                rs2 = DbUtil.executeQuery(conn, query, new Object[] { to_id, companyid });

                if (rs2.next()) {
                    to_id = (rs2.getString(1));
                } else {
                    to_id = poster_id;
                }
                if (done == false) {
                    if (to_id.compareTo("-1") != 0) {
                        if (StringUtil.isNullOrEmpty(to_id) || StringUtil.isNullOrEmpty(poster_id)) {
                            return KWLErrorMsgs.exSuccessFail;
                        }
                        DbResults rstemp = null;
                        query = "select poster_id from mailmessages where post_id=?";
                        rstemp = DbUtil.executeQuery(conn, query, new Object[] { post_id });
                        if (rstemp.next()) {

                            query = "update mailmessages set to_id=?, poster_id=?, post_subject=?, post_text=?, post_time=?, flag=false, folder=?, reply_to=?, last_folder_id=?, readflag=? where post_id=?";

                            numRows = DbUtil.executeUpdate(conn, query,
                                    new Object[] { to_id, poster_id, post_subject, post_text, sqlPostDate,
                                            folder, reply_to, last_folder_id, readflag, post_id });

                        } else {

                            String tpost_id = UUID.randomUUID().toString();
                            folder = getDraftId(poster_id);//"3";
                            last_folder_id = "0";
                            readflag = false;
                            reply_to = "";
                            if (fid.compareTo("3") != 0) {
                                if (StringUtil.isNullOrEmpty(to_id) || StringUtil.isNullOrEmpty(poster_id)) {
                                    return KWLErrorMsgs.exSuccessFail;
                                }
                                query = "Insert into mailmessages (post_id, to_id, poster_id, post_subject, post_text, post_time, flag, folder, reply_to, last_folder_id, readflag) values ( ?, ?, ?, ?, ?,?,false, ?, ?, ?, ?)";
                                numRows = DbUtil.executeUpdate(conn, query,
                                        new Object[] { tpost_id, to_id, poster_id, post_subject, post_text,
                                                sqlPostDate, folder, reply_to, last_folder_id, readflag });
                            }
                        }
                    }
                }
            }
        }
    }
    if (fileupload && !uploaded) {
        forummsgcomm.doPost(conn, fileItems, post_id, sqlPostDate, poster_id, sendefolderpostid);
    }
    if (numRows == 0) {
        if (to_id.compareTo("-1") == 0) {
            com.krawler.utils.json.base.JSONObject jtemp = new com.krawler.utils.json.base.JSONObject();
            jtemp.put("Success", "userfail");
            jtemp.put("Subject", usr);
            jobj.append("data", jtemp);
            return jobj.toString();
        } else {
            return KWLErrorMsgs.exSuccessFail;
        }
    } else {
        String dateTime = "";
        String UserName = "";
        String Image = "";
        query = "SELECT userlogin.username,image FROM users inner join userlogin on users.userid=userlogin.userid where users.userid=?;";
        rs2 = DbUtil.executeQuery(conn, query, poster_id);

        if (rs2.next()) {
            UserName = (rs2.getString(1));
            Image = (rs2.getString(2));
        }
        if (draft == 1 && sendflag.compareTo("reply") == 0) {
            query = "SELECT post_time FROM mailmessages where post_id=?;";
            rs1 = DbUtil.executeQuery(conn, query, post_id1);

            if (rs1.next()) {
                dateTime = (rs1.getObject(1).toString());
            }
        } else {
            query = "SELECT post_time FROM mailmessages where post_id=?;";
            rs1 = DbUtil.executeQuery(conn, query, post_id);

            if (rs1.next()) {
                dateTime = (rs1.getObject(1).toString());
            }
        }
        String userTime = Timezone.toCompanyTimezone(conn, sqlPostDate.toString(), companyid);
        java.util.Date tempdate = sdf.parse(userTime);
        java.text.SimpleDateFormat sdf1 = new java.text.SimpleDateFormat("yyyy-MM-dd h:mm a");
        JSONStringer j = new JSONStringer();
        com.krawler.utils.json.base.JSONObject jtemp = new com.krawler.utils.json.base.JSONObject();
        String success = "Success";
        if (folder.compareTo("3") == 0) {
            success = "Draft";
        }
        jtemp.put("Success", success);
        jtemp.put("post_time", sdf1.format(tempdate).toString());
        jtemp.put("flag", "false");
        jtemp.put("post_id", post_id);
        jtemp.put("post_subject", post_subject);
        jtemp.put("post_text", "");
        jtemp.put("poster_id", UserName);
        jtemp.put("readflag", "0");
        jtemp.put("imgsrc", Image);
        jtemp.put("senderid", poster_id);
        jobj.append("data", jtemp);
        /*temp = j.object().key("Success").value("Success").key("post_time")
        .value(sdf1.format(tempdate).toString()).key("flag").value(
        "false").key("post_id").value(post_id).key(
        "post_subject").value(post_subject)
        .key("post_text").value("").key("poster_id")
        .value(UserName).key("readflag").value("0").key("imgsrc")
        .value(Image).key("senderid").value(poster_id).endObject()
        .toString();*/

    }
    return jobj.toString();
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public boolean hasAuth(String who, int privIndex) throws NoSuchObjectException, MetaException {
    Connection con = null;//from   w  ww  .j a v a2  s  .  c  om
    ;
    Statement ps = null;
    boolean success = false;
    who = who.toLowerCase();

    Privilege priv = privMap.get(privIndex);

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("audit error, user=" + user + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("audit error, user=" + user + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    long nowtime = (new Date()).getTime();
    Map<String, String> outofdataInfoMap = new HashMap<String, String>();

    try {
        ps = con.createStatement();

        String sql = "select alter_priv, create_priv, createview_priv, dba_priv, "
                + "delete_priv, drop_priv, index_priv, insert_priv, select_priv, showview_priv, "
                + "update_priv, user_name, group_name, out_of_date_time from tdwuser where tdwuser.user_name='"
                + who.toLowerCase() + "'";

        ResultSet userSet = ps.executeQuery(sql);
        boolean isUserFind = false;

        Timestamp ts = null;
        long outofdateTime = 0;
        boolean isOutofdate = false;

        while (userSet.next()) {
            isUserFind = true;

            ts = userSet.getTimestamp(14);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }

            boolean isDBA = userSet.getBoolean(4);
            if (isDBA) {
                return true;
            }

            switch (priv) {
            case SELECT_PRIV:
                if (userSet.getBoolean(9)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " SELECT_PRIV", ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (userSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " ALTER_PRIV", ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (userSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " INSERT_PRIV", ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (userSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " INDEX_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (userSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " CREATE_PRIV", ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (userSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " DROP_PRIV", ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (userSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " DELETE_PRIV", ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (userSet.getBoolean(11)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " UPDATE_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_VIEW_PRIV:
                if (userSet.getBoolean(3)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " CREATE_VIEW_PRIV", ts.toString());
                    }
                }
                break;

            case SHOW_VIEW_PRIV:
                if (userSet.getBoolean(10)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " SHOW_VIEW_PRIV", ts.toString());
                    }
                }
                break;

            case DBA_PRIV:
                if (userSet.getBoolean(4)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " DBA_PRIV", ts.toString());
                    }
                }
                break;

            }
            break;
        }

        userSet.close();

        if (!isUserFind) {
            throw new NoSuchObjectException("can not find user:" + who);
        }

        sql = "select tdwrole.alter_priv, tdwrole.create_priv, tdwrole.createview_priv, tdwrole.dba_priv, "
                + "tdwrole.delete_priv, tdwrole.drop_priv, tdwrole.index_priv, tdwrole.insert_priv, tdwrole.select_priv, tdwrole.showview_priv, "
                + "tdwrole.update_priv, tdwrole.role_name, tdwrole.out_of_date_time from tdwrole, tdwuserrole where tdwuserrole.user_name='"
                + who.toLowerCase() + "'" + " and tdwrole.role_name=tdwuserrole.role_name";

        ResultSet roleSet = ps.executeQuery(sql);
        String roleName = null;
        while (roleSet.next()) {
            roleName = roleSet.getString(12);
            ts = roleSet.getTimestamp(13);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }

            boolean isDBA = roleSet.getBoolean(4);
            if (isDBA) {
                return true;
            }
            switch (priv) {
            case SELECT_PRIV:
                if (roleSet.getBoolean(9)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " SELECT_PRIV", ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (roleSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " ALTER_PRIV", ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (roleSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " INSERT_PRIV", ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (roleSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " INDEX_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (roleSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " CREATE_PRIV", ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (roleSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " DROP_PRIV", ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (roleSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " DELETE_PRIV", ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (roleSet.getBoolean(11)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " UPDATE_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_VIEW_PRIV:
                if (roleSet.getBoolean(3)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " CREATE_VIEW_PRIV", ts.toString());
                    }
                }
                break;

            case SHOW_VIEW_PRIV:
                if (roleSet.getBoolean(10)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " SHOW_VIEW_PRIV", ts.toString());
                    }
                }
                break;

            case DBA_PRIV:
                if (roleSet.getBoolean(4)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " DBA_PRIV", ts.toString());
                    }
                }
                break;

            }
        }

        roleSet.close();

        success = true;
    } catch (SQLException sqlex) {
        LOG.error("get user error, user=" + user + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        closeStatement(ps);
        closeConnection(con);
    }

    if (!outofdataInfoMap.isEmpty()) {
        StringBuilder sb = new StringBuilder();
        sb.append("priv out of date, you should renewal you privlage; the detail information is ");
        sb.append("\n");
        for (Entry<String, String> e : outofdataInfoMap.entrySet()) {
            sb.append("priv:" + e.getKey() + ",");
            sb.append("out of date:" + e.getValue());
            sb.append("\n");
        }
        throw new MetaException(sb.toString());
    }

    return false;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public boolean hasAuthWithRole(String who, String role, int privIndex)
        throws NoSuchObjectException, MetaException {
    Connection con = null;/*from   ww  w .j av a2s . c  o m*/
    ;
    Statement ps = null;
    boolean success = false;
    who = who.toLowerCase();
    role = role.toLowerCase();

    Privilege priv = privMap.get(privIndex);

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("audit error, user=" + user + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("audit error, user=" + user + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    long nowtime = (new Date()).getTime();
    Map<String, String> outofdataInfoMap = new HashMap<String, String>();

    try {
        ps = con.createStatement();

        Timestamp ts = null;
        long outofdateTime = 0;
        boolean isOutofdate = false;

        String sql = "select tdwrole.alter_priv, tdwrole.create_priv, tdwrole.createview_priv, tdwrole.dba_priv, "
                + "tdwrole.delete_priv, tdwrole.drop_priv, tdwrole.index_priv, tdwrole.insert_priv, tdwrole.select_priv, tdwrole.showview_priv, "
                + "tdwrole.update_priv, tdwrole.role_name, tdwrole.out_of_date_time from tdwrole, tdwuserrole where tdwuserrole.user_name='"
                + who.toLowerCase() + "' and tdwrole.role_name='" + role + "' "
                + " and tdwrole.role_name=tdwuserrole.role_name";

        ResultSet roleSet = ps.executeQuery(sql);
        String roleName = null;
        boolean isRoleFind = false;
        while (roleSet.next()) {
            isRoleFind = true;
            roleName = roleSet.getString(12);
            ts = roleSet.getTimestamp(13);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }

            boolean isDBA = roleSet.getBoolean(4);
            if (isDBA) {
                return true;
            }
            switch (priv) {
            case SELECT_PRIV:
                if (roleSet.getBoolean(9)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " SELECT_PRIV", ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (roleSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " ALTER_PRIV", ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (roleSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " INSERT_PRIV", ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (roleSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " INDEX_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (roleSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " CREATE_PRIV", ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (roleSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " DROP_PRIV", ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (roleSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " DELETE_PRIV", ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (roleSet.getBoolean(11)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " UPDATE_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_VIEW_PRIV:
                if (roleSet.getBoolean(3)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " CREATE_VIEW_PRIV", ts.toString());
                    }
                }
                break;

            case SHOW_VIEW_PRIV:
                if (roleSet.getBoolean(10)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " SHOW_VIEW_PRIV", ts.toString());
                    }
                }
                break;

            case DBA_PRIV:
                if (roleSet.getBoolean(4)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " DBA_PRIV", ts.toString());
                    }
                }
                break;

            }
            break;
        }

        roleSet.close();

        if (!isRoleFind) {
            LOG.error("user/role does not exist or user does not play this role:" + who + "/" + role);
            throw new MetaException("user/role does not exist or user does not play this role:" + who + "/"
                    + role + ", you shoule check it");
        }

        sql = "select alter_priv, create_priv, createview_priv, dba_priv, "
                + "delete_priv, drop_priv, index_priv, insert_priv, select_priv, showview_priv, "
                + "update_priv, user_name, group_name, out_of_date_time from tdwuser where tdwuser.user_name='"
                + who.toLowerCase() + "'";

        ResultSet userSet = ps.executeQuery(sql);
        boolean isUserFind = false;

        while (userSet.next()) {
            isUserFind = true;

            ts = userSet.getTimestamp(14);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }

            boolean isDBA = userSet.getBoolean(4);
            if (isDBA) {
                return true;
            }

            switch (priv) {
            case SELECT_PRIV:
                if (userSet.getBoolean(9)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " SELECT_PRIV", ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (userSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " ALTER_PRIV", ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (userSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " INSERT_PRIV", ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (userSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " INDEX_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (userSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " CREATE_PRIV", ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (userSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " DROP_PRIV", ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (userSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " DELETE_PRIV", ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (userSet.getBoolean(11)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " UPDATE_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_VIEW_PRIV:
                if (userSet.getBoolean(3)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " CREATE_VIEW_PRIV", ts.toString());
                    }
                }
                break;

            case SHOW_VIEW_PRIV:
                if (userSet.getBoolean(10)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " SHOW_VIEW_PRIV", ts.toString());
                    }
                }
                break;

            case DBA_PRIV:
                if (userSet.getBoolean(4)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " DBA_PRIV", ts.toString());
                    }
                }
                break;

            }
            break;
        }

        userSet.close();

        if (!isUserFind) {
            throw new NoSuchObjectException("can not find user:" + who);
        }

        success = true;
    } catch (SQLException sqlex) {
        LOG.error("get user error, user=" + user + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        closeStatement(ps);
        closeConnection(con);
    }

    if (!outofdataInfoMap.isEmpty()) {
        StringBuilder sb = new StringBuilder();
        sb.append("priv out of date, you should renewal you privlage; the detail information is ");
        sb.append("\n");
        for (Entry<String, String> e : outofdataInfoMap.entrySet()) {
            sb.append("priv:" + e.getKey() + ",");
            sb.append("out of date:" + e.getValue());
            sb.append("\n");
        }
        throw new MetaException(sb.toString());
    }

    return false;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public boolean hasAuthOnDb(String who, String db, int privIndex) throws NoSuchObjectException, MetaException {
    Connection con = null;/*w w  w. j av a  2 s.  co  m*/
    ;
    Statement ps = null;
    Privilege priv = privMap.get(privIndex);
    who = who.toLowerCase();
    db = db.toLowerCase();

    long nowtime = (new Date()).getTime();
    Map<String, String> outofdataInfoMap = new HashMap<String, String>();
    Timestamp ts = null;
    long outofdateTime = 0;
    boolean isOutofdate = false;

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("audit error, user=" + user + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("audit error, user=" + user + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        ps = con.createStatement();

        String sql = "select alter_priv, create_priv, createview_priv, dba_priv, "
                + "delete_priv, drop_priv, index_priv, insert_priv, select_priv, showview_priv, "
                + "update_priv, user_name, group_name, out_of_date_time from tdwuser where tdwuser.user_name='"
                + who.toLowerCase() + "'";

        ResultSet userSet = ps.executeQuery(sql);
        boolean isUserFind = false;

        while (userSet.next()) {
            isUserFind = true;

            ts = userSet.getTimestamp(14);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }

            boolean isDBA = userSet.getBoolean(4);
            if (isDBA) {
                return true;
            }

            switch (priv) {
            case SELECT_PRIV:
                if (userSet.getBoolean(9)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " SELECT_PRIV", ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (userSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " ALTER_PRIV", ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (userSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " INSERT_PRIV", ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (userSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " INDEX_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (userSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " CREATE_PRIV", ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (userSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " DROP_PRIV", ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (userSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " DELETE_PRIV", ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (userSet.getBoolean(11)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " UPDATE_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_VIEW_PRIV:
                if (userSet.getBoolean(3)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " CREATE_VIEW_PRIV", ts.toString());
                    }
                }
                break;

            case SHOW_VIEW_PRIV:
                if (userSet.getBoolean(10)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " SHOW_VIEW_PRIV", ts.toString());
                    }
                }
                break;

            }
        }

        userSet.close();

        if (!isUserFind) {
            throw new NoSuchObjectException("can not find user:" + who);
        }

        sql = "select alter_priv, create_priv, createview_priv, "
                + "delete_priv, drop_priv, index_priv, insert_priv, select_priv, "
                + "showview_priv, update_priv, out_of_date_time from dbpriv where user_name='"
                + who.toLowerCase() + "' and db_name='" + db.toLowerCase() + "'";

        ResultSet dbPrivSet = ps.executeQuery(sql);

        while (dbPrivSet.next()) {
            ts = dbPrivSet.getTimestamp(11);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }

            switch (priv) {
            case SELECT_PRIV:
                if (dbPrivSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " SELECT_PRIV", ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (dbPrivSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " ALTER_PRIV", ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (dbPrivSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " INSERT_PRIV", ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (dbPrivSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " INDEX_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (dbPrivSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " CREATE_PRIV", ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (dbPrivSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " DROP_PRIV", ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (dbPrivSet.getBoolean(4)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " DELETE_PRIV", ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (dbPrivSet.getBoolean(10)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " UPDATE_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_VIEW_PRIV:
                if (dbPrivSet.getBoolean(3)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " CREATE_VIEW_PRIV",
                                ts.toString());
                    }
                }
                break;

            case SHOW_VIEW_PRIV:
                if (dbPrivSet.getBoolean(9)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " SHOW_VIEW_PRIV", ts.toString());
                    }
                }
                break;

            }
        }

        sql = "select tdwrole.alter_priv, tdwrole.create_priv, tdwrole.createview_priv, tdwrole.dba_priv, "
                + "tdwrole.delete_priv, tdwrole.drop_priv, tdwrole.index_priv, tdwrole.insert_priv, tdwrole.select_priv, tdwrole.showview_priv, "
                + "tdwrole.update_priv, tdwrole.role_name, tdwrole.out_of_date_time from tdwrole, tdwuserrole where tdwuserrole.user_name='"
                + who.toLowerCase() + "'" + " and tdwrole.role_name=tdwuserrole.role_name";

        ResultSet roleSet = ps.executeQuery(sql);

        String roleName = null;
        List<String> roleList = new ArrayList<String>();
        while (roleSet.next()) {
            roleName = roleSet.getString(12);
            ts = roleSet.getTimestamp(13);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }

            boolean isDBA = roleSet.getBoolean(4);
            if (isDBA) {
                return true;
            }

            switch (priv) {
            case SELECT_PRIV:
                if (roleSet.getBoolean(9)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " SELECT_PRIV", ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (roleSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " ALTER_PRIV", ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (roleSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " INSERT_PRIV", ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (roleSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " INDEX_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (roleSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " CREATE_PRIV", ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (roleSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " DROP_PRIV", ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (roleSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " DELETE_PRIV", ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (roleSet.getBoolean(11)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " UPDATE_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_VIEW_PRIV:
                if (roleSet.getBoolean(3)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " CREATE_VIEW_PRIV", ts.toString());
                    }
                }
                break;

            case SHOW_VIEW_PRIV:
                if (roleSet.getBoolean(10)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " SHOW_VIEW_PRIV", ts.toString());
                    }
                }
                break;

            case DBA_PRIV:
                if (roleSet.getBoolean(4)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + roleName + " DBA_PRIV", ts.toString());
                    }
                }
                break;

            }

            roleList.add(roleSet.getString(12));
        }

        roleSet.close();

        for (String r : roleList) {
            sql = "select alter_priv, create_priv, createview_priv, "
                    + "delete_priv, drop_priv, index_priv, insert_priv, select_priv, "
                    + "showview_priv, update_priv, out_of_date_time from dbpriv where user_name='"
                    + r.toLowerCase() + "' and db_name='" + db.toLowerCase() + "'";

            dbPrivSet = ps.executeQuery(sql);

            while (dbPrivSet.next()) {
                ts = dbPrivSet.getTimestamp(11);

                if (ts != null) {
                    outofdateTime = ts.getTime();
                } else {
                    outofdateTime = Long.MAX_VALUE;
                }

                if (nowtime > outofdateTime) {
                    isOutofdate = true;
                } else {
                    isOutofdate = false;
                }

                switch (priv) {
                case SELECT_PRIV:
                    if (dbPrivSet.getBoolean(8)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put("role " + r + " on db:" + db + " SELECT_PRIV", ts.toString());
                        }
                    }
                    break;

                case ALTER_PRIV:
                    if (dbPrivSet.getBoolean(1)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put("role " + r + " on db:" + db + " ALTER_PRIV", ts.toString());
                        }
                    }
                    break;

                case INSERT_PRIV:
                    if (dbPrivSet.getBoolean(7)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put("role " + r + " on db:" + db + " INSERT_PRIV", ts.toString());
                        }
                    }
                    break;

                case INDEX_PRIV:
                    if (dbPrivSet.getBoolean(6)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put("role " + r + " on db:" + db + " INDEX_PRIV", ts.toString());
                        }
                    }
                    break;

                case CREATE_PRIV:
                    if (dbPrivSet.getBoolean(2)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put("role " + r + " on db:" + db + " CREATE_PRIV", ts.toString());
                        }
                    }
                    break;

                case DROP_PRIV:
                    if (dbPrivSet.getBoolean(5)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put("role " + r + " on db:" + db + " DROP_PRIV", ts.toString());
                        }
                    }
                    break;

                case DELETE_PRIV:
                    if (dbPrivSet.getBoolean(4)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put("role " + r + " on db:" + db + " DELETE_PRIV", ts.toString());
                        }
                    }
                    break;

                case UPDATE_PRIV:
                    if (dbPrivSet.getBoolean(10)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put("role " + r + " on db:" + db + " UPDATE_PRIV", ts.toString());
                        }
                    }
                    break;

                case CREATE_VIEW_PRIV:
                    if (dbPrivSet.getBoolean(3)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put("role " + r + " on db:" + db + " CREATE_VIEW_PRIV",
                                    ts.toString());
                        }
                    }
                    break;

                case SHOW_VIEW_PRIV:
                    if (dbPrivSet.getBoolean(9)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put("role " + r + " on db:" + db + " SHOW_VIEW_PRIV",
                                    ts.toString());
                        }
                    }
                    break;

                }
            }
        }

    } catch (SQLException sqlex) {
        LOG.error("get user error, user=" + user + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        closeStatement(ps);
        closeConnection(con);
    }

    if (!outofdataInfoMap.isEmpty()) {
        StringBuilder sb = new StringBuilder();
        sb.append("priv out of date, you should renewal you privlage; the detail information is ");
        sb.append("\n");
        for (Entry<String, String> e : outofdataInfoMap.entrySet()) {
            sb.append("priv:" + e.getKey() + ",");
            sb.append("out of date:" + e.getValue());
            sb.append("\n");
        }
        throw new MetaException(sb.toString());
    }

    return false;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public boolean hasAuthOnTbl(String who, String db, String table, int privIndex)
        throws NoSuchObjectException, MetaException {
    Connection con = null;/*from w w  w .j a  v  a2 s.  co  m*/
    ;
    Statement ps = null;
    Privilege priv = privMap.get(privIndex);
    who = who.toLowerCase();
    db = db.toLowerCase();
    table = table.toLowerCase();

    long nowtime = (new Date()).getTime();
    Map<String, String> outofdataInfoMap = new HashMap<String, String>();
    Timestamp ts = null;
    long outofdateTime = 0;
    boolean isOutofdate = false;

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("audit error, user=" + user + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("audit error, user=" + user + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        ps = con.createStatement();

        String sql = "select alter_priv, create_priv, createview_priv, dba_priv, "
                + "delete_priv, drop_priv, index_priv, insert_priv, select_priv, showview_priv, "
                + "update_priv, user_name, group_name, out_of_date_time from tdwuser where tdwuser.user_name='"
                + who.toLowerCase() + "'";

        ResultSet userSet = ps.executeQuery(sql);
        boolean isUserFind = false;

        while (userSet.next()) {
            isUserFind = true;

            ts = userSet.getTimestamp(14);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }

            boolean isDBA = userSet.getBoolean(4);
            if (isDBA) {
                return true;
            }

            switch (priv) {
            case SELECT_PRIV:
                if (userSet.getBoolean(9)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " SELECT_PRIV", ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (userSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " ALTER_PRIV", ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (userSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " INSERT_PRIV", ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (userSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " INDEX_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (userSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " CREATE_PRIV", ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (userSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " DROP_PRIV", ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (userSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " DELETE_PRIV", ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (userSet.getBoolean(11)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " UPDATE_PRIV", ts.toString());
                    }
                }
                break;

            }
        }

        userSet.close();

        if (!isUserFind) {
            throw new NoSuchObjectException("can not find user:" + who);
        }

        sql = "select alter_priv, create_priv, createview_priv, "
                + "delete_priv, drop_priv, index_priv, insert_priv, select_priv, "
                + "showview_priv, update_priv, out_of_date_time from dbpriv where user_name='"
                + who.toLowerCase() + "' and db_name='" + db.toLowerCase() + "'";

        ResultSet dbPrivSet = ps.executeQuery(sql);

        while (dbPrivSet.next()) {
            ts = dbPrivSet.getTimestamp(11);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }

            switch (priv) {
            case SELECT_PRIV:
                if (dbPrivSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " SELECT_PRIV", ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (dbPrivSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " ALTER_PRIV", ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (dbPrivSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " INSERT_PRIV", ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (dbPrivSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " INDEX_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (dbPrivSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " CREATE_PRIV", ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (dbPrivSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " DROP_PRIV", ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (dbPrivSet.getBoolean(4)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " DELETE_PRIV", ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (dbPrivSet.getBoolean(10)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " UPDATE_PRIV", ts.toString());
                    }
                }
                break;

            }
        }

        dbPrivSet.close();

        sql = "select alter_priv, create_priv, delete_priv "
                + ",drop_priv, index_priv, insert_priv, select_priv, update_priv, out_of_date_time"
                + " from tblpriv where user_name='" + who.toLowerCase() + "' and db_name='" + db.toLowerCase()
                + "' and tbl_name='" + table.toLowerCase() + "'";

        ResultSet tblSet = ps.executeQuery(sql);

        while (tblSet.next()) {
            ts = tblSet.getTimestamp(9);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }

            switch (priv) {
            case SELECT_PRIV:
                if (tblSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on tbl:" + db + "/" + table + " SELECT_PRIV",
                                ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (tblSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on tbl:" + db + "/" + table + " ALTER_PRIV",
                                ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (tblSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on tbl:" + db + "/" + table + " INSERT_PRIV",
                                ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (tblSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on tbl:" + db + "/" + table + " INDEX_PRIV",
                                ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (tblSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on tbl:" + db + "/" + table + " CREATE_PRIV",
                                ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (tblSet.getBoolean(4)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on tbl:" + db + "/" + table + " DROP_PRIV",
                                ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (tblSet.getBoolean(3)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on tbl:" + db + "/" + table + " DELETE_PRIV",
                                ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (tblSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on tbl:" + db + "/" + table + " UPDATE_PRIV",
                                ts.toString());
                    }
                }
                break;
            }
            break;
        }

        tblSet.close();

        sql = "select tdwrole.alter_priv, tdwrole.create_priv, tdwrole.createview_priv, tdwrole.dba_priv, "
                + "tdwrole.delete_priv, tdwrole.drop_priv, tdwrole.index_priv, tdwrole.insert_priv, tdwrole.select_priv, tdwrole.showview_priv, "
                + "tdwrole.update_priv, tdwrole.role_name, tdwrole.out_of_date_time from tdwrole, tdwuserrole where tdwuserrole.user_name='"
                + who.toLowerCase() + "'" + " and tdwrole.role_name=tdwuserrole.role_name";

        ResultSet roleSet = ps.executeQuery(sql);

        String rName = null;
        List<String> roleList = new ArrayList<String>();
        while (roleSet.next()) {
            rName = roleSet.getString(12);
            ts = roleSet.getTimestamp(13);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }
            boolean isDBA = roleSet.getBoolean(4);
            if (isDBA) {
                return true;
            }

            switch (priv) {
            case SELECT_PRIV:
                if (roleSet.getBoolean(9)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + rName + " SELECT_PRIV", ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (roleSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + rName + " ALTER_PRIV", ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (roleSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + rName + " INSERT_PRIV", ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (roleSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + rName + " INDEX_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (roleSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + rName + " CREATE_PRIV", ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (roleSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + rName + " DROP_PRIV", ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (roleSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + rName + " DELETE_PRIV", ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (roleSet.getBoolean(11)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + rName + " UPDATE_PRIV", ts.toString());
                    }
                }
                break;

            }

            roleList.add(roleSet.getString(12));
        }
        roleSet.close();

        for (String roleName : roleList) {
            sql = "select alter_priv, create_priv, createview_priv, "
                    + "delete_priv, drop_priv, index_priv, insert_priv, select_priv, "
                    + "showview_priv, update_priv, out_of_date_time from dbpriv where user_name='"
                    + roleName.toLowerCase() + "' and db_name='" + db.toLowerCase() + "'";

            dbPrivSet = ps.executeQuery(sql);

            while (dbPrivSet.next()) {
                ts = dbPrivSet.getTimestamp(11);

                if (ts != null) {
                    outofdateTime = ts.getTime();
                } else {
                    outofdateTime = Long.MAX_VALUE;
                }

                if (nowtime > outofdateTime) {
                    isOutofdate = true;
                } else {
                    isOutofdate = false;
                }

                switch (priv) {
                case SELECT_PRIV:
                    if (dbPrivSet.getBoolean(8)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put("role " + roleName + " on db:" + db + " SELECT_PRIV",
                                    ts.toString());
                        }
                    }
                    break;

                case ALTER_PRIV:
                    if (dbPrivSet.getBoolean(1)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put("role " + roleName + " on db:" + db + " ALTER_PRIV",
                                    ts.toString());
                        }
                    }
                    break;

                case INSERT_PRIV:
                    if (dbPrivSet.getBoolean(7)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put("role " + roleName + " on db:" + db + " INSERT_PRIV",
                                    ts.toString());
                        }
                    }
                    break;

                case INDEX_PRIV:
                    if (dbPrivSet.getBoolean(6)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put("role " + roleName + " on db:" + db + " INDEX_PRIV",
                                    ts.toString());
                        }
                    }
                    break;

                case CREATE_PRIV:
                    if (dbPrivSet.getBoolean(2)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put("role " + roleName + " on db:" + db + " CREATE_PRIV",
                                    ts.toString());
                        }
                    }
                    break;

                case DROP_PRIV:
                    if (dbPrivSet.getBoolean(5)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put("role " + roleName + " on db:" + db + " DROP_PRIV",
                                    ts.toString());
                        }
                    }
                    break;

                case DELETE_PRIV:
                    if (dbPrivSet.getBoolean(4)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put("role " + roleName + " on db:" + db + " DELETE_PRIV",
                                    ts.toString());
                        }
                    }
                    break;

                case UPDATE_PRIV:
                    if (dbPrivSet.getBoolean(10)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put("role " + roleName + " on db:" + db + " UPDATE_PRIV",
                                    ts.toString());
                        }
                    }
                    break;

                case CREATE_VIEW_PRIV:
                    if (dbPrivSet.getBoolean(3)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put("role " + roleName + " on db:" + db + " CREATE_VIEW_PRIV",
                                    ts.toString());
                        }
                    }
                    break;

                case SHOW_VIEW_PRIV:
                    if (dbPrivSet.getBoolean(9)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put("role " + roleName + " on db:" + db + " CREATE_VIEW_PRIV",
                                    ts.toString());
                        }
                    }
                    break;

                }
            }

            dbPrivSet.close();

            sql = "select alter_priv, create_priv, delete_priv "
                    + ",drop_priv, index_priv, insert_priv, select_priv, update_priv, out_of_date_time "
                    + " from tblpriv where user_name='" + roleName.toLowerCase() + "' and db_name='"
                    + db.toLowerCase() + "' and tbl_name='" + table.toLowerCase() + "'";

            tblSet = ps.executeQuery(sql);

            while (tblSet.next()) {
                ts = tblSet.getTimestamp(9);

                if (ts != null) {
                    outofdateTime = ts.getTime();
                } else {
                    outofdateTime = Long.MAX_VALUE;
                }

                if (nowtime > outofdateTime) {
                    isOutofdate = true;
                } else {
                    isOutofdate = false;
                }

                switch (priv) {
                case SELECT_PRIV:
                    if (tblSet.getBoolean(7)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put(
                                    "role " + roleName + " on tbl:" + db + "/" + table + " SELECT_PRIV",
                                    ts.toString());
                        }
                    }
                    break;

                case ALTER_PRIV:
                    if (tblSet.getBoolean(1)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put(
                                    "role " + roleName + " on tbl:" + db + "/" + table + " ALTER_PRIV",
                                    ts.toString());
                        }
                    }
                    break;

                case INSERT_PRIV:
                    if (tblSet.getBoolean(6)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put(
                                    "role " + roleName + " on tbl:" + db + "/" + table + " INSERT_PRIV",
                                    ts.toString());
                        }
                    }
                    break;

                case INDEX_PRIV:
                    if (tblSet.getBoolean(5)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put(
                                    "role " + roleName + " on tbl:" + db + "/" + table + " INDEX_PRIV",
                                    ts.toString());
                        }
                    }
                    break;

                case CREATE_PRIV:
                    if (tblSet.getBoolean(2)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put(
                                    "role " + roleName + " on tbl:" + db + "/" + table + " CREATE_PRIV",
                                    ts.toString());
                        }
                    }
                    break;

                case DROP_PRIV:
                    if (tblSet.getBoolean(4)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put(
                                    "role " + roleName + " on tbl:" + db + "/" + table + " DROP_PRIV",
                                    ts.toString());
                        }
                    }
                    break;

                case DELETE_PRIV:
                    if (tblSet.getBoolean(3)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put(
                                    "role " + roleName + " on tbl:" + db + "/" + table + " DELETE_PRIV",
                                    ts.toString());
                        }
                    }
                    break;

                case UPDATE_PRIV:
                    if (tblSet.getBoolean(8)) {
                        if (!isOutofdate) {
                            return true;
                        } else {
                            outofdataInfoMap.put(
                                    "role " + roleName + " on tbl:" + db + "/" + table + " UPDATE_PRIV",
                                    ts.toString());
                        }
                    }
                    break;
                }
                break;
            }

            tblSet.close();
        }

    } catch (SQLException sqlex) {
        LOG.error("get user error, user=" + user + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        closeStatement(ps);
        closeConnection(con);
    }

    if (!outofdataInfoMap.isEmpty()) {
        StringBuilder sb = new StringBuilder();
        sb.append("priv out of date, you should renewal you privlage; the detail information is ");
        sb.append("\n");
        for (Entry<String, String> e : outofdataInfoMap.entrySet()) {
            sb.append("priv:" + e.getKey() + ",");
            sb.append("out of date:" + e.getValue());
            sb.append("\n");
        }
        throw new MetaException(sb.toString());
    }

    return false;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public boolean hasAuthOnDbWithRole(String who, String role, String db, int privIndex)
        throws NoSuchObjectException, MetaException {
    Connection con = null;// ww  w . ja  v  a2  s . com
    ;
    Statement ps = null;
    Privilege priv = privMap.get(privIndex);
    who = who.toLowerCase();
    db = db.toLowerCase();
    role = role.toLowerCase();

    long nowtime = (new Date()).getTime();
    Map<String, String> outofdataInfoMap = new HashMap<String, String>();
    Timestamp ts = null;
    long outofdateTime = 0;
    boolean isOutofdate = false;

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("audit error, user=" + user + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("audit error, user=" + user + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        ps = con.createStatement();

        String sql = "select tdwrole.alter_priv, tdwrole.create_priv, tdwrole.createview_priv, tdwrole.dba_priv, "
                + "tdwrole.delete_priv, tdwrole.drop_priv, tdwrole.index_priv, tdwrole.insert_priv, tdwrole.select_priv, tdwrole.showview_priv, "
                + "tdwrole.update_priv, tdwrole.role_name, tdwrole.out_of_date_time from tdwrole, tdwuserrole where tdwuserrole.user_name='"
                + who.toLowerCase() + "' and tdwrole.role_name='" + role + "' "
                + " and tdwrole.role_name=tdwuserrole.role_name";

        ResultSet roleSet = ps.executeQuery(sql);

        boolean isRoleFind = false;
        while (roleSet.next()) {
            isRoleFind = true;
            ts = roleSet.getTimestamp(13);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }

            boolean isDBA = roleSet.getBoolean(4);
            if (isDBA) {
                return true;
            }

            switch (priv) {
            case SELECT_PRIV:
                if (roleSet.getBoolean(9)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " SELECT_PRIV", ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (roleSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " ALTER_PRIV", ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (roleSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " INSERT_PRIV", ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (roleSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " INDEX_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (roleSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " CREATE_PRIV", ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (roleSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " DROP_PRIV", ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (roleSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " DELETE_PRIV", ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (roleSet.getBoolean(11)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " UPDATE_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_VIEW_PRIV:
                if (roleSet.getBoolean(3)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " CREATE_VIEW_PRIV", ts.toString());
                    }
                }
                break;

            case SHOW_VIEW_PRIV:
                if (roleSet.getBoolean(10)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " SHOW_VIEW_PRIV", ts.toString());
                    }
                }
                break;

            case DBA_PRIV:
                if (roleSet.getBoolean(4)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " DBA_PRIV", ts.toString());
                    }
                }
                break;

            }
            break;
        }

        roleSet.close();
        if (!isRoleFind) {
            LOG.error("user/role does not exist or user does not play this role:" + who + "/" + role);
            throw new MetaException("user/role does not exist or user does not play this role:" + who + "/"
                    + role + ", you shoule check it");
        }

        sql = "select alter_priv, create_priv, createview_priv, "
                + "delete_priv, drop_priv, index_priv, insert_priv, select_priv, "
                + "showview_priv, update_priv, out_of_date_time from dbpriv where user_name='" + role
                + "' and db_name='" + db.toLowerCase() + "'";

        ResultSet dbPrivSet = ps.executeQuery(sql);

        while (dbPrivSet.next()) {
            ts = dbPrivSet.getTimestamp(11);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }

            switch (priv) {
            case SELECT_PRIV:
                if (dbPrivSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on db:" + db + " SELECT_PRIV", ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (dbPrivSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on db:" + db + " ALTER_PRIV", ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (dbPrivSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on db:" + db + " INSERT_PRIV", ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (dbPrivSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on db:" + db + " INDEX_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (dbPrivSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on db:" + db + " CREATE_PRIV", ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (dbPrivSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on db:" + db + " DROP_PRIV", ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (dbPrivSet.getBoolean(4)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on db:" + db + " DELETE_PRIV", ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (dbPrivSet.getBoolean(10)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on db:" + db + " UPDATE_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_VIEW_PRIV:
                if (dbPrivSet.getBoolean(3)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on db:" + db + " CREATE_VIEW_PRIV",
                                ts.toString());
                    }
                }
                break;

            case SHOW_VIEW_PRIV:
                if (dbPrivSet.getBoolean(9)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on db:" + db + " SHOW_VIEW_PRIV",
                                ts.toString());
                    }
                }
                break;

            }
            break;
        }

        sql = "select alter_priv, create_priv, createview_priv, dba_priv, "
                + "delete_priv, drop_priv, index_priv, insert_priv, select_priv, showview_priv, "
                + "update_priv, user_name, group_name, out_of_date_time from tdwuser where tdwuser.user_name='"
                + who.toLowerCase() + "'";

        ResultSet userSet = ps.executeQuery(sql);
        boolean isUserFind = false;

        while (userSet.next()) {
            isUserFind = true;

            ts = userSet.getTimestamp(14);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }

            boolean isDBA = userSet.getBoolean(4);
            if (isDBA) {
                return true;
            }

            switch (priv) {
            case SELECT_PRIV:
                if (userSet.getBoolean(9)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " SELECT_PRIV", ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (userSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " ALTER_PRIV", ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (userSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " INSERT_PRIV", ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (userSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " INDEX_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (userSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " CREATE_PRIV", ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (userSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " DROP_PRIV", ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (userSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " DELETE_PRIV", ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (userSet.getBoolean(11)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " UPDATE_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_VIEW_PRIV:
                if (userSet.getBoolean(3)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " CREATE_VIEW_PRIV", ts.toString());
                    }
                }
                break;

            case SHOW_VIEW_PRIV:
                if (userSet.getBoolean(10)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " SHOW_VIEW_PRIV", ts.toString());
                    }
                }
                break;

            }
            break;
        }

        userSet.close();

        if (!isUserFind) {
            throw new NoSuchObjectException("can not find user:" + who);
        }

        sql = "select alter_priv, create_priv, createview_priv, "
                + "delete_priv, drop_priv, index_priv, insert_priv, select_priv, "
                + "showview_priv, update_priv, out_of_date_time from dbpriv where user_name='"
                + who.toLowerCase() + "' and db_name='" + db.toLowerCase() + "'";

        dbPrivSet = ps.executeQuery(sql);

        while (dbPrivSet.next()) {
            ts = dbPrivSet.getTimestamp(11);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }

            switch (priv) {
            case SELECT_PRIV:
                if (dbPrivSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " SELECT_PRIV", ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (dbPrivSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " ALTER_PRIV", ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (dbPrivSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " INSERT_PRIV", ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (dbPrivSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " INDEX_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (dbPrivSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " CREATE_PRIV", ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (dbPrivSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " DROP_PRIV", ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (dbPrivSet.getBoolean(4)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " DELETE_PRIV", ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (dbPrivSet.getBoolean(10)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " UPDATE_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_VIEW_PRIV:
                if (dbPrivSet.getBoolean(3)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " CREATE_VIEW_PRIV",
                                ts.toString());
                    }
                }
                break;

            case SHOW_VIEW_PRIV:
                if (dbPrivSet.getBoolean(9)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " SHOW_VIEW_PRIV", ts.toString());
                    }
                }
                break;

            }
            break;
        }

    } catch (SQLException sqlex) {
        LOG.error("get user error, user=" + user + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        closeStatement(ps);
        closeConnection(con);
    }

    if (!outofdataInfoMap.isEmpty()) {
        StringBuilder sb = new StringBuilder();
        sb.append("priv out of date, you should renewal you privlage; the detail information is ");
        sb.append("\n");
        for (Entry<String, String> e : outofdataInfoMap.entrySet()) {
            sb.append("priv:" + e.getKey() + ",");
            sb.append("out of date:" + e.getValue());
            sb.append("\n");
        }
        throw new MetaException(sb.toString());
    }

    return false;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public boolean hasAuthOnTblWithRole(String who, String role, String db, String table, int privIndex)
        throws NoSuchObjectException, MetaException {
    Connection con = null;/* ww  w . j a v a 2 s.c om*/
    ;
    Statement ps = null;
    Privilege priv = privMap.get(privIndex);
    who = who.toLowerCase();
    db = db.toLowerCase();
    table = table.toLowerCase();
    role = role.toLowerCase();

    long nowtime = (new Date()).getTime();
    Map<String, String> outofdataInfoMap = new HashMap<String, String>();
    Timestamp ts = null;
    long outofdateTime = 0;
    boolean isOutofdate = false;

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("audit error, user=" + user + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("audit error, user=" + user + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        ps = con.createStatement();

        String sql = "select tdwrole.alter_priv, tdwrole.create_priv, tdwrole.createview_priv, tdwrole.dba_priv, "
                + "tdwrole.delete_priv, tdwrole.drop_priv, tdwrole.index_priv, tdwrole.insert_priv, tdwrole.select_priv, tdwrole.showview_priv, "
                + "tdwrole.update_priv, tdwrole.role_name, tdwrole.out_of_date_time from tdwrole, tdwuserrole where tdwuserrole.user_name='"
                + who.toLowerCase() + "' and tdwrole.role_name='" + role
                + "' and tdwuserrole.role_name=tdwrole.role_name";

        System.out.println(sql);

        ResultSet roleSet = ps.executeQuery(sql);
        boolean isRoleFind = false;

        while (roleSet.next()) {
            isRoleFind = true;
            ts = roleSet.getTimestamp(13);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }
            boolean isDBA = roleSet.getBoolean(4);
            if (isDBA) {
                return true;
            }

            switch (priv) {
            case SELECT_PRIV:
                if (roleSet.getBoolean(9)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " SELECT_PRIV", ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (roleSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " ALTER_PRIV", ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (roleSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " INSERT_PRIV", ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (roleSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " INDEX_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (roleSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " CREATE_PRIV", ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (roleSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " DROP_PRIV", ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (roleSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " DELETE_PRIV", ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (roleSet.getBoolean(11)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " UPDATE_PRIV", ts.toString());
                    }
                }
                break;

            }
            break;
        }
        roleSet.close();

        if (!isRoleFind) {
            LOG.error("user/role does not exist or user does not play this role:" + who + "/" + role);
            throw new MetaException("user/role does not exist or user does not play this role:" + who + "/"
                    + role + ", you shoule check it");
        }

        sql = "select alter_priv, create_priv, createview_priv, "
                + "delete_priv, drop_priv, index_priv, insert_priv, select_priv, "
                + "showview_priv, update_priv, out_of_date_time from dbpriv where user_name='" + role
                + "' and db_name='" + db.toLowerCase() + "'";

        System.out.println(sql);

        ResultSet dbPrivSet = ps.executeQuery(sql);

        while (dbPrivSet.next()) {
            ts = dbPrivSet.getTimestamp(11);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }

            switch (priv) {
            case SELECT_PRIV:
                if (dbPrivSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on db:" + db + " SELECT_PRIV", ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (dbPrivSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on db:" + db + " ALTER_PRIV", ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (dbPrivSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on db:" + db + " INSERT_PRIV", ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (dbPrivSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on db:" + db + " INDEX_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (dbPrivSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on db:" + db + " CREATE_PRIV", ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (dbPrivSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on db:" + db + " DROP_PRIV", ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (dbPrivSet.getBoolean(4)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on db:" + db + " DELETE_PRIV", ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (dbPrivSet.getBoolean(10)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on db:" + db + " UPDATE_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_VIEW_PRIV:
                if (dbPrivSet.getBoolean(3)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on db:" + db + " CREATE_VIEW_PRIV",
                                ts.toString());
                    }
                }
                break;

            case SHOW_VIEW_PRIV:
                if (dbPrivSet.getBoolean(9)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on db:" + db + " CREATE_VIEW_PRIV",
                                ts.toString());
                    }
                }
                break;

            }
            break;
        }

        dbPrivSet.close();

        sql = "select alter_priv, create_priv, delete_priv "
                + ",drop_priv, index_priv, insert_priv, select_priv, update_priv, out_of_date_time "
                + " from tblpriv where user_name='" + role + "' and db_name='" + db.toLowerCase()
                + "' and tbl_name='" + table.toLowerCase() + "'";

        System.out.println(sql);

        ResultSet tblSet = ps.executeQuery(sql);

        while (tblSet.next()) {
            ts = tblSet.getTimestamp(9);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }

            switch (priv) {
            case SELECT_PRIV:
                if (tblSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on tbl:" + db + "/" + table + " SELECT_PRIV",
                                ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (tblSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on tbl:" + db + "/" + table + " ALTER_PRIV",
                                ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (tblSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on tbl:" + db + "/" + table + " INSERT_PRIV",
                                ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (tblSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on tbl:" + db + "/" + table + " INDEX_PRIV",
                                ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (tblSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on tbl:" + db + "/" + table + " CREATE_PRIV",
                                ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (tblSet.getBoolean(4)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on tbl:" + db + "/" + table + " DROP_PRIV",
                                ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (tblSet.getBoolean(3)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on tbl:" + db + "/" + table + " DELETE_PRIV",
                                ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (tblSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("role " + role + " on tbl:" + db + "/" + table + " UPDATE_PRIV",
                                ts.toString());
                    }
                }
                break;
            }
            break;
        }

        tblSet.close();

        sql = "select alter_priv, create_priv, createview_priv, dba_priv, "
                + "delete_priv, drop_priv, index_priv, insert_priv, select_priv, showview_priv, "
                + "update_priv, user_name, group_name, out_of_date_time from tdwuser where tdwuser.user_name='"
                + who.toLowerCase() + "'";

        ResultSet userSet = ps.executeQuery(sql);
        boolean isUserFind = false;

        while (userSet.next()) {
            isUserFind = true;

            ts = userSet.getTimestamp(14);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }

            boolean isDBA = userSet.getBoolean(4);
            if (isDBA) {
                return true;
            }

            switch (priv) {
            case SELECT_PRIV:
                if (userSet.getBoolean(9)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " SELECT_PRIV", ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (userSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " ALTER_PRIV", ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (userSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " INSERT_PRIV", ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (userSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " INDEX_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (userSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " CREATE_PRIV", ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (userSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " DROP_PRIV", ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (userSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " DELETE_PRIV", ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (userSet.getBoolean(11)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " UPDATE_PRIV", ts.toString());
                    }
                }
                break;

            }
            break;
        }

        userSet.close();

        if (!isUserFind) {
            throw new NoSuchObjectException("can not find user:" + who);
        }

        sql = "select alter_priv, create_priv, createview_priv, "
                + "delete_priv, drop_priv, index_priv, insert_priv, select_priv, "
                + "showview_priv, update_priv, out_of_date_time from dbpriv where user_name='"
                + who.toLowerCase() + "' and db_name='" + db.toLowerCase() + "'";

        dbPrivSet = ps.executeQuery(sql);

        while (dbPrivSet.next()) {
            ts = dbPrivSet.getTimestamp(11);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }

            switch (priv) {
            case SELECT_PRIV:
                if (dbPrivSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " SELECT_PRIV", ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (dbPrivSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " ALTER_PRIV", ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (dbPrivSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " INSERT_PRIV", ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (dbPrivSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " INDEX_PRIV", ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (dbPrivSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " CREATE_PRIV", ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (dbPrivSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " DROP_PRIV", ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (dbPrivSet.getBoolean(4)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " DELETE_PRIV", ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (dbPrivSet.getBoolean(10)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on db:" + db + " UPDATE_PRIV", ts.toString());
                    }
                }
                break;

            }
            break;
        }

        dbPrivSet.close();

        sql = "select alter_priv, create_priv, delete_priv "
                + ",drop_priv, index_priv, insert_priv, select_priv, update_priv, out_of_date_time"
                + " from tblpriv where user_name='" + who.toLowerCase() + "' and db_name='" + db.toLowerCase()
                + "' and tbl_name='" + table.toLowerCase() + "'";

        LOG.error(sql);

        tblSet = ps.executeQuery(sql);

        while (tblSet.next()) {
            ts = tblSet.getTimestamp(9);

            if (ts != null) {
                outofdateTime = ts.getTime();
            } else {
                outofdateTime = Long.MAX_VALUE;
            }

            if (nowtime > outofdateTime) {
                isOutofdate = true;
            } else {
                isOutofdate = false;
            }

            switch (priv) {
            case SELECT_PRIV:
                if (tblSet.getBoolean(7)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on tbl:" + db + "/" + table + " SELECT_PRIV",
                                ts.toString());
                    }
                }
                break;

            case ALTER_PRIV:
                if (tblSet.getBoolean(1)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on tbl:" + db + "/" + table + " ALTER_PRIV",
                                ts.toString());
                    }
                }
                break;

            case INSERT_PRIV:
                if (tblSet.getBoolean(6)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on tbl:" + db + "/" + table + " INSERT_PRIV",
                                ts.toString());
                    }
                }
                break;

            case INDEX_PRIV:
                if (tblSet.getBoolean(5)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on tbl:" + db + "/" + table + " INDEX_PRIV",
                                ts.toString());
                    }
                }
                break;

            case CREATE_PRIV:
                if (tblSet.getBoolean(2)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on tbl:" + db + "/" + table + " CREATE_PRIV",
                                ts.toString());
                    }
                }
                break;

            case DROP_PRIV:
                if (tblSet.getBoolean(4)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on tbl:" + db + "/" + table + " DROP_PRIV",
                                ts.toString());
                    }
                }
                break;

            case DELETE_PRIV:
                if (tblSet.getBoolean(3)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on tbl:" + db + "/" + table + " DELETE_PRIV",
                                ts.toString());
                    }
                }
                break;

            case UPDATE_PRIV:
                if (tblSet.getBoolean(8)) {
                    if (!isOutofdate) {
                        return true;
                    } else {
                        outofdataInfoMap.put("user " + who + " on tbl:" + db + "/" + table + " UPDATE_PRIV",
                                ts.toString());
                    }
                }
                break;
            }
            break;
        }

        tblSet.close();

    } catch (SQLException sqlex) {
        LOG.error("get user error, user=" + user + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        closeStatement(ps);
        closeConnection(con);
    }

    if (!outofdataInfoMap.isEmpty()) {
        StringBuilder sb = new StringBuilder();
        sb.append("priv out of date, you should renewal you privlage; the detail information is ");
        sb.append("\n");
        for (Entry<String, String> e : outofdataInfoMap.entrySet()) {
            sb.append("priv:" + e.getKey() + ",");
            sb.append("out of date:" + e.getValue());
            sb.append("\n");
        }
        throw new MetaException(sb.toString());
    }

    return false;
}