List of usage examples for com.liferay.portal.kernel.io.unsync UnsyncPrintWriter append
@Override
public PrintWriter append(CharSequence charSequence)
From source file:com.slemarchand.portal.scripting.sql.internal.SQLExecutor.java
License:Open Source License
@Override public Map<String, Object> eval(Set<String> allowedClasses, Map<String, Object> inputObjects, Set<String> outputNames, String script) throws ScriptingException { if (allowedClasses != null) { throw new ExecutionException("Constrained execution not supported for database queries"); }/*w w w . j a v a2s. c o m*/ UnsyncPrintWriter out = (UnsyncPrintWriter) inputObjects.get("out"); Map<String, String> hints = _getHints(script); boolean emptyScript = script.replace("--help", "").trim().length() == 0; boolean help = _getHint("help", hints, null) != null; if (help || emptyScript) { _displayHelp(out); } else { out.append( "To learn more about execution parameters, add \"--help\" line in your script or execute empty script\n\n"); } if (!emptyScript) { int maxRows = _getIntegerHint("maxRows", hints, 50); List<String> columnLabels = new LinkedList<String>(); List<List<Object>> rows = null; try { rows = _execQuery(script, maxRows, columnLabels); } catch (SQLException e) { throw new ScriptingException(e); } String format = _getHint("format", hints, "html"); if ("html".equalsIgnoreCase(format)) { _formatHTML(columnLabels, rows, out); } else if ("csv".equalsIgnoreCase(format)) { _formatCSV(columnLabels, rows, out); } else { throw new ScriptingException("Unknown value '" + format + "' for hint 'format'"); } } Map<String, Object> outputObjects; if (outputNames != null) { // Output objects not supported outputObjects = new HashMap<String, Object>(); } else { outputObjects = null; } return outputObjects; }
From source file:com.slemarchand.portal.scripting.sql.internal.SQLExecutor.java
License:Open Source License
private void _formatCSV(List<String> columnLabels, List<List<Object>> rows, UnsyncPrintWriter out) { List<List<?>> allLines = new LinkedList(); allLines.add(columnLabels);/*from w w w .ja v a 2 s . c o m*/ allLines.addAll(rows); for (List<?> line : allLines) { for (Iterator<?> iterator = line.iterator(); iterator.hasNext();) { Object value = (Object) iterator.next(); if (value != null) { out.append("\"" + value.toString().replaceAll("\"", "\"\"") + "\""); } else { out.append("\"null\""); } if (iterator.hasNext()) { out.append(','); } } out.append("\r\n"); } }
From source file:com.slemarchand.portal.scripting.sql.internal.SQLExecutor.java
License:Open Source License
private void _displayHelp(UnsyncPrintWriter out) { out.append( "Usage:\nTo set execution parameters, you can add some hint lines in your script, like these:\n"); out.append("\t--help\t\t\tDisplay this help\n"); out.append("\t--format=html\t\tFormat results in HTML (default format)\n"); out.append("\t--format=csv\t\tFormat results in CSV\n"); out.append("\t--maxRows=200\t\tLimit display to 200 first results (default limit is 50)\n"); out.append("You can combine several hints using multiple lines.\n\n"); out.append(//w w w.j av a2s .c om "Learn more about SQL Query Scripting Hook at <a href=\"https://github.com/slemarchand/portal-scripting-sql/wiki\" target=\"_blank\">https://github.com/slemarchand/portal-scripting-sql/wiki</a>.\n\n"); }
From source file:com.slemarchand.portal.scripting.sql.internal.SQLExecutor.java
License:Open Source License
private void _formatHTML(List<String> columnLabels, List<List<Object>> rows, UnsyncPrintWriter out) { out.append("</pre>"); out.append("<div class=\"component searchcontainer\"><div class=\"searchcontainer-content\">"); out.append("<table class=\"table table-bordered table-striped\">"); out.append("<thead class=\"table-columns\">"); out.append("<tr>"); for (String value : columnLabels) { out.append("<th>"); out.append(value);/* www . j a v a 2 s .co m*/ out.append("</th>"); } out.append("</tr>"); out.append("</thead>"); out.append("<tbody class=\"table-data\">"); boolean alt = false; for (List<?> line : rows) { out.append("<tr class=\"portlet-section-alternate results-row " + (alt ? "alt" : "") + "\">"); for (int i = 0; i < line.size(); i++) { Object value = (Object) line.get(i); out.append("<td class=\"table-cell"); if (i == 0) { out.append(" first"); } else if (i == line.size()) { out.append(" last"); } out.append("\">"); if (value != null) { out.append(value.toString()); } else { out.append("<span style=\"font-style:italic\">null</span>"); } out.append("</td>"); } out.append("</tr>"); alt = !alt; } out.append("</tbody>"); out.append("</table>"); out.append("</div></div>"); out.append("</pre>"); }
From source file:com.slemarchand.sqlqueryscripting.scripting.sqlquery.SQLQueryExecutor.java
License:Open Source License
public Map<String, Object> eval(Set<String> allowedClasses, Map<String, Object> inputObjects, Set<String> outputNames, String script, ClassLoader... classLoaders) throws ScriptingException { if (allowedClasses != null) { throw new ExecutionException("Constrained execution not supported for database queries"); }//from w w w .ja v a 2 s . c o m UnsyncPrintWriter out = (UnsyncPrintWriter) inputObjects.get("out"); Map<String, String> hints = _getHints(script); boolean emptyScript = script.replace("--help", "").trim().length() == 0; boolean help = _getHint("help", hints, null) != null; if (help || emptyScript) { _displayHelp(out); } else { out.append( "To learn more about execution parameters, add \"--help\" line in your script or execute empty script\n\n"); } if (!emptyScript) { int maxRows = _getIntegerHint("maxRows", hints, 50); List<String> columnLabels = new LinkedList<String>(); List<List<Object>> rows = null; try { rows = _execQuery(script, maxRows, columnLabels); } catch (SQLException e) { throw new ScriptingException(e); } String format = _getHint("format", hints, "html"); if ("html".equalsIgnoreCase(format)) { _formatHTML(columnLabels, rows, out); } else if ("csv".equalsIgnoreCase(format)) { _formatCSV(columnLabels, rows, out); } else { throw new ScriptingException("Unknown value '" + format + "' for hint 'format'"); } } Map<String, Object> outputObjects; if (outputNames != null) { // Output objects not supported outputObjects = new HashMap<String, Object>(); } else { outputObjects = null; } return outputObjects; }
From source file:com.slemarchand.sqlqueryscripting.scripting.sqlquery.SQLQueryExecutor.java
License:Open Source License
private void _displayHelp(UnsyncPrintWriter out) { out.append( "Usage:\nTo set execution parameters, you can add some hint lines in your script, like these:\n"); out.append("\t--help\t\t\tDisplay this help\n"); out.append("\t--format=html\t\tFormat results in HTML (default format)\n"); out.append("\t--format=csv\t\tFormat results in CSV\n"); out.append("\t--maxRows=200\t\tLimit display to 200 first results (default limit is 50)\n"); out.append("You can combine several hints using multiple lines.\n\n"); out.append(//ww w . j av a2 s .c om "Learn more about SQL Query Scripting Hook at <a href=\"https://github.com/slemarchand/sql-query-scripting-hook/wiki\" target=\"_blank\">https://github.com/slemarchand/sql-query-scripting-hook/wiki</a>.\n\n"); }