List of usage examples for com.liferay.portal.kernel.dao.jdbc DataAccess cleanUp
public static void cleanUp(Statement statement)
From source file:com.fingence.slayer.service.impl.MyResultServiceImpl.java
License:Open Source License
public JSONArray getCollateralBreakdown(String portfolioIds) { JSONArray jsonArray = JSONFactoryUtil.createJSONArray(); Connection conn = null;//from ww w . j a va 2s .c om try { conn = DataAccess.getConnection(); String[] tokens = { "[$PORTFOLIO_IDS$]", "[$FING_BOND_COLUMNS$]", "[$FING_BOND_TABLE$]", "[$FING_BOND_WHERE_CLAUSE$]" }; String[] replacements = { portfolioIds, ",f.*, DATEDIFF(f.maturity_dt,now()) AS maturing_after", ",fing_Bond f", "and a.assetId = f.assetId" }; String sql = StringUtil.replace(CustomSQLUtil.get(QUERY), tokens, replacements); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); double totalMarketValue = getTotalMarketValue(portfolioIds); double totalValueOfBonds = 0.0; while (rs.next()) { String collatTyp = rs.getString("collat_typ"); double currentMarketValue = rs.getDouble("currentMarketValue"); totalValueOfBonds += currentMarketValue; JSONObject jsonObj = null; if (jsonArray.length() > 0) { for (int i = 0; i < jsonArray.length(); i++) { JSONObject _jsonObj = jsonArray.getJSONObject(i); if (_jsonObj.getString("bucket").equalsIgnoreCase(collatTyp)) { jsonObj = _jsonObj; break; } } } if (Validator.isNull(jsonObj)) { jsonObj = JSONFactoryUtil.createJSONObject(); jsonObj.put("bucket", collatTyp); jsonObj.put("market_value", 0.0); jsonObj.put("bond_holdings_percent", 0.0); jsonObj.put("total_holdings_percent", 0.0); jsonArray.put(jsonObj); } jsonObj.put("market_value", jsonObj.getDouble("market_value") + currentMarketValue); jsonObj.put("total_holdings_percent", jsonObj.getDouble("total_holdings_percent") + currentMarketValue * 100 / totalMarketValue); } rs.close(); stmt.close(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObj = jsonArray.getJSONObject(i); jsonObj.put("bond_holdings_percent", jsonObj.getDouble("market_value") * 100 / totalValueOfBonds); } } catch (SQLException e) { e.printStackTrace(); } finally { DataAccess.cleanUp(conn); } return jsonArray; }
From source file:com.fingence.slayer.service.impl.MyResultServiceImpl.java
License:Open Source License
public JSONArray getCashFlow(String portfolioIds) { JSONArray jsonArray = JSONFactoryUtil.createJSONArray(); Connection conn = null;//from w w w . ja va2 s .co m try { conn = DataAccess.getConnection(); String[] tokens = { "[$PORTFOLIO_IDS$]", "[$FING_BOND_COLUMNS$]", "[$FING_BOND_TABLE$]", "[$FING_BOND_WHERE_CLAUSE$]" }; String[] replacements = { portfolioIds, ",b.portfolioName ,f.*", ", fing_Bond f", "and a.assetId = f.assetId" }; String sql = StringUtil.replace(CustomSQLUtil.get(QUERY), tokens, replacements); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { String portfolioName = rs.getString("portfolioName"); Date date = new Date(); double calcTyp = rs.getDouble("calc_typ"); String collatTyp = rs.getString("collat_typ"); String isBondNoCalcTyp = rs.getString("is_bond_no_calctyp"); // if bond if (calcTyp > 0.0d && Validator.isNotNull(collatTyp) && Validator.isNotNull(isBondNoCalcTyp)) { Date issueDate = rs.getDate("issue_dt"); List<History> histories = null; try { //System.out.println("Entered If, assetId :: " + rs.getLong("assetId")); OrderByComparator comparator = OrderByComparatorFactoryUtil .create(HistoryModelImpl.TABLE_NAME, "logDate", true); histories = historyPersistence.findByAssetId_Type(rs.getLong("assetId"), 3, 0, historyPersistence.countByAssetId(rs.getLong("assetId")), comparator); for (History history : histories) { //System.out.println("Asset Id :: " + rs.getLong("assetId") + ", History Log Date :: " + history.getLogDate() + ", issueDate :: " + issueDate); } //date = history.getLogDate(); } catch (SystemException e) { e.printStackTrace(); } } else { //date = rs.getDate("purchaseDate"); //System.out.println("Entered Else"); } String nameSecurityDes = rs.getString("name") + StringPool.SPACE + rs.getString("security_des"); String idIsin = rs.getString("id_isin"); double purchaseQty = rs.getDouble("purchaseQty"); double most_recent_reported_factor = rs.getDouble("amount_outstanding") / rs.getDouble("amount_issued"); double amountOutstanding = purchaseQty * most_recent_reported_factor; double cashFlow = amountOutstanding * (rs.getDouble("cpn") / (rs.getDouble("cpn_freq") * 100)); String currency = rs.getString("currencyDesc"); String currencySymbol = rs.getString("currencySymbol"); double currencyConversionRate = rs.getDouble("current_fx"); double cashFlowCurrency = cashFlow * currencyConversionRate; JSONObject jsonObj = JSONFactoryUtil.createJSONObject(); ; jsonObj.put("portfolioName", portfolioName); DateFormat dateFormat = new SimpleDateFormat("dd/MMM/YYYY"); jsonObj.put("date", dateFormat.format(date)); jsonObj.put("nameSecurityDes", nameSecurityDes); jsonObj.put("securityID", idIsin); jsonObj.put("purchaseQty", purchaseQty); jsonObj.put("amountOutstanding", amountOutstanding); jsonObj.put("transaction", ""); jsonObj.put("cashFlow", cashFlow); jsonObj.put("currency", currency); jsonObj.put("currencySymbol", currencySymbol); jsonObj.put("currencyConversionRate", currencyConversionRate); jsonObj.put("cashFlowCurrency", cashFlowCurrency); jsonArray.put(jsonObj); } rs.close(); stmt.close(); } catch (SQLException e) { e.printStackTrace(); } finally { DataAccess.cleanUp(conn); } return jsonArray; }
From source file:com.fingence.slayer.service.impl.MyResultServiceImpl.java
License:Open Source License
public JSONArray getBondsMaturing(String portfolioIds) { JSONArray jsonArray = JSONFactoryUtil.createJSONArray(); // initialization of JSONArray with default values for (int i = 0; i < bucketNames.length; i++) { JSONObject jsonObject = JSONFactoryUtil.createJSONObject(); jsonObject.put("bucket", bucketNames[i]); jsonObject.put("market_value", 0.0); jsonObject.put("bond_holdings_percent", 0.0); jsonObject.put("total_holdings_percent", 0.0); jsonArray.put(jsonObject);//from w ww . j av a 2 s . c o m } Connection conn = null; try { conn = DataAccess.getConnection(); String[] tokens = { "[$PORTFOLIO_IDS$]", "[$FING_BOND_COLUMNS$]", "[$FING_BOND_TABLE$]", "[$FING_BOND_WHERE_CLAUSE$]" }; String[] replacements = { portfolioIds, ",f.*, round(mty_years_tdy * 360) AS maturing_after", ",fing_Bond f", "and a.assetId = f.assetId" }; String sql = StringUtil.replace(CustomSQLUtil.get(QUERY), tokens, replacements); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); double totalMarketValue = getTotalMarketValue(portfolioIds); double totalValueOfBonds = 0.0; while (rs.next()) { int maturingAfter = rs.getInt("maturing_after"); double currentMarketValue = rs.getDouble("currentMarketValue"); totalValueOfBonds += currentMarketValue; int index = 0; if (maturingAfter > 0 && maturingAfter <= 210) { index = 1; } else if (maturingAfter > 210 && maturingAfter <= 365) { index = 2; } else if (maturingAfter > 365 && maturingAfter <= 730) { index = 3; } else if (maturingAfter > 730 && maturingAfter <= 1825) { index = 4; } else if (maturingAfter > 1825 && maturingAfter <= 3650) { index = 5; } else if (maturingAfter > 3650) { index = 6; } JSONObject jsonObj = jsonArray.getJSONObject(index); jsonObj.put("market_value", jsonObj.getDouble("market_value") + currentMarketValue); jsonObj.put("total_holdings_percent", jsonObj.getDouble("total_holdings_percent") + currentMarketValue * 100 / totalMarketValue); } rs.close(); stmt.close(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObj = jsonArray.getJSONObject(i); jsonObj.put("bond_holdings_percent", jsonObj.getDouble("market_value") * 100 / totalValueOfBonds); } } catch (SQLException e) { e.printStackTrace(); } finally { DataAccess.cleanUp(conn); } return jsonArray; }
From source file:com.fingence.slayer.service.impl.MyResultServiceImpl.java
License:Open Source License
public JSONArray getYldToMaturity(String portfolioIds) { JSONArray jsonArray = JSONFactoryUtil.createJSONArray(); for (int i = 0; i < yldToMaturityRange.length; i++) { JSONObject jsonObject = JSONFactoryUtil.createJSONObject(); if (i <= 5) { jsonObject.put("yldToMaturityRange", yldToMaturityRange[i][0] + StringPool.DASH + yldToMaturityRange[i][1]); } else {/*from w w w. j a va 2 s. c om*/ jsonObject.put("yldToMaturityRange", yldToMaturityRange[i][0] + StringPool.PLUS); } for (int j = 0; j < durationRange.length; j++) { jsonObject.put((int) durationRange[j][0] + StringPool.DASH + (int) durationRange[j][1], 0.0d); jsonObject.put("index" + j, (i + StringPool.COLON + j)); } jsonArray.put(jsonObject); } Connection conn = null; try { conn = DataAccess.getConnection(); String[] tokens = { "[$PORTFOLIO_IDS$]", "[$FING_BOND_COLUMNS$]", "[$FING_BOND_TABLE$]", "[$FING_BOND_WHERE_CLAUSE$]" }; String[] replacements = { portfolioIds, ",f.*", ",fing_Bond f", "and a.assetId = f.assetId" }; String sql = StringUtil.replace(CustomSQLUtil.get(QUERY), tokens, replacements); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); double totalValueOfBonds = 0.0; while (rs.next()) { double dur_mid = rs.getDouble("dur_mid"); double yld_ytm_bid = rs.getDouble("yld_ytm_bid"); double currentMarketValue = rs.getDouble("currentMarketValue"); totalValueOfBonds += currentMarketValue; for (int i = 0; i < yldToMaturityRange.length; i++) { if (yld_ytm_bid > yldToMaturityRange[i][0] && yld_ytm_bid <= yldToMaturityRange[i][1]) { JSONObject jsonObj = jsonArray.getJSONObject(i); for (int j = 0; j < durationRange.length; j++) { if (dur_mid > durationRange[j][0] && dur_mid <= durationRange[j][1]) { String key = (int) durationRange[j][0] + StringPool.DASH + (int) durationRange[j][1]; jsonObj.put(key, jsonObj.getDouble(key) + currentMarketValue); } } } } } rs.close(); stmt.close(); for (int i = 0; i < yldToMaturityRange.length; i++) { JSONObject jsonObj = jsonArray.getJSONObject(i); for (int j = 0; j < durationRange.length; j++) { String key = (int) durationRange[j][0] + StringPool.DASH + (int) durationRange[j][1]; jsonObj.put(key, jsonObj.getDouble(key) * 100 / totalValueOfBonds); } } // append a summary row JSONObject summary = JSONFactoryUtil.createJSONObject(); summary.put("summary", true); summary.put("yldToMaturityRange", "Total"); for (int i = 0; i < yldToMaturityRange.length; i++) { JSONObject jsonObj = jsonArray.getJSONObject(i); for (int j = 0; j < durationRange.length; j++) { String key = (int) durationRange[j][0] + StringPool.DASH + (int) durationRange[j][1]; if (Double.isNaN(summary.getDouble(key))) { summary.put(key, jsonObj.getDouble(key)); } else { summary.put(key, summary.getDouble(key) + jsonObj.getDouble(key)); } } } jsonArray.put(summary); } catch (SQLException e) { e.printStackTrace(); } finally { DataAccess.cleanUp(conn); } return jsonArray; }
From source file:com.fingence.slayer.service.impl.MyResultServiceImpl.java
License:Open Source License
public JSONArray getCpnTypVsMtyTyp(String portfolioIds) { JSONArray jsonArray = JSONFactoryUtil.createJSONArray(); List<String> cpnTypes = getDistinctValues("cpn_typ", portfolioIds); List<String> mtyTypes = getDistinctValues("mty_typ", portfolioIds); for (String cpnType : cpnTypes) { JSONObject jsonObject = JSONFactoryUtil.createJSONObject(); jsonObject.put("cpnType", cpnType); int i = 0; for (String mtyType : mtyTypes) { jsonObject.put(mtyType, 0.0d); jsonObject.put(cpnType + ++i, mtyType + StringPool.COLON + cpnType); }//w ww . j a va 2 s . c o m jsonObject.put("grandTotal", 0.0d); jsonArray.put(jsonObject); } Connection conn = null; try { conn = DataAccess.getConnection(); String[] tokens = { "[$PORTFOLIO_IDS$]", "[$FING_BOND_COLUMNS$]", "[$FING_BOND_TABLE$]", "[$FING_BOND_WHERE_CLAUSE$]" }; String[] replacements = { portfolioIds, ",f.*", ",fing_Bond f", "and a.assetId = f.assetId" }; String sql = StringUtil.replace(CustomSQLUtil.get(QUERY), tokens, replacements); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); double totalValueOfBonds = 0.0; while (rs.next()) { String cpn_typ = rs.getString("cpn_typ"); String mty_typ = rs.getString("mty_typ"); double currentMarketValue = rs.getDouble("currentMarketValue"); totalValueOfBonds += currentMarketValue; for (int i = 0; i < cpnTypes.size(); i++) { if (cpn_typ.equalsIgnoreCase(cpnTypes.get(i))) { JSONObject jsonObj = jsonArray.getJSONObject(i); for (String mtyType : mtyTypes) { if (mtyType.equalsIgnoreCase(mty_typ)) { jsonObj.put(mtyType, jsonObj.getDouble(mtyType) + currentMarketValue); } } } } } rs.close(); stmt.close(); for (int i = 0; i < cpnTypes.size(); i++) { JSONObject jsonObj = jsonArray.getJSONObject(i); for (String mtyType : mtyTypes) { jsonObj.put(mtyType, jsonObj.getDouble(mtyType) * 100 / totalValueOfBonds); if (Double.isNaN(jsonObj.getDouble("grandTotal"))) { jsonObj.put("grandTotal", jsonObj.getDouble(mtyType)); } else { jsonObj.put("grandTotal", jsonObj.getDouble(mtyType) + jsonObj.getDouble("grandTotal")); } } } // append a summary row double grandTotal = 0.0d; JSONObject summary = JSONFactoryUtil.createJSONObject(); summary.put("summary", true); summary.put("cpnType", "Grand Total"); for (int i = 0; i < cpnTypes.size(); i++) { JSONObject jsonObj = jsonArray.getJSONObject(i); for (String mtyType : mtyTypes) { if (Double.isNaN(summary.getDouble(mtyType))) { summary.put(mtyType, jsonObj.getDouble(mtyType)); } else { summary.put(mtyType, summary.getDouble(mtyType) + jsonObj.getDouble(mtyType)); grandTotal += summary.getDouble(mtyType); } } } summary.put("grandTotal", grandTotal); jsonArray.put(summary); } catch (SQLException e) { e.printStackTrace(); } finally { DataAccess.cleanUp(conn); } return jsonArray; }
From source file:com.fingence.slayer.service.impl.MyResultServiceImpl.java
License:Open Source License
public JSONArray getBondsQuality(String portfolioIds) { String[] categories = { "Investment", "Non Investment", "Others" }; JSONArray jsonArray = JSONFactoryUtil.createJSONArray(); // initialization of JSONArray with default values for (int i = 0; i < categories.length; i++) { JSONObject jsonObject = JSONFactoryUtil.createJSONObject(); jsonObject.put("category", categories[i]); jsonObject.put("children", JSONFactoryUtil.createJSONArray()); jsonArray.put(jsonObject);//from ww w .j a v a 2 s . c om } Connection conn = null; try { conn = DataAccess.getConnection(); String[] tokens = { "[$PORTFOLIO_IDS$]", "[$FING_BOND_COLUMNS$]", "[$FING_BOND_TABLE$]", "[$FING_BOND_WHERE_CLAUSE$]" }; String[] replacements = { portfolioIds, ",f.*", ",fing_Bond f", "and a.assetId = f.assetId" }; String sql = StringUtil.replace(CustomSQLUtil.get(QUERY), tokens, replacements); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); double totalMarketValue = getTotalMarketValue(portfolioIds); double totalValueOfBonds = 0.0; while (rs.next()) { double currentMarketValue = rs.getDouble("currentMarketValue"); totalValueOfBonds += currentMarketValue; String spRating = rs.getString("rtg_sp"); String moodyRating = rs.getString("rtg_moody"); Rating rating = null; try { rating = ratingPersistence.fetchBySP_Moody(spRating, moodyRating); } catch (SystemException e) { e.printStackTrace(); } // identify the object int index = 2; String description = "No Rating Available"; if (Validator.isNotNull(rating)) { String category = rating.getCategory(); description = rating.getDescription(); for (int i = 0; i < categories.length; i++) { if (categories[i].equalsIgnoreCase(category)) { index = i; } } } JSONArray children = jsonArray.getJSONObject(index).getJSONArray("children"); // identify the child within the parent JSONObject child = null; if (children.length() == 0) { child = JSONFactoryUtil.createJSONObject(); child.put("bucket", description); child.put("market_value", 0.0); child.put("bond_holdings_percent", 0.0); child.put("total_holdings_percent", 0.0); children.put(child); } for (int i = 0; i < children.length(); i++) { child = children.getJSONObject(i); if (child.getString("bucket").equalsIgnoreCase(description)) { child.put("market_value", child.getDouble("market_value") + currentMarketValue); child.put("total_holdings_percent", child.getDouble("total_holdings_percent") + currentMarketValue * 100 / totalMarketValue); } } } rs.close(); stmt.close(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject parent = jsonArray.getJSONObject(i); JSONArray children = parent.getJSONArray("children"); for (int j = 0; j < children.length(); j++) { JSONObject child = children.getJSONObject(j); child.put("bond_holdings_percent", child.getDouble("market_value") * 100 / totalValueOfBonds); } } } catch (SQLException e) { e.printStackTrace(); } finally { DataAccess.cleanUp(conn); } return jsonArray; }
From source file:com.fingence.slayer.service.impl.MyResultServiceImpl.java
License:Open Source License
public List<String> getDistinctValues(String columnName, String portfolioIds) { List<String> distinctValues = new ArrayList<String>(); Connection conn = null;/*from w w w . ja v a 2s . c o m*/ try { conn = DataAccess.getConnection(); String[] tokens = { "[$COLUMN_NAME$]", "[$PORTFOLIO_IDS$]" }; String[] replacements = { columnName, portfolioIds }; String sql = StringUtil.replace(CustomSQLUtil.get(QUERY2), tokens, replacements); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { distinctValues.add(rs.getString(1)); } rs.close(); stmt.close(); } catch (SQLException e) { e.printStackTrace(); } finally { DataAccess.cleanUp(conn); } return distinctValues; }
From source file:com.liferay.announcements.web.internal.upgrade.v1_0_2.UpgradePermission.java
License:Open Source License
protected void addAnnouncementsAdminViewResourcePermission(long companyId, int scope, String primKey, long primKeyId, long roleId) throws Exception { String key = getKey(companyId, scope, primKey, roleId); if (_resourcePermissions.contains(key)) { return;//from ww w . j a va 2 s. c o m } _resourcePermissions.add(key); PreparedStatement ps = null; try { long resourcePermissionId = increment(ResourcePermission.class.getName()); long actionBitwiseValue = _BITWISE_VALUE_VIEW | _BITWISE_VALUE_ACCESS_IN_CONTROL_PANEL; String name = "com_liferay_announcements_web_portlet_" + "AnnouncementsAdminPortlet"; long ownerId = 0; StringBundler sb = new StringBundler(4); sb.append("insert into ResourcePermission (mvccVersion, "); sb.append("resourcePermissionId, companyId, name, scope, "); sb.append("primKey, primKeyId, roleId, ownerId, actionIds, "); sb.append("viewActionId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); String sql = sb.toString(); ps = connection.prepareStatement(sql); ps.setLong(1, 0); ps.setLong(2, resourcePermissionId); ps.setLong(3, companyId); ps.setString(4, name); ps.setInt(5, scope); ps.setString(6, primKey); ps.setLong(7, primKeyId); ps.setLong(8, roleId); ps.setLong(9, ownerId); ps.setLong(10, actionBitwiseValue); ps.setBoolean(11, true); ps.executeUpdate(); } catch (Exception e) { if (_log.isWarnEnabled()) { _log.warn("Unable to add resource permission", e); } } finally { DataAccess.cleanUp(ps); } }
From source file:com.liferay.announcements.web.internal.upgrade.v1_0_2.UpgradePermission.java
License:Open Source License
protected void addResourceAction(String actionId, long bitwiseValue) { PreparedStatement ps = null;/*from ww w .ja v a2s . co m*/ try { long resourceActionId = increment(ResourceAction.class.getName()); StringBundler sb = new StringBundler(3); sb.append("insert into ResourceAction (mvccVersion, "); sb.append("resourceActionId, name, actionId, bitwiseValue) "); sb.append("values (?, ?, ?, ?, ?)"); String sql = sb.toString(); ps = connection.prepareStatement(sql); ps.setLong(1, 0); ps.setLong(2, resourceActionId); ps.setString(3, "com_liferay_announcements_web_portlet_" + "AnnouncementsAdminPortlet"); ps.setString(4, actionId); ps.setLong(5, bitwiseValue); ps.executeUpdate(); } catch (Exception e) { if (_log.isWarnEnabled()) { _log.warn("Unable to add resource action", e); } } finally { DataAccess.cleanUp(ps); } }
From source file:com.liferay.calendar.hook.upgrade.v1_0_1.UpgradeCalendar.java
License:Open Source License
@Override protected void doUpgrade() throws Exception { Connection con = null;/*from w w w .ja v a 2 s. c o m*/ PreparedStatement ps = null; ResultSet rs = null; try { con = DataAccess.getUpgradeOptimizedConnection(); StringBundler sb = new StringBundler(6); sb.append("select Calendar.calendarId, CalendarResource."); sb.append("classNameId, User_.timeZoneId from Calendar "); sb.append("inner join CalendarResource on Calendar."); sb.append("calendarResourceId = CalendarResource."); sb.append("calendarResourceId inner join User_ on "); sb.append("CalendarResource.userId = User_.userId"); ps = con.prepareStatement(sb.toString()); rs = ps.executeQuery(); long userClassNameId = PortalUtil.getClassNameId(User.class); while (rs.next()) { long calendarId = rs.getLong(1); long classNameId = rs.getLong(2); String timeZoneId = null; if (classNameId == userClassNameId) { timeZoneId = rs.getString(3); } else { timeZoneId = PropsUtil.get(PropsKeys.COMPANY_DEFAULT_TIME_ZONE); } updateCalendarTimeZoneId(con, calendarId, timeZoneId); } } finally { DataAccess.cleanUp(con); } }