velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Neal Smith" <Neal.Sm...@sas.com>
Subject RE: Struts and I18N
Date Thu, 25 Aug 2005 18:17:56 GMT
Re: Formatting Numbers, etc. -  I had read that, but being so wrapped up in Struts stuff, I
completely forgot about that.  


Re: VelocityContext - Hmmm...  This is where I thought I was going to have an issue.  The
existing code is just creating a new VelocityContext.  I'm not sure I understand all of your
last question.  I see by the doc, that VelocityViewServlet has a protected createContext(),
but how do I get hold of that context?  Is the request holding the VelocityViewServlet for
me or do I need to create my own?


Thanks,
Neal

P.S.  Here is the code where we create the VelocityContext:


public boolean sendEmail(AlertEmailContent emailContent, UserContent userContent)
	   throws ServiceException
	{
		String alertID = String.valueOf(emailContent.getAlertID());
		String toAddresses = emailContent.getToAddresses();			

		//		String ccAddresses = emailContent.getCcAddresses();
		String userID = emailContent.getUserID();

		AlertContent alertContent = _alertServ.getAlertContent(alertID);

		VelocityContext alertContext = new VelocityContext();
		alertContext.put("ac", alertContent);
		
		File alertFile = createFile("Alert", ALERT_TEMPLATE_NAME, alertContext);

		ArrayList commentsList = (ArrayList) _alertServ.getComments(alertID, "ALT");

		MimeMessage msg = new MimeMessage(_mailSession);
		
//		Automatically cc the sender if enabled
		if (ccSelf)
		{
            try 
            {             
                String addy = userContent.getEmailAddress();
                msg.setHeader("cc", userContent.getEmailAddress());
            } catch (MessagingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
		}
		
		ArrayList toList = new ArrayList();

		StringTokenizer st = new StringTokenizer(toAddresses, ",;");

		File commentsFile = null;
		Transport transportClass = null;

		try
		{
			while (st.hasMoreTokens())
			{
				toList.add(new InternetAddress(st.nextToken()));
			}

			//			InternetAddress[] address = { new InternetAddress(toAddresses) };
			InternetAddress[] toRecipients =
				(InternetAddress[]) toList.toArray(new InternetAddress[0]);
			msg.setRecipients(Message.RecipientType.TO, toRecipients);

			//			if (ccAddresses.length() > 0)
			//			{
			//				ArrayList ccList = new ArrayList();
			//
			//				st = new StringTokenizer(ccAddresses, ",");
			//
			//				while (st.hasMoreTokens())
			//				{
			//					ccList.add(new InternetAddress(st.nextToken()));
			//				}
			//
			//				InternetAddress[] ccRecipients =
			//					(InternetAddress[]) ccList.toArray(new InternetAddress[0]);
			//				msg.setRecipients(Message.RecipientType.CC, ccRecipients);
			//			}
			msg.setFrom(new InternetAddress(_mailFromUserID, _mailFromUserName));
			msg.setSubject(emailContent.getSubject());

			MimeBodyPart msgBody = new MimeBodyPart();

			msgBody.setText(emailContent.getMessage());

			MimeBodyPart alertBodyPart = new MimeBodyPart();
			alertBodyPart.setDataHandler(new DataHandler(new FileDataSource(alertFile)));
			alertBodyPart.setFileName("Alert-" + alertID + _mailFileExtension);

			Multipart multipart = new MimeMultipart();
			multipart.addBodyPart(msgBody);
			multipart.addBodyPart(alertBodyPart);

			if (commentsList.size() > 0)
			{
				VelocityContext commentsContext = new VelocityContext();
				commentsContext.put("ac", alertContent);
				commentsContext.put("appTitle", _appTitle);
				commentsContext.put("comments", commentsList);
				commentsContext.put("commentCount", String.valueOf(commentsList.size()));

				commentsFile = createFile("Comments", COMMENTS_TEMPLATE_NAME, commentsContext);

				MimeBodyPart commentsBodyPart = new MimeBodyPart();
				commentsBodyPart.setDataHandler(new DataHandler(new FileDataSource(commentsFile)));
				commentsBodyPart.setFileName("Comments-" + alertID + _mailFileExtension);
				multipart.addBodyPart(commentsBodyPart);
			}

			addAttachments(alertID, multipart);
			msg.setContent(multipart);
			msg.setSentDate(new Date());

			if (_mailSMTPPassword == null)
			{
				Transport.send(msg);
			}
			else
			{
				msg.saveChanges();
				transportClass = _mailSession.getTransport();
				transportClass.connect(_mailHost, _mailSMTPUserID, _mailSMTPPassword);

				//t.sendMessage(msg, msg.getAllRecipients());
				transportClass.sendMessage(msg, toRecipients);
			}
		}
		catch (Exception ex) // AddressException, UnsupportedEncodingException, MessagingException
		{
			throw new ServiceException("Problem sending email to " + toAddresses, ex);
		}
		finally
		{
			if (alertFile != null)
				alertFile.delete();

			if (commentsFile != null)
				commentsFile.delete();

			try
			{
				if (transportClass != null)
					transportClass.close();
			}
			catch (MessagingException e)
			{
				// can't close transport, message was sent?  Don't throw exception
				_log.warn("Unable to close mail transport.", e);
			}
		}

		long emailID;

		try
		{
			emailID = saveInfo(emailContent);
		}
		catch (ServiceException e)
		{
			throw new ServiceException("Problem saving email info to database.", e);
		}

    	_audit.track(Integer.valueOf(alertID).intValue(),AuditTrailConstants.EMAIL,
    	        MessageFormat.format(_msgs.getString("alert.history.email.txt"),new Object[]
{String.valueOf(emailID),toAddresses})
    	        ,userID);

    	return true;
	}

-----Original Message-----
From: Nathan Bubna [mailto:nbubna@gmail.com] 
Sent: Thursday, August 25, 2005 12:48 PM
To: Velocity Users List
Subject: Re: Struts and I18N


actually, you can do this with VelocityTools.  we have both a DateTool and a NumberTool that
should support I18N just fine. 
http://jakarta.apache.org/velocity/tools/generic/


>   Here is the method where we are "generating" the template:
> 
> private File createFile(String prefix, String template, VelocityContext context)
>            throws ServiceException
>         {
#snip

ok, but where are you getting the VelocityContext from?  if you want access to a working and
initialized MessageTool then you'll want to get the context created by the VelocityViewServlet.
 which, by the way, are you using in the course of the servlet request that calls the methods
above?



---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Mime
View raw message