001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.authoring.struts.actions;
020:
021: import java.util.List;
022:
023: import javax.servlet.ServletException;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.struts.action.ActionError;
030: import org.apache.struts.action.ActionErrors;
031: import org.apache.struts.action.ActionForm;
032: import org.apache.struts.action.ActionForward;
033: import org.apache.struts.action.ActionMapping;
034: import org.apache.struts.action.ActionMessage;
035: import org.apache.struts.action.ActionMessages;
036: import org.apache.struts.actions.DispatchAction;
037: import org.apache.roller.business.referrers.RefererManager;
038: import org.apache.roller.business.RollerFactory;
039: import org.apache.roller.pojos.RefererData;
040: import org.apache.roller.pojos.WebsiteData;
041: import org.apache.roller.ui.core.BasePageModel;
042: import org.apache.roller.ui.core.RollerRequest;
043: import org.apache.roller.ui.core.RollerSession;
044: import org.apache.roller.util.cache.CacheManager;
045:
046: /**
047: * Display today's referers.
048: * @struts.action name="refererForm" path="/roller-ui/authoring/referers"
049: * scope="session" parameter="method"
050: *
051: * @struts.action-forward name="referers.page" path=".referers"
052: */
053: public class ReferersAction extends DispatchAction {
054: private static Log mLogger = LogFactory.getFactory().getInstance(
055: ReferersAction.class);
056:
057: public ActionForward unspecified(ActionMapping mapping,
058: ActionForm actionForm, HttpServletRequest request,
059: HttpServletResponse response) throws Exception {
060: return view(mapping, actionForm, request, response);
061: }
062:
063: /**
064: * execute
065: */
066: public ActionForward view(ActionMapping mapping, ActionForm form,
067: HttpServletRequest req, HttpServletResponse res)
068: throws Exception {
069: ActionForward forward = mapping.findForward("referers.page");
070: RollerRequest rreq = RollerRequest.getRollerRequest(req);
071: RollerSession rollerSession = RollerSession
072: .getRollerSession(req);
073: RefererManager refmgr = RollerFactory.getRoller()
074: .getRefererManager();
075: try {
076: if (rreq.getWebsite() != null
077: && rollerSession.isUserAuthorizedToAuthor(rreq
078: .getWebsite())) {
079: BasePageModel pageModel = new BasePageModel(
080: "referers.todaysReferers", req, res, mapping);
081: req.setAttribute("model", pageModel);
082: req.setAttribute("pageHits", new Integer(refmgr
083: .getDayHits(rreq.getWebsite())));
084:
085: req.setAttribute("totalHits", new Integer(refmgr
086: .getTotalHits(rreq.getWebsite())));
087:
088: List refs = refmgr.getTodaysReferers(rreq.getWebsite());
089: req.setAttribute("referers", refs);
090: }
091: } catch (Exception e) {
092: mLogger.error("ERROR in action", e);
093: throw new ServletException(e);
094: }
095:
096: return forward;
097: }
098:
099: public ActionForward reset(ActionMapping mapping, ActionForm form,
100: HttpServletRequest req, HttpServletResponse res)
101: throws Exception {
102: this .servlet.log("ReferersAction.reset()");
103: RollerRequest rreq = RollerRequest.getRollerRequest(req);
104: RollerSession rollerSession = RollerSession
105: .getRollerSession(req);
106: try {
107: if (rreq.getWebsite() != null
108: && rollerSession.isUserAuthorizedToAuthor(rreq
109: .getWebsite())) {
110: RefererManager refmgr = RollerFactory.getRoller()
111: .getRefererManager();
112: WebsiteData website = rreq.getWebsite();
113: refmgr.clearReferrers(website);
114: RollerFactory.getRoller().flush();
115:
116: CacheManager.invalidate(website);
117: }
118: this .servlet
119: .log("ReferersAction.reset(): don't have permission");
120: } catch (Exception e) {
121: mLogger.error("ERROR in action", e);
122: throw new ServletException(e);
123: }
124: return view(mapping, form, req, res);
125: }
126:
127: public ActionForward delete(ActionMapping mapping, ActionForm form,
128: HttpServletRequest req, HttpServletResponse res)
129: throws Exception {
130: //this.servlet.log("ReferersAction.delete()");
131: RollerRequest rreq = RollerRequest.getRollerRequest(req);
132: RollerSession rollerSession = RollerSession
133: .getRollerSession(req);
134: try {
135: if (rreq.getWebsite() != null
136: && rollerSession.isUserAuthorizedToAuthor(rreq
137: .getWebsite())) {
138: RefererManager refmgr = RollerFactory.getRoller()
139: .getRefererManager();
140: WebsiteData website = rreq.getWebsite();
141:
142: RefererData referer = null;
143: String[] deleteIds = req.getParameterValues("id");
144: if (deleteIds != null) {
145: for (int i = 0; i < deleteIds.length; i++) {
146: referer = refmgr.getReferer(deleteIds[i]);
147: refmgr.removeReferer(referer);
148: }
149: RollerFactory.getRoller().flush();
150:
151: CacheManager.invalidate(website);
152:
153: ActionMessages messages = new ActionMessages();
154: messages.add(null, new ActionMessage(
155: "referers.deletedReferers"));
156: saveMessages(req, messages);
157: } else {
158: ActionErrors errors = new ActionErrors();
159: errors.add(null, new ActionError(
160: "referers.noReferersSpecified"));
161: saveErrors(req, errors);
162: }
163: }
164: //this.servlet.log("ReferersAction.delete(): don't have permission");
165: } catch (Exception e) {
166: mLogger.error("ERROR in action", e);
167: throw new ServletException(e);
168: }
169: return view(mapping, form, req, res);
170: }
171: }
|