List of usage examples for org.apache.commons.beanutils PropertyUtils copyProperties
public static void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Copy property values from the "origin" bean to the "destination" bean for all cases where the property names are the same (even though the actual getter and setter methods might have been customized via BeanInfo
classes).
For more details see PropertyUtilsBean
.
From source file:org.apache.struts.webapp.example2.EditSubscriptionAction.java
/** * Process the specified HTTP request, and create the corresponding HTTP * response (or forward to another web component that will create it). * Return an <code>ActionForward</code> instance describing where and how * control should be forwarded, or <code>null</code> if the response has * already been completed.//from w ww.j a v a 2 s .c om * * @param mapping The ActionMapping used to select this instance * @param form The optional ActionForm bean for this request (if any) * @param request The HTTP request we are processing * @param response The HTTP response we are creating * * @exception Exception if the application business logic throws * an exception */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Extract attributes we will need Locale locale = getLocale(request); MessageResources messages = getResources(request); HttpSession session = request.getSession(); String action = request.getParameter("action"); if (action == null) { action = "Create"; } String host = request.getParameter("host"); if (log.isDebugEnabled()) { log.debug("EditSubscriptionAction: Processing " + action + " action"); } // Is there a currently logged on user? User user = (User) session.getAttribute(Constants.USER_KEY); if (user == null) { if (log.isTraceEnabled()) { log.trace(" User is not logged on in session " + session.getId()); } return (mapping.findForward("logon")); } // Identify the relevant subscription Subscription subscription = user.findSubscription(request.getParameter("host")); if ((subscription == null) && !action.equals("Create")) { if (log.isTraceEnabled()) { log.trace(" No subscription for user " + user.getUsername() + " and host " + host); } return (mapping.findForward("failure")); } if (subscription != null) { session.setAttribute(Constants.SUBSCRIPTION_KEY, subscription); } // Populate the subscription form if (form == null) { if (log.isTraceEnabled()) { log.trace(" Creating new SubscriptionForm bean under key " + mapping.getAttribute()); } form = new SubscriptionForm(); if ("request".equals(mapping.getScope())) { request.setAttribute(mapping.getAttribute(), form); } else { session.setAttribute(mapping.getAttribute(), form); } } SubscriptionForm subform = (SubscriptionForm) form; subform.setAction(action); if (!action.equals("Create")) { if (log.isTraceEnabled()) { log.trace(" Populating form from " + subscription); } try { PropertyUtils.copyProperties(subform, subscription); subform.setAction(action); } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); if (t == null) t = e; log.error("SubscriptionForm.populate", t); throw new ServletException("SubscriptionForm.populate", t); } catch (Throwable t) { log.error("SubscriptionForm.populate", t); throw new ServletException("SubscriptionForm.populate", t); } } // Forward control to the edit subscription page if (log.isTraceEnabled()) { log.trace(" Forwarding to 'success' page"); } return (mapping.findForward("success")); }
From source file:org.apache.struts.webapp.example2.SaveRegistrationAction.java
/** * Process the specified HTTP request, and create the corresponding HTTP * response (or forward to another web component that will create it). * Return an <code>ActionForward</code> instance describing where and how * control should be forwarded, or <code>null</code> if the response has * already been completed.//from w ww. ja va 2 s . c o m * * @param mapping The ActionMapping used to select this instance * @param form The optional ActionForm bean for this request (if any) * @param request The HTTP request we are processing * @param response The HTTP response we are creating * * @exception Exception if the application business logic throws * an exception */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Extract attributes and parameters we will need Locale locale = getLocale(request); MessageResources messages = getResources(request); HttpSession session = request.getSession(); RegistrationForm regform = (RegistrationForm) form; String action = regform.getAction(); if (action == null) { action = "Create"; } UserDatabase database = (UserDatabase) servlet.getServletContext().getAttribute(Constants.DATABASE_KEY); if (log.isDebugEnabled()) { log.debug("SaveRegistrationAction: Processing " + action + " action"); } // Is there a currently logged on user (unless creating)? User user = (User) session.getAttribute(Constants.USER_KEY); if (!"Create".equals(action) && (user == null)) { if (log.isTraceEnabled()) { log.trace(" User is not logged on in session " + session.getId()); } return (mapping.findForward("logon")); } // Was this transaction cancelled? if (isCancelled(request)) { if (log.isTraceEnabled()) { log.trace(" Transaction '" + action + "' was cancelled"); } session.removeAttribute(Constants.SUBSCRIPTION_KEY); return (mapping.findForward("failure")); } // Validate the transactional control token ActionErrors errors = new ActionErrors(); if (log.isTraceEnabled()) { log.trace(" Checking transactional control token"); } if (!isTokenValid(request)) { errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.transaction.token")); } resetToken(request); // Validate the request parameters specified by the user if (log.isTraceEnabled()) { log.trace(" Performing extra validations"); } String value = null; value = regform.getUsername(); if (("Create".equals(action)) && (database.findUser(value) != null)) { errors.add("username", new ActionError("error.username.unique", regform.getUsername())); } if ("Create".equals(action)) { value = regform.getPassword(); if ((value == null) || (value.length() < 1)) { errors.add("password", new ActionError("error.password.required")); } value = regform.getPassword2(); if ((value == null) || (value.length() < 1)) { errors.add("password2", new ActionError("error.password2.required")); } } // Report any errors we have discovered back to the original form if (!errors.isEmpty()) { saveErrors(request, errors); saveToken(request); return (mapping.getInputForward()); } // Update the user's persistent profile information try { if ("Create".equals(action)) { user = database.createUser(regform.getUsername()); } String oldPassword = user.getPassword(); PropertyUtils.copyProperties(user, regform); if ((regform.getPassword() == null) || (regform.getPassword().length() < 1)) { user.setPassword(oldPassword); } } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); if (t == null) { t = e; } log.error("Registration.populate", t); throw new ServletException("Registration.populate", t); } catch (Throwable t) { log.error("Registration.populate", t); throw new ServletException("Subscription.populate", t); } try { database.save(); } catch (Exception e) { log.error("Database save", e); } // Log the user in if appropriate if ("Create".equals(action)) { session.setAttribute(Constants.USER_KEY, user); if (log.isTraceEnabled()) { log.trace(" User '" + user.getUsername() + "' logged on in session " + session.getId()); } } // Remove the obsolete form bean if (mapping.getAttribute() != null) { if ("request".equals(mapping.getScope())) request.removeAttribute(mapping.getAttribute()); else session.removeAttribute(mapping.getAttribute()); } // Forward control to the specified success URI if (log.isTraceEnabled()) { log.trace(" Forwarding to success page"); } return (mapping.findForward("success")); }
From source file:org.apache.struts.webapp.example2.SaveSubscriptionAction.java
/** * Process the specified HTTP request, and create the corresponding HTTP * response (or forward to another web component that will create it). * Return an <code>ActionForward</code> instance describing where and how * control should be forwarded, or <code>null</code> if the response has * already been completed.// ww w .j a v a2s.co m * * @param mapping The ActionMapping used to select this instance * @param form The optional ActionForm bean for this request (if any) * @param request The HTTP request we are processing * @param response The HTTP response we are creating * * @exception Exception if the application business logic throws * an exception */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Extract attributes and parameters we will need Locale locale = getLocale(request); MessageResources messages = getResources(request); HttpSession session = request.getSession(); SubscriptionForm subform = (SubscriptionForm) form; String action = subform.getAction(); if (action == null) { action = "?"; } if (log.isDebugEnabled()) { log.debug("SaveSubscriptionAction: Processing " + action + " action"); } // Is there a currently logged on user? User user = (User) session.getAttribute(Constants.USER_KEY); if (user == null) { if (log.isTraceEnabled()) { log.trace(" User is not logged on in session " + session.getId()); } return (mapping.findForward("logon")); } // Was this transaction cancelled? if (isCancelled(request)) { if (log.isTraceEnabled()) { log.trace(" Transaction '" + action + "' was cancelled"); } session.removeAttribute(Constants.SUBSCRIPTION_KEY); return (mapping.findForward("success")); } // Is there a related Subscription object? Subscription subscription = (Subscription) session.getAttribute(Constants.SUBSCRIPTION_KEY); if ("Create".equals(action)) { if (log.isTraceEnabled()) { log.trace(" Creating subscription for mail server '" + subform.getHost() + "'"); } subscription = user.createSubscription(subform.getHost()); } if (subscription == null) { if (log.isTraceEnabled()) { log.trace(" Missing subscription for user '" + user.getUsername() + "'"); } response.sendError(HttpServletResponse.SC_BAD_REQUEST, messages.getMessage("error.noSubscription")); return (null); } // Was this transaction a Delete? if (action.equals("Delete")) { if (log.isTraceEnabled()) { log.trace(" Deleting mail server '" + subscription.getHost() + "' for user '" + user.getUsername() + "'"); } user.removeSubscription(subscription); session.removeAttribute(Constants.SUBSCRIPTION_KEY); try { UserDatabase database = (UserDatabase) servlet.getServletContext() .getAttribute(Constants.DATABASE_KEY); database.save(); } catch (Exception e) { log.error("Database save", e); } return (mapping.findForward("success")); } // All required validations were done by the form itself // Update the persistent subscription information if (log.isTraceEnabled()) { log.trace(" Populating database from form bean"); } try { PropertyUtils.copyProperties(subscription, subform); } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); if (t == null) t = e; log.error("Subscription.populate", t); throw new ServletException("Subscription.populate", t); } catch (Throwable t) { log.error("Subscription.populate", t); throw new ServletException("Subscription.populate", t); } try { UserDatabase database = (UserDatabase) servlet.getServletContext().getAttribute(Constants.DATABASE_KEY); database.save(); } catch (Exception e) { log.error("Database save", e); } // Remove the obsolete form bean and current subscription if (mapping.getAttribute() != null) { if ("request".equals(mapping.getScope())) request.removeAttribute(mapping.getAttribute()); else session.removeAttribute(mapping.getAttribute()); } session.removeAttribute(Constants.SUBSCRIPTION_KEY); // Forward control to the specified success URI if (log.isTraceEnabled()) { log.trace(" Forwarding to success page"); } return (mapping.findForward("success")); }
From source file:org.bitsofinfo.util.address.usps.ais.USPSFullAddressService.java
private void processStreetRecord(ZipPlus4Detail z4record, List<USPSFullAddress> addys, AddressRange primaryRange, ZipPlus4Range z4range, CityStateDetail detail, CityStateSeasonal seasonal) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { // generate a full record for each address while (primaryRange.hasNext()) { USPSFullAddress full = new USPSFullAddress(); // clone over all source z4 record props PropertyUtils.copyProperties(full, z4record); // clone over city state detail source record props PropertyUtils.copyProperties(full, detail); // clone over city state seasonal source record props if (seasonal != null) { PropertyUtils.copyProperties(full, seasonal); }// w w w . ja v a2s . c o m // set the actual address full.setPrimaryAddress(primaryRange.next()); // set the full ZIP+4 zip code full.setFullZipPlus4Code(full.getZipCode() + "-" + z4range.getSingleCode()); // deliverable? if (z4range.isNonDeliverySegment()) { full.setNonDeliverable(true); } // add the address to the return set addys.add(full); } }
From source file:org.cloudifysource.esc.driver.provisioning.PersistentMachineDetails.java
/****** * Populates the fields of the current object from the given MachineDetails. Note that all copy operations are * shallow. Unsafe properties will be replaced. * /* w w w. ja v a2s. c o m*/ * @param md * the machine detaions to copy. */ public void populateFromMachineDetails(final MachineDetails md) { try { PropertyUtils.copyProperties(this, md); } catch (IllegalAccessException e) { throw new IllegalStateException("Failed to create persistent machine details: " + e.getMessage(), e); } catch (InvocationTargetException e) { throw new IllegalStateException("Failed to create persistent machine details: " + e.getMessage(), e); } catch (NoSuchMethodException e) { throw new IllegalStateException("Failed to create persistent machine details: " + e.getMessage(), e); } if (this.getKeyFile() != null) { this.setKeyFileName(this.getKeyFile().getAbsolutePath()); this.setKeyFile(null); } }
From source file:org.cloudifysource.esc.driver.provisioning.PersistentMachineDetails.java
/************** * Creates a new machine details object from the current persistent details. Unsafe properties are re-created. * //from w w w .java2 s. co m * @return the machine details. **/ public MachineDetails toMachineDetails() { final MachineDetails md = new MachineDetails(); try { PropertyUtils.copyProperties(md, this); } catch (IllegalAccessException e) { throw new IllegalStateException("Failed to create machine details: " + e.getMessage(), e); } catch (InvocationTargetException e) { throw new IllegalStateException("Failed to create machine details: " + e.getMessage(), e); } catch (NoSuchMethodException e) { throw new IllegalStateException("Failed to create machine details: " + e.getMessage(), e); } if (this.getKeyFileName() != null) { final File keyFile = new File(this.getKeyFileName()); md.setKeyFile(keyFile); } return md; }
From source file:org.gbif.occurrence.download.oozie.ArchiveBuilder.java
/** * Checks the contacts of a dataset and finds the preferred contact that should be used as the main author * of a dataset.//from w w w . java 2 s . c om * * @return preferred author contact or null */ private Contact getContentProviderContact(Dataset dataset) { Contact author = null; for (ContactType type : AUTHOR_TYPES) { for (Contact c : dataset.getContacts()) { if (type == c.getType()) { if (author == null) { author = c; } else if (c.isPrimary()) { author = c; } } } if (author != null) { Contact provider = new Contact(); try { PropertyUtils.copyProperties(provider, author); provider.setKey(null); provider.setType(ContactType.CONTENT_PROVIDER); provider.setPrimary(false); return provider; } catch (IllegalAccessException e) { LOG.error("Error setting provider contact", e); } catch (InvocationTargetException e) { LOG.error("Error setting provider contact", e); } catch (NoSuchMethodException e) { LOG.error("Error setting provider contact", e); } } } return null; }
From source file:org.gluu.oxtrust.action.UpdateOrganizationAction.java
private String modifyOrganization() { if (this.organization != null) { return OxTrustConstants.RESULT_SUCCESS; }/*from ww w .jav a 2 s . co m*/ try { GluuOrganization tmpOrganization = organizationService.getOrganization(); this.organization = new GluuOrganization(); // Clone shared instance try { PropertyUtils.copyProperties(this.organization, tmpOrganization); } catch (Exception ex) { log.error("Failed to load organization", ex); this.organization = null; } } catch (LdapMappingException ex) { log.error("Failed to load organization", ex); } if (this.organization == null) { return OxTrustConstants.RESULT_FAILURE; } initLogoImage(); initFaviconImage(); this.loginPageCustomMessage = organizationService .getOrganizationCustomMessage(OxTrustConstants.CUSTOM_MESSAGE_LOGIN_PAGE); this.welcomePageCustomMessage = organizationService .getOrganizationCustomMessage(OxTrustConstants.CUSTOM_MESSAGE_WELCOME_PAGE); this.welcomeTitleText = organizationService .getOrganizationCustomMessage(OxTrustConstants.CUSTOM_MESSAGE_TITLE_TEXT); appliances = new ArrayList<GluuAppliance>(); try { appliances.addAll(ApplianceService.instance().getAppliances()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return OxTrustConstants.RESULT_SUCCESS; }
From source file:org.kuali.coeus.propdev.impl.attachment.ProposalDevelopmentAttachmentController.java
@Transactional @RequestMapping(value = "/proposalDevelopment", params = "methodToCall=prepareNarrative") public ModelAndView prepareNarrative(@ModelAttribute("KualiForm") ProposalDevelopmentDocumentForm form) throws Exception { String selectedLine = form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX); form.getProposalDevelopmentAttachmentHelper().reset(); if (StringUtils.isNotEmpty(selectedLine)) { Narrative tmpNarrative = new Narrative(); form.getProposalDevelopmentAttachmentHelper().setSelectedLineIndex(selectedLine); PropertyUtils.copyProperties(tmpNarrative, form.getDevelopmentProposal().getNarrative(Integer.parseInt(selectedLine))); form.getProposalDevelopmentAttachmentHelper().setNarrative(tmpNarrative); }//w w w.jav a 2 s . c o m return getModelAndViewService().showDialog( ProposalDevelopmentConstants.KradConstants.PROP_DEV_ATTACHMENTS_PAGE_PROPOSAL_DETAILS, true, form); }
From source file:org.kuali.coeus.propdev.impl.attachment.ProposalDevelopmentAttachmentController.java
@Transactional @RequestMapping(value = "/proposalDevelopment", params = "methodToCall=prepareBiography") public ModelAndView prepareBiography(@ModelAttribute("KualiForm") ProposalDevelopmentDocumentForm form) throws Exception { String selectedLine = form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX); form.getProposalDevelopmentAttachmentHelper().reset(); if (StringUtils.isNotEmpty(selectedLine)) { ProposalPersonBiography tmpBiography = new ProposalPersonBiography(); form.getProposalDevelopmentAttachmentHelper().setSelectedLineIndex(selectedLine); PropertyUtils.copyProperties(tmpBiography, form.getDevelopmentProposal().getPropPersonBio(Integer.parseInt(selectedLine))); form.getProposalDevelopmentAttachmentHelper().setBiography(tmpBiography); }//from www . j av a 2 s .co m return getModelAndViewService().showDialog( ProposalDevelopmentConstants.KradConstants.PROP_DEV_ATTACHMENTS_PAGE_PERSONNEL_DETAILS, true, form); }