001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * This file creation date: 13/01/2004 / 18:45:31
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.view.admin;
044:
045: import java.io.File;
046:
047: import net.jforum.dao.DataAccessDriver;
048: import net.jforum.dao.SmilieDAO;
049: import net.jforum.entities.Smilie;
050: import net.jforum.repository.SmiliesRepository;
051: import net.jforum.util.MD5;
052: import net.jforum.util.legacy.commons.fileupload.FileItem;
053: import net.jforum.util.preferences.ConfigKeys;
054: import net.jforum.util.preferences.SystemGlobals;
055: import net.jforum.util.preferences.TemplateKeys;
056: import net.jforum.view.forum.common.UploadUtils;
057:
058: /**
059: * @author Rafael Steil
060: * @version $Id: SmiliesAction.java,v 1.15 2006/08/20 22:47:44 rafaelsteil Exp $
061: */
062: public class SmiliesAction extends AdminCommand {
063: private String processUpload() {
064: String imgName = "";
065:
066: if (this .request.getObjectParameter("smilie_img") != null) {
067: FileItem item = (FileItem) this .request
068: .getObjectParameter("smilie_img");
069: UploadUtils uploadUtils = new UploadUtils(item);
070:
071: imgName = MD5.crypt(item.getName());
072:
073: uploadUtils.saveUploadedFile(SystemGlobals
074: .getApplicationPath()
075: + "/"
076: + SystemGlobals
077: .getValue(ConfigKeys.SMILIE_IMAGE_DIR)
078: + "/" + imgName + "." + uploadUtils.getExtension());
079:
080: imgName += "." + uploadUtils.getExtension();
081: }
082:
083: return imgName;
084: }
085:
086: public void insert() {
087: this .setTemplateName(TemplateKeys.SMILIES_INSERT);
088: this .context.put("action", "insertSave");
089: }
090:
091: public void insertSave() {
092: Smilie s = new Smilie();
093: s.setCode(this .request.getParameter("code"));
094:
095: String imgName = this .processUpload();
096: s.setUrl(SystemGlobals
097: .getValue(ConfigKeys.SMILIE_IMAGE_PATTERN).replaceAll(
098: "#IMAGE#", imgName));
099:
100: s.setDiskName(imgName);
101:
102: DataAccessDriver.getInstance().newSmilieDAO().addNew(s);
103:
104: SmiliesRepository.loadSmilies();
105: this .list();
106: }
107:
108: public void edit() {
109: int id = 1;
110:
111: if (this .request.getParameter("id") != null) {
112: id = this .request.getIntParameter("id");
113: }
114:
115: this .setTemplateName(TemplateKeys.SMILIES_EDIT);
116: this .context.put("smilie", DataAccessDriver.getInstance()
117: .newSmilieDAO().selectById(id));
118: this .context.put("action", "editSave");
119: }
120:
121: public void editSave() {
122: Smilie s = DataAccessDriver.getInstance().newSmilieDAO()
123: .selectById(this .request.getIntParameter("id"));
124: s.setCode(this .request.getParameter("code"));
125:
126: if (this .request.getObjectParameter("smilie_img") != null) {
127: String imgName = this .processUpload();
128: s.setUrl(SystemGlobals.getValue(
129: ConfigKeys.SMILIE_IMAGE_PATTERN).replaceAll(
130: "#IMAGE#", imgName));
131: s.setDiskName(imgName);
132: }
133:
134: DataAccessDriver.getInstance().newSmilieDAO().update(s);
135:
136: SmiliesRepository.loadSmilies();
137: this .list();
138: }
139:
140: public void delete() {
141: String[] ids = this .request.getParameterValues("id");
142:
143: if (ids != null) {
144: SmilieDAO dao = DataAccessDriver.getInstance()
145: .newSmilieDAO();
146:
147: for (int i = 0; i < ids.length; i++) {
148: int id = Integer.parseInt(ids[i]);
149:
150: Smilie s = dao.selectById(id);
151: dao.delete(id);
152:
153: File f = new File(SystemGlobals.getApplicationPath()
154: + "/"
155: + SystemGlobals
156: .getValue(ConfigKeys.SMILIE_IMAGE_DIR)
157: + "/" + s.getDiskName());
158:
159: if (f.exists()) {
160: f.delete();
161: }
162: }
163: }
164:
165: SmiliesRepository.loadSmilies();
166: this .list();
167: }
168:
169: /**
170: * @see net.jforum.Command#list()
171: */
172: public void list() {
173: this .context.put("smilies", SmiliesRepository.getSmilies());
174: this.setTemplateName(TemplateKeys.SMILIES_LIST);
175: }
176: }
|