Java tutorial
/* * This file is part of the Synyx Greetingcard module for OpenCms. * * Copyright (c) 2007 Synyx GmbH & Co.KG (http://www.synyx.de) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package com.mylab; import com.mylab.cms.CmsContext; import com.mylab.cms.OpenCmsContext; import com.mylab.cms.OpenCmsVFSService; import com.mylab.cms.VFSService; import com.mylab.dao.GreetingcardConfig; import com.mylab.dao.GreetingcardConfigDao; import com.mylab.dao.GreetingcardTemplate; import com.mylab.dao.GreetingcardTemplateDao; import com.mylab.dao.JdbcWhitelistDao; import com.mylab.dao.OpenCmsGreetingcardConfigDao; import com.mylab.dao.OpenCmsGreetingcardTemplateDao; import com.mylab.dao.TextField; import com.mylab.dao.WhitelistDao; import java.awt.Image; import java.awt.image.RenderedImage; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.servlet.jsp.PageContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.opencms.file.CmsObject; import org.opencms.jsp.CmsJspActionElement; import org.opencms.jsp.CmsJspXmlContentBean; import org.opencms.main.CmsException; /** * Bean that encapsulates the logic used in showPreviewBean.jsp * @author Rainer Steinegger, Synyx GmbH & Co. KG * @author Florian Hopf, Synyx GmbH & Co. KG */ public class ShowPreviewBean extends CmsJspXmlContentBean { private boolean sendNow; private boolean isDateInPast; private boolean isTooManyDays; private String url; private String dateDay; private String dateMonth; private String dateYear; private String dateHour; private String dateMinute; private String authorAddress; private String receiverAddress; private String subject; private String authorName; private String receiverName; private String resourceName; private long transmitTime; private boolean error = false; private Log log = LogFactory.getLog(ShowPreviewBean.class); private String audiomessage; /* * Empty constructor, required for every JavaBean. */ public ShowPreviewBean() { super(); } /** * Constructor, with parameters. * * @param context the JSP page context object * @param req the JSP request * @param res the JSP response */ public ShowPreviewBean(PageContext context, HttpServletRequest req, HttpServletResponse res) { super(context, req, res); } public void init(PageContext context, HttpServletRequest request, HttpServletResponse response) { super.init(context, request, response); sendNow = (request.getParameter("sendNow") != null && request.getParameter("sendNow").equals("0")); dateDay = request.getParameter("date_day"); dateMonth = request.getParameter("date_month"); dateYear = request.getParameter("date_year"); dateHour = request.getParameter("date_hour"); dateMinute = request.getParameter("date_minute"); // check if the entered date is valid, if not, the user has a second chance to enter it, either the mail becomes send the next possible date // get the entered date from the request int numericDateDay = Integer.parseInt(dateDay); int numericDateMonth = Integer.parseInt(dateMonth); int numericDateYear = Integer.parseInt(dateYear); int numericDateHour = Integer.parseInt(dateHour); int numericDateMinute = Integer.parseInt(dateMinute); // get the amount days for the selected year and month int daysOfTheMonth = DateBean.getAmountDays(numericDateMonth, numericDateYear); Calendar calendar = Calendar.getInstance(); //calendar.setTimeZone(TimeZone.getTimeZone("Europe/Berlin")); long currentLong = calendar.getTimeInMillis(); calendar.set(Calendar.HOUR_OF_DAY, numericDateHour); calendar.set(Calendar.MINUTE, numericDateMinute); calendar.set(Calendar.DATE, numericDateDay); calendar.set(Calendar.MONTH, numericDateMonth - 1); calendar.set(Calendar.YEAR, numericDateYear); transmitTime = calendar.getTimeInMillis(); isDateInPast = !isSendNow() && transmitTime < currentLong; isTooManyDays = !isSendNow() && numericDateDay > daysOfTheMonth; if (!isIsDateInPast() && !isIsTooManyDays()) { try { createCard(context, request, response); } catch (RuntimeException ex) { error = true; throw ex; } catch (Exception ex) { log.error("Error occured trying to create card", ex); error = true; } } } public boolean hasError() { return error; } public boolean isDateInPast() { return isIsDateInPast(); } public boolean isTooManyDays() { return isIsTooManyDays(); } /** * @param context * @param request * @param response * @throws Exception */ private void createCard(PageContext context, HttpServletRequest request, HttpServletResponse response) throws Exception { CmsJspActionElement jsp = new CmsJspActionElement(context, request, response); CmsObject cmsObject = jsp.getCmsObject(); // the text is send by the parameter text0, text1, .. depending on how much textfields are available // this text fields are reveived now int whichTextField = 0; List<String> text = new ArrayList<String>(); while (request.getParameter("text" + whichTextField) != null) { text.add(request.getParameter("text" + whichTextField)); whichTextField++; } // to get the text information its necessary to know the path of the xml content String cardPath = request.getParameter("greetingcard_fileName"); //get the parameters from the request authorName = request.getParameter("author_name"); authorAddress = request.getParameter("author_address"); receiverName = request.getParameter("receiver_name"); if (request.getParameter("receiver_address") != null) { receiverAddress = request.getParameter("receiver_address"); } else { receiverAddress = request.getParameter("receiver_address_whitelist") + "@" + request.getParameter("receiver_domain"); } subject = request.getParameter("subject"); VFSService vfsService = new OpenCmsVFSService(jsp); GreetingcardConfigDao configDao = new OpenCmsGreetingcardConfigDao(cmsObject); GreetingcardTemplateDao templateDao = new OpenCmsGreetingcardTemplateDao(cmsObject); CmsContext cmsContext = new OpenCmsContext(jsp); GreetingcardService greetingcardService = new DefaultGreetingcardService(jsp, configDao, cmsContext); GreetingcardTemplate card = templateDao.readCard(cardPath); GreetingcardConfig config = greetingcardService.getGreetingcardConfig(); String imagePath = card.getImage(); // generate the picture and initialize an instance of the image creater class for adding the received text to the picture String newUrl = ""; String pictureFileName = cmsContext.getServerPath(cmsContext.link(imagePath)); Image backgroundImage = ImageIO.read(new URL(pictureFileName)); ImageCreator imageCreator = new ImageCreator(backgroundImage, authorName, receiverName, config.getArchiveFolder()); // add the text to the picture for (int i = 0; i < card.getTextFields().size(); i++) { TextField field = card.getTextFields().get(i); String currentText = text.get(i); String fontColor = field.getFontColor(); String fontType = field.getFontType(); int fontSize = field.getFontSize(); int pictureTopX = field.getTopX(); int pictureTopY = field.getTopY(); int pictureBottomX = field.getBottomX(); int pictureBottomY = field.getBottomY(); imageCreator.addText(pictureTopX, pictureTopY, pictureBottomX, pictureBottomY, currentText, fontSize, fontColor, field.isItalic(), field.isUnderline(), field.isBold(), field.isPlain(), fontType); } // get the generated image from the image creater and save it RenderedImage newPicture = (RenderedImage) imageCreator.getCardFile(); // the image creater class also handles the new filename (with the path) of the picture to save at newUrl = cmsContext.removeSiteRoot(imageCreator.getNewImageLocation()); WhitelistDao whitelistDao = new JdbcWhitelistDao(config); loginUser(); vfsService.writeImage(newPicture, newUrl); logout(); resourceName = newUrl + ".png"; url = cmsContext.link(resourceName); } /** * logout the user * */ public void logout() { HttpSession session = getRequest().getSession(false); if (session != null) { session.invalidate(); } } /** * Login the given user, preserving the current project context. * * @param User username to login * @param Password user's password * @return FORMACTION_OK if successful, else FORMACTION_ERROR. If an error occurred * the error message can be retrieved via {@link #getRegisterActionMessage()} */ protected int loginUser() { try { // attempt to login the user String OU = "/cardadmin"; getCmsObject().loginUser(OU, "xxx17yyy"); // make sure we have a new session after login for security reasons HttpSession session = getRequest().getSession(false); if (session != null) { session.invalidate(); } session = getRequest().getSession(true); } catch (CmsException e) { // return error code } finally { // switch back to original project } return 0; } public boolean isSendNow() { return sendNow; } public boolean isIsDateInPast() { return isDateInPast; } public boolean isIsTooManyDays() { return isTooManyDays; } public String getUrl() { return url; } public String getDateDay() { return dateDay; } public String getDateMonth() { return dateMonth; } public String getDateYear() { return dateYear; } public String getDateHour() { return dateHour; } public String getDateMinute() { return dateMinute; } public String getAuthorAddress() { return authorAddress; } public String getReceiverAddress() { return receiverAddress; } public String getSubject() { return subject; } public String getAuthorName() { return authorName; } public String getReceiverName() { return receiverName; } public long getTransmitTime() { return transmitTime; } public String getResourceName() { return resourceName; } public String getAudioMessage() { if (audiomessage == null) audiomessage = "http://92.67.39.201/opencms/opencms/simon/audio/Letter-To-Hermione.mp3"; return audiomessage; } public void setAudioMessage(String audiomessage) { this.audiomessage = audiomessage; } }