List of usage examples for java.lang Boolean Boolean
@Deprecated(since = "9") public Boolean(String s)
From source file:net.sourceforge.fenixedu.presentationTier.Action.person.PartyContactsManagementDispatchAction.java
public ActionForward postbackSetElements(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) { PartyContactBean contact = getRenderedObject("edit-contact"); RenderUtils.invalidateViewState();/*from w ww . j a v a 2s. c o m*/ if (contact.getVisibleToPublic().booleanValue()) { contact.setVisibleToPublic(new Boolean( contact.getVisibleToStudents().booleanValue() && contact.getVisibleToTeachers().booleanValue() && contact.getVisibleToEmployees().booleanValue() && contact.getVisibleToAlumni().booleanValue())); } contact.setVisibleToManagement(Boolean.TRUE); request.setAttribute("partyContact", contact); request.setAttribute("partyContactClass", contact.getContactName()); return backToEditOrCreate(mapping, actionForm, request, response); }
From source file:in.hatimi.nosh.support.CmdLineManager.java
private boolean injectString(Object target, Field field, String value) { if (field.getType().equals(Boolean.class) || field.getType().equals(Boolean.TYPE)) { Boolean vobj = new Boolean(value); return injectImpl(target, field, vobj); }// w ww . ja v a 2 s . co m if (field.getType().equals(Double.class) || field.getType().equals(Double.TYPE)) { Double vobj = Double.valueOf(value); return injectImpl(target, field, vobj); } if (field.getType().equals(Float.class) || field.getType().equals(Float.TYPE)) { Float vobj = Float.valueOf(value); return injectImpl(target, field, vobj); } if (field.getType().equals(Long.class) || field.getType().equals(Long.TYPE)) { Long vobj = Long.valueOf(value); return injectImpl(target, field, vobj); } if (field.getType().equals(Integer.class) || field.getType().equals(Integer.TYPE)) { Integer vobj = Integer.valueOf(value); return injectImpl(target, field, vobj); } if (field.getType().equals(String.class)) { return injectImpl(target, field, value); } if (field.getType().equals(byte[].class)) { return injectImpl(target, field, value.getBytes()); } if (field.getType().equals(char[].class)) { return injectImpl(target, field, value.toCharArray()); } return false; }
From source file:in.mycp.remote.EucalyptusService.java
/** * setup the following in the order listed 1.user - admin@mycp.com/password * 2.Region - Bangalore 3.Infra - Euca details 4.example product catalog * produced out of the above infra 5.asset type * //from w w w .j ava2 s. c om */ @RemoteMethod public void setUpDataForEuca() { //TODO Infra infra = null; try { logger.info("start setUpDataForEuca "); User user = null; try { user = User.findUsersByEmailEquals("admin@mycp.com").getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (user == null) { user = new User(); user.setActive(new Boolean(true)); user.setEmail("admin@mycp.com"); user.setPassword("password"); user.setRegistereddate(new Date()); user = user.merge(); } logger.info("user setup"); Company company = null; try { company = Company.findCompanysByNameEquals("MyCP IDC").getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (company == null) { company = new Company(); company.setAddress("Up St"); company.setCity("Bangalore"); company.setCountry("India"); company.setEmail("me@mycpidc.com"); company.setName("MyCP IDC"); company.setPhone("474747"); company = company.merge(); } RegionP regionP = null; try { regionP = RegionP.findRegionPsByNameEquals("Bangalore").getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (regionP == null) { regionP = new RegionP(); regionP.setName("Bangalore"); //regionP.setCity("Bangalore"); //regionP.setCountry("India"); //regionP.setAddress("007, Gothilla Layout"); regionP.setUrl("url"); regionP.setCompany(company); regionP = regionP.merge(); } logger.info("region setp"); try { infra = Infra.findInfrasByNameEquals("Eucalyptus Bang").getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (infra == null) { infra = new Infra(); infra.setServer("xx.xx.xx.xx"); infra.setPort(8773); infra.setAccessId("xxx"); infra.setSecretKey("xxx"); infra.setIsSecure(false); infra.setResourcePrefix("/services/Eucalyptus"); infra.setSignatureVersion(1); //infra.setRegion(regionP); infra.setCompany(company); infra.setName("Eucalyptus Bang"); infra.setDetails("Eucalyptus 2.x setup in Bangalore"); infra.setImportDate(new Date()); infra.setSyncInProgress(true); infra.setSyncstatus(Commons.sync_status.running.ordinal()); infra = infra.merge(); } logger.info("infra setp"); ProductCatalog productCatalog = null; Set<ProductCatalog> productCatalogs = new HashSet<ProductCatalog>(); try { productCatalog = ProductCatalog.findProductCatalogsByNameEquals("Eucalyptus Bang IpAddress") .getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (productCatalog == null) { productCatalog = new ProductCatalog(); productCatalog.setCurrency("INR"); productCatalog.setDetails("Eucalyptus Bang IpAddress"); productCatalog.setName("Eucalyptus Bang IpAddress"); productCatalog.setPrice(12); productCatalog.setInfra(infra); productCatalog = productCatalog.merge(); productCatalogs.add(productCatalog); } logger.info("product setp " + productCatalog.getName()); // reset the local variable productCatalog = null; try { productCatalog = ProductCatalog.findProductCatalogsByNameEquals("Eucalyptus Bang SecurityGroup") .getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (productCatalog == null) { productCatalog = new ProductCatalog(); productCatalog.setCurrency("INR"); productCatalog.setDetails("Eucalyptus Bang SecurityGroup"); productCatalog.setName("Eucalyptus Bang SecurityGroup"); productCatalog.setPrice(12); productCatalog.setInfra(infra); productCatalog = productCatalog.merge(); productCatalogs.add(productCatalog); } logger.info("product setp " + productCatalog.getName()); // reset the local variable productCatalog = null; try { productCatalog = ProductCatalog.findProductCatalogsByNameEquals("Eucalyptus Bang IpPermission") .getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (productCatalog == null) { productCatalog = new ProductCatalog(); productCatalog.setCurrency("INR"); productCatalog.setDetails("Eucalyptus Bang IpPermission"); productCatalog.setName("Eucalyptus Bang IpPermission"); productCatalog.setPrice(12); productCatalog.setInfra(infra); productCatalog = productCatalog.merge(); productCatalogs.add(productCatalog); } logger.info("product setp " + productCatalog.getName()); // reset the local variable productCatalog = null; try { productCatalog = ProductCatalog.findProductCatalogsByNameEquals("Eucalyptus Bang Volume") .getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (productCatalog == null) { productCatalog = new ProductCatalog(); productCatalog.setCurrency("INR"); productCatalog.setDetails("Eucalyptus Bang Volume"); productCatalog.setName("Eucalyptus Bang Volume"); productCatalog.setPrice(12); productCatalog.setInfra(infra); productCatalog = productCatalog.merge(); productCatalogs.add(productCatalog); } logger.info("product setp " + productCatalog.getName()); // reset the local variable productCatalog = null; try { productCatalog = ProductCatalog.findProductCatalogsByNameEquals("Eucalyptus Bang VolumeSnapshot") .getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (productCatalog == null) { productCatalog = new ProductCatalog(); productCatalog.setCurrency("INR"); productCatalog.setDetails("Eucalyptus Bang VolumeSnapshot"); productCatalog.setName("Eucalyptus Bang VolumeSnapshot"); productCatalog.setPrice(12); productCatalog.setInfra(infra); productCatalog = productCatalog.merge(); productCatalogs.add(productCatalog); } logger.info("product setp " + productCatalog.getName()); // reset the local variable productCatalog = null; try { productCatalog = ProductCatalog.findProductCatalogsByNameEquals("Eucalyptus Bang ComputeImage") .getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (productCatalog == null) { productCatalog = new ProductCatalog(); productCatalog.setCurrency("INR"); productCatalog.setDetails("Eucalyptus Bang ComputeImage"); productCatalog.setName("Eucalyptus Bang ComputeImage"); productCatalog.setPrice(12); productCatalog.setInfra(infra); productCatalog = productCatalog.merge(); productCatalogs.add(productCatalog); } logger.info("product setp " + productCatalog.getName()); // reset the local variable productCatalog = null; try { productCatalog = ProductCatalog .findProductCatalogsByNameEquals("Eucalyptus Bang ComputeReservation").getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (productCatalog == null) { productCatalog = new ProductCatalog(); productCatalog.setCurrency("INR"); productCatalog.setDetails("Eucalyptus Bang ComputeReservation"); productCatalog.setName("Eucalyptus Bang ComputeReservation"); productCatalog.setPrice(12); productCatalog.setInfra(infra); productCatalog = productCatalog.merge(); productCatalogs.add(productCatalog); } logger.info("product setp " + productCatalog.getName()); // reset the local variable productCatalog = null; try { productCatalog = ProductCatalog.findProductCatalogsByNameEquals("Eucalyptus Bang ComputeInstance") .getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (productCatalog == null) { productCatalog = new ProductCatalog(); productCatalog.setCurrency("INR"); productCatalog.setDetails("Eucalyptus Bang ComputeInstance"); productCatalog.setName("Eucalyptus Bang ComputeInstance"); productCatalog.setPrice(12); productCatalog.setInfra(infra); productCatalog = productCatalog.merge(); productCatalogs.add(productCatalog); } logger.info("product setp " + productCatalog.getName()); // reset the local variable productCatalog = null; try { productCatalog = ProductCatalog.findProductCatalogsByNameEquals("Eucalyptus Bang KeyPair") .getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (productCatalog == null) { productCatalog = new ProductCatalog(); productCatalog.setCurrency("INR"); productCatalog.setDetails("Eucalyptus Bang KeyPair"); productCatalog.setName("Eucalyptus Bang KeyPair"); productCatalog.setPrice(12); productCatalog.setInfra(infra); productCatalog = productCatalog.merge(); productCatalogs.add(productCatalog); } logger.info("product setp " + productCatalog.getName()); infra.setProductCatalogs(productCatalogs); infra = infra.merge(); AssetType assetType = null; try { assetType = AssetType.findAssetTypesByNameEquals("IpAddress").getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (assetType == null) { assetType = new AssetType(); assetType.setName("IpAddress"); assetType.setDescription("IpAddress"); assetType.merge(); } logger.info("assetType setp " + assetType.getName()); assetType = null; try { assetType = AssetType.findAssetTypesByNameEquals("SecurityGroup").getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (assetType == null) { assetType = new AssetType(); assetType.setName("SecurityGroup"); assetType.setDescription("SecurityGroup"); assetType.merge(); } logger.info("assetType setp " + assetType.getName()); assetType = null; try { assetType = AssetType.findAssetTypesByNameEquals("IpPermission").getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (assetType == null) { assetType = new AssetType(); assetType.setName("IpPermission"); assetType.setDescription("IpPermission"); assetType.merge(); } logger.info("assetType setp " + assetType.getName()); assetType = null; try { assetType = AssetType.findAssetTypesByNameEquals("Volume").getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (assetType == null) { assetType = new AssetType(); assetType.setName("Volume"); assetType.setDescription("Volume"); assetType.merge(); } logger.info("assetType setp " + assetType.getName()); assetType = null; try { assetType = AssetType.findAssetTypesByNameEquals("VolumeSnapshot").getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (assetType == null) { assetType = new AssetType(); assetType.setName("VolumeSnapshot"); assetType.setDescription("VolumeSnapshot"); assetType.merge(); } logger.info("assetType setp " + assetType.getName()); /* * insert into asset_type(name,description) values('IpAddress',''); * insert into asset_type(name,description) * values('SecurityGroup',''); insert into * asset_type(name,description) values('IpPermission',''); insert * into asset_type(name,description) values('Volume',''); insert * into asset_type(name,description) values('VolumeSnapshot',''); * insert into asset_type(name,description) * values('ComputeImage',''); insert into * asset_type(name,description) values('ComputeReservation',''); * insert into asset_type(name,description) * values('ComputeInstance',''); insert into * asset_type(name,description) values('KeyPair',''); */ assetType = null; try { assetType = AssetType.findAssetTypesByNameEquals("ComputeImage").getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (assetType == null) { assetType = new AssetType(); assetType.setName("ComputeImage"); assetType.setDescription("ComputeImage"); assetType.merge(); } logger.info("assetType setp " + assetType.getName()); assetType = null; try { assetType = AssetType.findAssetTypesByNameEquals("ComputeReservation").getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (assetType == null) { assetType = new AssetType(); assetType.setName("ComputeReservation"); assetType.setDescription("ComputeReservation"); assetType.merge(); } logger.info("assetType setp " + assetType.getName()); assetType = null; try { assetType = AssetType.findAssetTypesByNameEquals("ComputeInstance").getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (assetType == null) { assetType = new AssetType(); assetType.setName("ComputeInstance"); assetType.setDescription("ComputeInstance"); assetType.merge(); } logger.info("assetType setp " + assetType.getName()); assetType = null; try { assetType = AssetType.findAssetTypesByNameEquals("KeyPair").getSingleResult(); } catch (Exception e) { logger.error(e);//e.printStackTrace(); } if (assetType == null) { assetType = new AssetType(); assetType.setName("KeyPair"); assetType.setDescription("KeyPair"); assetType.merge(); } logger.info("assetType setp " + assetType.getName()); logger.info("end setUpDataForEuca"); } catch (Exception e) { logger.info(" error while setUpDataForEuca "); logger.error(e);//e.printStackTrace(); } finally { logger.info(" setting back sync setSyncInProgress(false)"); infra.setSyncInProgress(false); infra.setSyncstatus(Commons.sync_status.success.ordinal()); infra = infra.merge(); } }
From source file:es.caib.seycon.ng.servei.XarxaServiceImpl.java
protected Boolean handleEsXarxaAdministrada(String codiXarxa) throws Exception { Usuari usuari = getUsuariService().getCurrentUsuari(); if (usuari == null) return false; Collection codisXarxa = getCodiXarxesAmbAccesAdministracio(usuari.getCodi()); for (Iterator iterator = codisXarxa.iterator(); iterator.hasNext();) { String currentCodiXarxa = (String) iterator.next(); if (currentCodiXarxa.compareTo(codiXarxa) == 0) { return new Boolean(true); }/*from w ww . j av a 2 s. co m*/ } return new Boolean(false); }
From source file:org.iwethey.forums.web.post.PostCreation.java
/** * Check to see if there are form errors. If so, show the form again, otherwise * process the submission.//ww w .j a va 2 s.c o m * <p> * @param request The servlet request object. * @param response The servlet response object. * @param command The form backing store object (a User object). * @param errors The Spring errors object. */ protected ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { boolean preview = RequestUtils.getBooleanParameter(request, "preview", false); String submitBtnVal = RequestUtils.getStringParameter(request, "SUBMIT"); boolean ignorePreview = "Save without Preview".equals(submitBtnVal); if (ignorePreview) { preview = false; } if (errors.hasErrors() || preview) { ModelAndView mv = showForm(request, response, errors); mv.addObject("preview", new Boolean(preview)); return mv; } else { return onSubmit(request, response, command, errors); } }
From source file:org.centralperf.service.CSVResultService.java
/** * Build a sample based on a CSV line and information about the headers names and orders * @param headerInfo Information about headers * @param CSVline Array of string// ww w . j av a 2 s. co m * @return a sample */ public Sample buildSampleFromCSVLine(CSVHeaderInfo headerInfo, String[] CSVline) { if (isHeaderLine(CSVline)) { return null; } Sample sample = new Sample(); try { // Try first to get a timestamp sample.setTimestamp( new Date(new Long(headerInfo.getValue(CSVHeaderInfo.CSV_HEADER_TIMESTAMP, CSVline)))); } catch (NumberFormatException e) { // Try to parse this format : 2012/10/30 12:47:47 SimpleDateFormat parserSDF = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); try { sample.setTimestamp( parserSDF.parse(headerInfo.getValue(CSVHeaderInfo.CSV_HEADER_TIMESTAMP, CSVline))); } catch (ParseException e1) { log.error("Error on CSV parsing:" + e.getMessage(), e1); } } try { sample.setElapsed(new Long(headerInfo.getValue(CSVHeaderInfo.CSV_HEADER_ELAPSED, CSVline))); } catch (NumberFormatException e) { } sample.setSampleName(headerInfo.getValue(CSVHeaderInfo.CSV_HEADER_SAMPLENAME, CSVline)); String status = headerInfo.getValue(CSVHeaderInfo.CSV_HEADER_STATUS, CSVline); if (status != null) { sample.setStatus(status); } String assertResult = headerInfo.getValue(CSVHeaderInfo.CSV_HEADER_ASSERTRESULT, CSVline); if (assertResult != null) { sample.setAssertResult( new Boolean(headerInfo.getValue(CSVHeaderInfo.CSV_HEADER_ASSERTRESULT, CSVline))); } String sizeString = headerInfo.getValue(CSVHeaderInfo.CSV_HEADER_SIZEINBYTES, CSVline); // TODO : manage format errors if (sizeString != null && !"".equals(sizeString.trim())) { try { sample.setSizeInOctet(new Long(headerInfo.getValue(CSVHeaderInfo.CSV_HEADER_SIZEINBYTES, CSVline))); } catch (NumberFormatException exception) { } } else { sample.setSizeInOctet(0); } String latencyString = headerInfo.getValue(CSVHeaderInfo.CSV_HEADER_LATENCY, CSVline); if (latencyString != null && !"".equals(latencyString.trim())) { try { sample.setLatency(new Long(headerInfo.getValue(CSVHeaderInfo.CSV_HEADER_LATENCY, CSVline))); } catch (NumberFormatException exception) { } } String grpThreads = headerInfo.getValue(CSVHeaderInfo.CSV_HEADER_GROUPTHREADS, CSVline); if (grpThreads != null) sample.setGrpThreads(new Long(grpThreads)); String allThreads = headerInfo.getValue(CSVHeaderInfo.CSV_HEADER_ALLTHREADS, CSVline); if (allThreads != null) sample.setAllThreads(new Long(allThreads)); return sample; }
From source file:com.duroty.application.admin.utils.AdminDefaultAction.java
/** * DOCUMENT ME!//w w w.ja v a 2s . c om * * @param request DOCUMENT ME! * * @return DOCUMENT ME! * * @throws NamingException DOCUMENT ME! * @throws RemoteException DOCUMENT ME! * @throws CreateException DOCUMENT ME! */ protected Preferences getPreferencesInstance(HttpServletRequest request) throws NamingException, RemoteException, CreateException { PreferencesHome home = null; Boolean localServer = new Boolean(Configuration.properties.getProperty(Configuration.LOCAL_WEB_SERVER)); if (localServer.booleanValue()) { home = PreferencesUtil.getHome(); } else { Hashtable environment = getContextProperties(request); home = PreferencesUtil.getHome(environment); } return home.create(); }
From source file:eionet.gdem.conversion.excel.writer.ExcelConversionHandler.java
@Override public void addCell(String type, String str_value, String style_name) { HSSFSheet _sheet = wb.getSheetAt(currentSheet); HSSFRow _row = _sheet.getRow(currentRow); HSSFCell _cell = _row.createCell((currentCell)); Double number_value = null;//from w w w . ja v a 2s. c om Boolean boolean_value = null; boolean isNumber = false; boolean isBoolean = false; if (type == null) { type = (String) getDefaultParams("data_type"); } if (type != null) { if (type.equals("float") || type.equals("number")) { if (str_value != null) { try { number_value = new Double(str_value); isNumber = true; } catch (Exception e) { // the value is not number, it will be inserted as a string // System.out.println(e.toString()); } } else { isNumber = true; } } else if (type.equals("boolean")) { if (str_value != null) { try { boolean_value = new Boolean(str_value); isBoolean = true; } catch (Exception e) { // the value is not boolean, it will be inserted as a string // System.out.println(e.toString()); } } else { isBoolean = true; } } else if (type.equals("date")) { if (str_value != null) { try { // cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("yyyymmdd")); /* * * The way how to handle user defined formats not supported right now HSSFDataFormat format = * wb.createDataFormat(); HSSFCellStyle style = wb.createCellStyle(); * style.setDataFormat(format.getFormat("yyyymmdd")); _cell.setCellStyle(style); */ // cellStyle.setDataFormat(new HSSFDataFormat("yyyymmdd")); /* * try{ l_value=Long.parseLong(value); System.out.println(String.valueOf(l_value)); isLong=true; } * catch(Exception e){ System.out.println(e.toString()); } */ /* * if (isLong){ Date d = new Date(); _cell.setCellStyle(cellStyle); //_cell.setCellValue(d); * _cell.setCellValue(value); //System.out.println(d.toString()); isDate=true; } else * _cell.setCellValue(value); */ // System.out.println("hh"); } catch (Exception e) { System.out.println(e.toString()); } } } } if (isNumber) { if (number_value != null) { _cell.setCellValue(number_value.doubleValue()); } _cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); } else if (isBoolean) { if (boolean_value != null) { _cell.setCellValue(boolean_value.booleanValue()); } _cell.setCellType(HSSFCell.CELL_TYPE_BOOLEAN); } else { _cell.setCellType(HSSFCell.CELL_TYPE_STRING); // _cell.setEncoding(HSSFCell.ENCODING_UTF_16 );// _cell.setCellValue(str_value); } short idx = -1; if (style_name != null) { idx = getStyleIdxByName(style_name, ExcelStyleIF.STYLE_FAMILY_TABLE_CELL); } if (idx < 0) { Short short_idx = (Short) getDefaultParams("style"); if (short_idx != null) { idx = short_idx.shortValue(); } } if (idx > -1) { _cell.setCellStyle(wb.getCellStyleAt(idx)); } // calculates the col with according to the first row if (currentRow == 0 && idx > -1) { short colStyleWidth = 0; HSSFCellStyle style = wb.getCellStyleAt(idx); int f_i = style.getFontIndex(); HSSFFont font = wb.getFontAt((short) f_i); // character size short size = font.getFontHeightInPoints(); if (columns.size() > currentCell) { RowColumnDefinition column = columns.get(currentCell); String column_style_name = column.getStyleName() == null ? "" : column.getStyleName(); ExcelStyleIF definedStyle = getStyleByName(column_style_name, ExcelStyleIF.STYLE_FAMILY_TABLE_CELL); if (definedStyle != null) { colStyleWidth = definedStyle.getColumnWidth(); } } short width = (short) (_sheet.getDefaultColumnWidth() * size * 25); if (colStyleWidth > 0) { width = colStyleWidth; } else if (str_value.length() > 0) { width = (short) (str_value.length() * size * 50); } _sheet.setColumnWidth(currentCell, width); } currentCell = _cell.getColumnIndex() + 1; // System.out.println("Cell" + currentCell+ "-" + value); }
From source file:com.antsdb.saltedfish.util.UberUtil.java
@SuppressWarnings("unchecked") public static <T> T toObject(Class<T> klass, Object val) { if (val == null) { return null; }/*from www .j a v a2 s .c o m*/ if (klass.isInstance(val)) { return (T) val; } if (val instanceof byte[]) { if (((byte[]) val).length == 0) { return null; } val = new String((byte[]) val, Charsets.UTF_8); } if (klass == String.class) { return (T) String.valueOf(val); } if (val instanceof String) { String text = (String) val; if (klass == String.class) { return (T) text; } if (text.length() == 0) { return null; } if (klass == Integer.class) { return (T) new Integer(text); } else if (klass == Long.class) { return (T) new Long(text); } else if (klass == BigDecimal.class) { return (T) new BigDecimal(text); } else if (klass == Timestamp.class) { return (T) Timestamp.valueOf(text); } else if (klass == Date.class) { return (T) Date.valueOf(text); } else if (klass == Boolean.class) { return (T) new Boolean(text); } else if (klass == Double.class) { return (T) new Double(text); } } if (val instanceof BigDecimal) { if (klass == Long.class) { Long n = ((BigDecimal) val).longValueExact(); return (T) n; } else if (klass == Integer.class) { Integer n = ((BigDecimal) val).intValueExact(); return (T) n; } else if (klass == Double.class) { Double n = ((BigDecimal) val).doubleValue(); return (T) n; } else if (klass == Boolean.class) { Integer n = ((BigDecimal) val).intValueExact(); return (T) (Boolean) (n != 0); } } if (val instanceof Integer) { if (klass == BigDecimal.class) { return (T) BigDecimal.valueOf((Integer) val); } else if (klass == Long.class) { return (T) Long.valueOf((Integer) val); } else if (klass == Boolean.class) { Integer n = (Integer) val; return (T) (Boolean) (n != 0); } } if (val instanceof Long) { if (klass == BigDecimal.class) { return (T) BigDecimal.valueOf((Long) val); } else if (klass == Boolean.class) { Long n = (Long) val; return (T) (Boolean) (n != 0); } } if (val instanceof Boolean) { if (klass == Long.class) { return (T) Long.valueOf((Boolean) val ? 1 : 0); } } throw new IllegalArgumentException("class: " + val.getClass()); }
From source file:com.duroty.application.mail.actions.SendAction.java
protected ActionForward doExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionMessages errors = new ActionMessages(); try {//from ww w. j ava 2 s . co m boolean isMultipart = FileUpload.isMultipartContent(request); Mail mailInstance = getMailInstance(request); if (isMultipart) { Map fields = new HashMap(); Vector attachments = new Vector(); //Parse the request List items = diskFileUpload.parseRequest(request); //Process the uploaded items Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (item.isFormField()) { if (item.getFieldName().equals("forwardAttachments")) { String[] aux = item.getString().split(":"); MailPartObj part = mailInstance.getAttachment(aux[0], aux[1]); attachments.addElement(part); } else { fields.put(item.getFieldName(), item.getString()); } } else { if (!StringUtils.isBlank(item.getName())) { ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); IOUtils.copy(item.getInputStream(), baos); MailPartObj part = new MailPartObj(); part.setAttachent(baos.toByteArray()); part.setContentType(item.getContentType()); part.setName(item.getName()); part.setSize(item.getSize()); attachments.addElement(part); } catch (Exception ex) { } finally { IOUtils.closeQuietly(baos); } } } } String body = ""; if (fields.get("taBody") != null) { body = (String) fields.get("taBody"); } else if (fields.get("taReplyBody") != null) { body = (String) fields.get("taReplyBody"); } Preferences preferencesInstance = getPreferencesInstance(request); Send sendInstance = getSendInstance(request); String mid = (String) fields.get("mid"); if (StringUtils.isBlank(mid)) { request.setAttribute("action", "compose"); } else { request.setAttribute("action", "reply"); } Boolean isHtml = null; if (StringUtils.isBlank((String) fields.get("isHtml"))) { isHtml = new Boolean(preferencesInstance.getPreferences().isHtmlMessage()); } else { isHtml = Boolean.valueOf((String) fields.get("isHtml")); } sendInstance.send(mid, Integer.parseInt((String) fields.get("identity")), (String) fields.get("to"), (String) fields.get("cc"), (String) fields.get("bcc"), (String) fields.get("subject"), body, attachments, isHtml.booleanValue(), Charset.defaultCharset().displayName(), (String) fields.get("priority")); } else { errors.add("general", new ActionMessage(ExceptionCode.ERROR_MESSAGES_PREFIX + "mail.send", "The form is null")); request.setAttribute("exception", "The form is null"); request.setAttribute("newLocation", null); doTrace(request, DLog.ERROR, getClass(), "The form is null"); } } catch (Exception ex) { String errorMessage = ExceptionUtilities.parseMessage(ex); if (errorMessage == null) { errorMessage = "NullPointerException"; } errors.add("general", new ActionMessage(ExceptionCode.ERROR_MESSAGES_PREFIX + "general", errorMessage)); request.setAttribute("exception", errorMessage); doTrace(request, DLog.ERROR, getClass(), errorMessage); } finally { } if (errors.isEmpty()) { doTrace(request, DLog.INFO, getClass(), "OK"); return mapping.findForward(Constants.ACTION_SUCCESS_FORWARD); } else { saveErrors(request, errors); return mapping.findForward(Constants.ACTION_FAIL_FORWARD); } }