001: /*
002: * (C) Copyright 2004 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package com.nabhinc.portlet.news;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.sql.Connection;
024: import java.sql.PreparedStatement;
025: import java.sql.ResultSet;
026: import java.sql.SQLException;
027:
028: import javax.portlet.ActionRequest;
029: import javax.portlet.ActionResponse;
030: import javax.portlet.PortletException;
031: import javax.sql.DataSource;
032:
033: import org.w3c.dom.Element;
034:
035: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
036: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
037: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
038: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
039: import com.nabhinc.util.XMLUtil;
040: import com.nabhinc.util.db.DBUtil;
041:
042: /**
043: *
044: *
045: * @author Padmanabh Dabke
046: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
047: */
048: public class DeleteChannelFiles extends BaseRequestProcessor implements
049: ActionProcessor {
050:
051: private String ccfBaseDir = null;
052: private String ccfRealBaseDir = null;
053: private String ccfBaseURL = null;
054: private String ccfChannelNameSQL = "SELECT cname FROM SB_NEWS_CHANNELS WHERE id = ?";
055:
056: public void init(Element config, ControllerPortletConfig conConfig)
057: throws PortletException {
058: super .init(config, conConfig);
059: ccfBaseDir = conConfig.getParameter("base-news-dir");
060: if (ccfBaseDir == null) {
061: throw new PortletException(
062: "Missing required global parameter: base-news-dir.");
063: }
064: ccfRealBaseDir = getRealPath(brpConfig.getPortletContext(),
065: ccfBaseDir);
066:
067: String temp = XMLUtil.getSubElementText(config,
068: "channel-name-sql");
069: if (temp != null)
070: ccfChannelNameSQL = temp;
071:
072: }
073:
074: /**
075: * @param request
076: * @param response
077: * @param actionConfig
078: * @return
079: * @throws PortletException
080: * @throws IOException
081: */
082: public String process(ActionRequest request,
083: ActionResponse response, ActionConfig actionConfig)
084: throws PortletException, IOException {
085:
086: String channelIDStr = request.getParameter("channel_id");
087: if (channelIDStr == null)
088: throw new PortletException("Missing channel id.");
089:
090: int channelID = 0;
091: String result = "success";
092:
093: try {
094: channelID = Integer.parseInt(channelIDStr);
095: } catch (Exception ex) {
096: throw new PortletException("Invalid channel ID: "
097: + channelIDStr + ".");
098: }
099: ResultSet rs = null;
100: PreparedStatement st = null;
101: Connection conn = null;
102: String baseURL = ccfBaseURL;
103: try {
104: DataSource ds = brpConfig.getDataSource();
105: conn = ds.getConnection();
106: st = conn.prepareStatement(ccfChannelNameSQL);
107: st.setInt(1, channelID);
108: rs = st.executeQuery();
109: if (!rs.next()) {
110: throw new PortletException("Invalid channel ID: "
111: + channelIDStr + ".");
112: }
113: String channelName = rs.getString(1);
114: File channelDir = new File(ccfRealBaseDir, channelName
115: .replaceAll(" ", "_"));
116:
117: // If the channel directory does not exist, there is nothing to be done
118: // so just return success
119: if (!channelDir.exists())
120: return "success";
121:
122: String[] oldFiles = channelDir.list();
123: if (oldFiles != null) {
124: for (int i = 0; i < oldFiles.length; i++) {
125: boolean deleteSuccess = new File(channelDir,
126: oldFiles[i]).delete();
127: if (!deleteSuccess)
128: result = "delete-failed";
129: }
130: }
131: boolean deleteSuccess = channelDir.delete();
132: if (!deleteSuccess)
133: result = "delete-failed";
134:
135: } catch (SQLException ex) {
136: throw new PortletException(
137: "SQL exception in getting database connection.", ex);
138: } finally {
139: DBUtil.close(rs);
140: DBUtil.close(st);
141: DBUtil.close(conn);
142: }
143: return result;
144: }
145:
146: }
|