List of usage examples for org.apache.commons.lang BooleanUtils toBooleanObject
public static Boolean toBooleanObject(String str)
Converts a String to a Boolean.
'true'
, 'on'
or 'yes'
(case insensitive) will return true
.
From source file:org.apache.cocoon.util.JDBCTypeConversions.java
/** * Get the Statement column so that the results are mapped correctly. * (this has been copied from AbstractDatabaseAction and modified slightly) */// w w w . j av a 2 s . c om public static Object getColumn(ResultSet set, Configuration column) throws Exception { Integer type = (Integer) JDBCTypeConversions.typeConstants.get(column.getAttribute("type")); String dbcol = column.getAttribute("name"); Object value = null; switch (type.intValue()) { case Types.CLOB: case Types.CHAR: Clob dbClob = set.getClob(dbcol); int length = (int) dbClob.length(); InputStream asciiStream = new BufferedInputStream(dbClob.getAsciiStream()); byte[] buffer = new byte[length]; asciiStream.read(buffer); String str = new String(buffer); asciiStream.close(); value = str; break; case Types.BIGINT: value = set.getBigDecimal(dbcol); break; case Types.TINYINT: value = new Byte(set.getByte(dbcol)); break; case Types.VARCHAR: value = set.getString(dbcol); break; case Types.DATE: value = set.getDate(dbcol); break; case Types.DOUBLE: value = new Double(set.getDouble(dbcol)); break; case Types.FLOAT: value = new Float(set.getFloat(dbcol)); break; case Types.INTEGER: value = new Integer(set.getInt(dbcol)); break; case Types.NUMERIC: value = new Long(set.getLong(dbcol)); break; case Types.SMALLINT: value = new Short(set.getShort(dbcol)); break; case Types.TIME: value = set.getTime(dbcol); break; case Types.TIMESTAMP: value = set.getTimestamp(dbcol); break; case Types.ARRAY: value = set.getArray(dbcol); // new Integer(set.getInt(dbcol)); break; case Types.BIT: value = BooleanUtils.toBooleanObject(set.getBoolean(dbcol)); break; case Types.STRUCT: value = (Struct) set.getObject(dbcol); break; case Types.OTHER: value = set.getObject(dbcol); break; default: // The blob types have to be requested separately, via a Reader. value = ""; break; } return value; }
From source file:org.apache.cocoon.util.JDBCTypeConversions.java
/** * Set the Statement column so that the results are mapped correctly. * * @param statement the prepared statement * @param position the position of the column * @param value the value of the column/*from w w w .j a v a 2s .co m*/ */ public static void setColumn(PreparedStatement statement, int position, Object value, Integer typeObject) throws Exception { if (value instanceof String) { value = ((String) value).trim(); } if (typeObject == null) { throw new SQLException("Can't set column because the type is unrecognized"); } if (value == null) { /** If the value is null, set the column value null and return **/ statement.setNull(position, typeObject.intValue()); return; } if ("".equals(value)) { switch (typeObject.intValue()) { case Types.CHAR: case Types.CLOB: case Types.VARCHAR: /** If the value is an empty string and the column is a string type, we can continue **/ break; default: /** If the value is an empty string and the column is something else, we treat it as a null value **/ statement.setNull(position, typeObject.intValue()); return; } } File file = null; int length = -1; InputStream asciiStream = null; //System.out.println("========================================================================"); //System.out.println("JDBCTypeConversions: setting type "+typeObject.intValue()); switch (typeObject.intValue()) { case Types.CLOB: //System.out.println("CLOB"); Clob clob = null; if (value instanceof Clob) { clob = (Clob) value; } else if (value instanceof File) { File asciiFile = (File) value; asciiStream = new BufferedInputStream(new FileInputStream(asciiFile)); length = (int) asciiFile.length(); clob = new ClobHelper(asciiStream, length); } else if (value instanceof Part) { Part anyFile = (Part) value; asciiStream = new BufferedInputStream(anyFile.getInputStream()); length = anyFile.getSize(); clob = new ClobHelper(asciiStream, length); } else if (value instanceof JDBCxlobHelper) { asciiStream = ((JDBCxlobHelper) value).inputStream; length = ((JDBCxlobHelper) value).length; clob = new ClobHelper(asciiStream, length); } else if (value instanceof Source) { asciiStream = ((Source) value).getInputStream(); length = (int) ((Source) value).getContentLength(); clob = new ClobHelper(asciiStream, length); } else { String asciiText = value.toString(); asciiStream = new ByteArrayInputStream(asciiText.getBytes()); length = asciiText.length(); clob = new ClobHelper(asciiStream, length); } statement.setClob(position, clob); break; case Types.CHAR: // simple large object, e.g. Informix's TEXT //System.out.println("CHAR"); if (value instanceof File) { File asciiFile = (File) value; asciiStream = new BufferedInputStream(new FileInputStream(asciiFile)); length = (int) asciiFile.length(); } else if (value instanceof JDBCxlobHelper) { asciiStream = ((JDBCxlobHelper) value).inputStream; length = ((JDBCxlobHelper) value).length; } else if (value instanceof Source) { asciiStream = ((Source) value).getInputStream(); length = (int) ((Source) value).getContentLength(); } else if (value instanceof Part) { Part anyFile = (Part) value; asciiStream = new BufferedInputStream(anyFile.getInputStream()); length = anyFile.getSize(); clob = new ClobHelper(asciiStream, length); } else { String asciiText = value.toString(); asciiStream = new BufferedInputStream(new ByteArrayInputStream(asciiText.getBytes())); length = asciiText.length(); } statement.setAsciiStream(position, asciiStream, length); break; case Types.BIGINT: //System.out.println("BIGINT"); BigDecimal bd = null; if (value instanceof BigDecimal) { bd = (BigDecimal) value; } else if (value instanceof Number) { bd = BigDecimal.valueOf(((Number) value).longValue()); } else { bd = new BigDecimal(value.toString()); } statement.setBigDecimal(position, bd); break; case Types.TINYINT: //System.out.println("TINYINT"); Byte b = null; if (value instanceof Byte) { b = (Byte) value; } else if (value instanceof Number) { b = new Byte(((Number) value).byteValue()); } else { b = new Byte(value.toString()); } statement.setByte(position, b.byteValue()); break; case Types.DATE: //System.out.println("DATE"); Date d = null; if (value instanceof Date) { d = (Date) value; } else if (value instanceof java.util.Date) { d = new Date(((java.util.Date) value).getTime()); } else if (value instanceof Calendar) { d = new Date(((Calendar) value).getTime().getTime()); } else { d = Date.valueOf(value.toString()); } statement.setDate(position, d); break; case Types.DOUBLE: //System.out.println("DOUBLE"); double db; if (value instanceof Number) { db = (((Number) value).doubleValue()); } else { db = Double.parseDouble(value.toString()); } statement.setDouble(position, db); break; case Types.FLOAT: //System.out.println("FLOAT"); float f; if (value instanceof Number) { f = (((Number) value).floatValue()); } else { f = Float.parseFloat(value.toString()); } statement.setFloat(position, f); break; case Types.NUMERIC: //System.out.println("NUMERIC"); long l; if (value instanceof Number) { l = (((Number) value).longValue()); } else { l = Long.parseLong(value.toString()); } statement.setLong(position, l); break; case Types.SMALLINT: //System.out.println("SMALLINT"); Short s = null; if (value instanceof Short) { s = (Short) value; } else if (value instanceof Number) { s = new Short(((Number) value).shortValue()); } else { s = new Short(value.toString()); } statement.setShort(position, s.shortValue()); break; case Types.TIME: //System.out.println("TIME"); Time t = null; if (value instanceof Time) { t = (Time) value; } else if (value instanceof java.util.Date) { t = new Time(((java.util.Date) value).getTime()); } else { t = Time.valueOf(value.toString()); } statement.setTime(position, t); break; case Types.TIMESTAMP: //System.out.println("TIMESTAMP"); Timestamp ts = null; if (value instanceof Time) { ts = (Timestamp) value; } else if (value instanceof java.util.Date) { ts = new Timestamp(((java.util.Date) value).getTime()); } else { ts = Timestamp.valueOf(value.toString()); } statement.setTimestamp(position, ts); break; case Types.ARRAY: //System.out.println("ARRAY"); statement.setArray(position, (Array) value); // no way to convert string to array break; case Types.STRUCT: //System.out.println("STRUCT"); case Types.OTHER: //System.out.println("OTHER"); statement.setObject(position, value); break; case Types.LONGVARBINARY: //System.out.println("LONGVARBINARY"); statement.setTimestamp(position, new Timestamp((new java.util.Date()).getTime())); break; case Types.VARCHAR: //System.out.println("VARCHAR"); statement.setString(position, value.toString()); break; case Types.BLOB: //System.out.println("BLOB"); if (value instanceof JDBCxlobHelper) { statement.setBinaryStream(position, ((JDBCxlobHelper) value).inputStream, ((JDBCxlobHelper) value).length); } else if (value instanceof Source) { statement.setBinaryStream(position, ((Source) value).getInputStream(), (int) ((Source) value).getContentLength()); } else { Blob blob = null; if (value instanceof Blob) { blob = (Blob) value; } else if (value instanceof File) { file = (File) value; blob = new BlobHelper(new FileInputStream(file), (int) file.length()); } else if (value instanceof String) { file = new File((String) value); blob = new BlobHelper(new FileInputStream(file), (int) file.length()); } else if (value instanceof Part) { Part anyFile = (Part) value; blob = new BlobHelper(new BufferedInputStream(anyFile.getInputStream()), anyFile.getSize()); } else { throw new SQLException("Invalid type for blob: " + value.getClass().getName()); } //InputStream input = new BufferedInputStream(new FileInputStream(file)); statement.setBlob(position, blob); } break; case Types.VARBINARY: //System.out.println("VARBINARY"); if (value instanceof JDBCxlobHelper) { statement.setBinaryStream(position, ((JDBCxlobHelper) value).inputStream, ((JDBCxlobHelper) value).length); } else if (value instanceof Source) { statement.setBinaryStream(position, ((Source) value).getInputStream(), (int) ((Source) value).getContentLength()); } else if (value instanceof Part) { statement.setBinaryStream(position, ((Part) value).getInputStream(), ((Part) value).getSize()); } else { if (value instanceof File) { file = (File) value; } else if (value instanceof String) { file = new File((String) value); } else { throw new SQLException("Invalid type for blob: " + value.getClass().getName()); } //InputStream input = new BufferedInputStream(new FileInputStream(file)); FileInputStream input = new FileInputStream(file); statement.setBinaryStream(position, input, (int) file.length()); } break; case Types.INTEGER: //System.out.println("INTEGER"); Integer i = null; if (value instanceof Integer) { i = (Integer) value; } else if (value instanceof Number) { i = new Integer(((Number) value).intValue()); } else { i = new Integer(value.toString()); } statement.setInt(position, i.intValue()); break; case Types.BIT: //System.out.println("BIT"); Boolean bo = null; if (value instanceof Boolean) { bo = (Boolean) value; } else if (value instanceof Number) { bo = BooleanUtils.toBooleanObject(((Number) value).intValue() == 1); } else { bo = BooleanUtils.toBooleanObject(value.toString()); } statement.setBoolean(position, bo.booleanValue()); break; default: //System.out.println("default"); throw new SQLException("Impossible exception - invalid type "); } //System.out.println("========================================================================"); }
From source file:org.apache.hive.beeline.SeparatedValuesOutputFormat.java
/** * Default is disabling the double quoting for separated value */// w w w . jav a2 s . c o m private boolean isQuotingDisabled() { Boolean quotingDisabled = Boolean.TRUE; String quotingDisabledStr = System.getProperty(SeparatedValuesOutputFormat.DISABLE_QUOTING_FOR_SV); if (StringUtils.isNotBlank(quotingDisabledStr)) { quotingDisabled = BooleanUtils.toBooleanObject(quotingDisabledStr); if (quotingDisabled == null) { beeLine.error("System Property " + SeparatedValuesOutputFormat.DISABLE_QUOTING_FOR_SV + " is now " + quotingDisabledStr + " which only accepts boolean values"); quotingDisabled = Boolean.TRUE; } } return quotingDisabled; }
From source file:org.apache.ojb.broker.platforms.BlobWrapper.java
public static BlobWrapper createTemporary(Connection conn, boolean b, int i) throws Exception { BlobWrapper retval = new BlobWrapper(); // Passing null to invoke static method retval.setBlob(createTemporary.invoke(null, new Object[] { conn, BooleanUtils.toBooleanObject(b), new Integer(i) })); return retval; }
From source file:org.apache.ojb.broker.platforms.ClobWrapper.java
public static ClobWrapper createTemporary(Connection conn, boolean b, int i) throws Exception { ClobWrapper retval = new ClobWrapper(); // Passing null to invoke static method retval.setClob(createTemporary.invoke(null, new Object[] { conn, BooleanUtils.toBooleanObject(b), new Integer(i) })); return retval; }
From source file:org.apache.solr.handler.component.RuleManagerComponent.java
/** * Search for matching rules. Any found rules are added to a map to process them later. * @param requestParams Incoming search params. * @param pageType Current page type.//from www.j av a 2 s . c o m * @return Map of rules where the key is the rule type. * @throws IOException If there are issues getting the rules core. */ private Map<RuleType, List<Document>> searchRules(SolrParams requestParams, PageType pageType) throws IOException { Map<RuleType, List<Document>> rulesMap = new HashMap<RuleType, List<Document>>(); //Prepare query to rules index String qParam = requestParams.get(CommonParams.Q); SolrQuery ruleParams = getRulesQuery(requestParams, pageType); if (ruleParams == null) { //Do nothing, this request is not supported by the RuleManager component return rulesMap; } SolrCore rulesCore = coreContainer.getCore(rulesCoreName); SearchHandler searchHandler = getSearchHandler(rulesCore); RefCounted<SolrIndexSearcher> rulesSearcher = rulesCore.getSearcher(); int ruleCounter = 0, matchedRules = 0; try { do { ruleParams.set(CommonParams.START, ruleCounter); logger.debug("Searching rules core - rules processed so far " + ruleCounter); SolrQueryResponse response = new SolrQueryResponse(); rulesCore.execute(searchHandler, new LocalSolrQueryRequest(rulesCore, ruleParams), response); ResultContext result = (ResultContext) response.getValues().get("response"); if (result != null) { DocList rules = result.docs; DocIterator ruleIterator = rules.iterator(); matchedRules = rules.matches(); //Process rules while (ruleIterator.hasNext()) { ruleCounter++; Document ruleDoc = rulesSearcher.get().doc(ruleIterator.nextDoc()); String ruleId = ruleDoc.getField("id").stringValue(); String excludeRuleParam = requestParams.get("excludeRules"); if (excludeRuleParam != null) { List<String> excludeRules = Arrays.asList(excludeRuleParam.split(",")); if (excludeRules.contains(ruleId)) { continue; } } IndexableField experimentalField = ruleDoc.getField(RuleConstants.FIELD_EXPERIMENTAL); if (experimentalField != null) { Boolean experimental = (BooleanUtils.toBooleanObject(experimentalField.stringValue())); String includeRuleParam = requestParams.get("includeRules"); if (experimental && includeRuleParam != null) { List<String> includeRules = Arrays.asList(includeRuleParam.split(",")); if (!includeRules.contains(ruleId)) { continue; } } } if (PageType.search == pageType && !filterExactMatch(qParam, ruleDoc)) { // Skip this rule as an exact match was required, but got only a partial match. continue; } RuleType ruleType = RuleType.valueOf(ruleDoc.get(RuleConstants.FIELD_RULE_TYPE)); addRuleToMap(rulesMap, ruleType, ruleDoc); } } else { logger.error("An error occurred when searching for matching rules. Processed rules so far: " + ruleCounter, response.getException()); break; } } while (ruleCounter < matchedRules); logger.debug("Rules found: " + matchedRules); } finally { rulesSearcher.decref(); } return rulesMap; }
From source file:org.apache.storm.daemon.logviewer.webapp.LogviewerResource.java
/** * Handles '/deepSearch' request.// w ww . j a va 2 s. co m */ @GET @Path("/deepSearch/{topoId}") public Response deepSearch(@PathParam("topoId") String topologyId, @Context HttpServletRequest request) throws IOException { String user = httpCredsHandler.getUserName(request); String searchString = request.getParameter("search-string"); String numMatchesStr = request.getParameter("num-matches"); String portStr = request.getParameter("port"); String startFileOffset = request.getParameter("start-file-offset"); String startByteOffset = request.getParameter("start-byte-offset"); String searchArchived = request.getParameter("search-archived"); String callback = request.getParameter("callback"); String origin = request.getHeader("Origin"); return logSearchHandler.deepSearchLogsForTopology(topologyId, user, searchString, numMatchesStr, portStr, startFileOffset, startByteOffset, BooleanUtils.toBooleanObject(searchArchived), callback, origin); }
From source file:org.displaytag.decorator.Decorator.java
/** * Check if a getter exists for a given property. Uses cached info if property has already been requested. This * method only check for a simple property, if pPropertyName contains multiple tokens only the first part is * evaluated/*from w w w .ja va2s . c o m*/ * @param propertyName name of the property to check * @return boolean true if the decorator has a getter for the given property */ public boolean hasGetterFor(String propertyName) { String simpleProperty = propertyName; // get the simple (not nested) bean property int indexOfDot = simpleProperty.indexOf('.'); if (indexOfDot > 0) { simpleProperty = simpleProperty.substring(0, indexOfDot); } Boolean cachedResult = (Boolean) propertyMap .get(getClass().getName() + CLASS_PROPERTY_SEPARATOR + simpleProperty); if (cachedResult != null) { return cachedResult.booleanValue(); } // not already cached... check boolean hasGetter = searchGetterFor(propertyName); // save in cache propertyMap.put(getClass().getName() + CLASS_PROPERTY_SEPARATOR + simpleProperty, BooleanUtils.toBooleanObject(hasGetter)); // and return return hasGetter; }
From source file:org.dspace.app.rest.submit.factory.impl.LicenseAddPatchOperation.java
@Override void add(Context context, Request currentRequest, WorkspaceItem source, String path, Object value) throws Exception { Boolean grant = BooleanUtils.toBooleanObject((String) value); if (grant == null) { throw new IllegalArgumentException( "Value is not a valid boolean expression (permitted value: on/off, true/false and yes/no"); }//from w ww. ja va2 s. com Item item = source.getItem(); EPerson submitter = context.getCurrentUser(); // remove any existing DSpace license (just in case the user // accepted it previously) itemService.removeDSpaceLicense(context, item); if (grant) { String license = LicenseUtils.getLicenseText(context.getCurrentLocale(), source.getCollection(), item, submitter); LicenseUtils.grantLicense(context, item, license, null); } }
From source file:org.dspace.app.rest.submit.factory.impl.LicenseReplacePatchOperation.java
@Override void replace(Context context, Request currentRequest, WorkspaceItem source, String path, Object value) throws Exception { Boolean grant = BooleanUtils.toBooleanObject((String) value); if (grant == null) { throw new IllegalArgumentException( "Value is not a valid boolean expression (permitted value: on/off, true/false and yes/no"); }// ww w. j a v a2 s.com Item item = source.getItem(); EPerson submitter = context.getCurrentUser(); // remove any existing DSpace license (just in case the user // accepted it previously) itemService.removeDSpaceLicense(context, item); if (grant) { String license = LicenseUtils.getLicenseText(context.getCurrentLocale(), source.getCollection(), item, submitter); LicenseUtils.grantLicense(context, item, license, null); } }