| 1 16 package org.outerj.daisy.frontend; 17 18 import org.outerj.daisy.frontend.util.*; 19 import org.outerj.daisy.frontend.components.docbasket.DocumentBasketEntry; 20 import org.outerj.daisy.frontend.components.docbasket.DocumentBasketHelper; 21 import org.outerj.daisy.repository.*; 22 import org.outerj.daisy.repository.acl.AclResultInfo; 23 import org.outerj.daisy.repository.comment.CommentVisibility; 24 import org.outerj.daisy.repository.comment.Comment; 25 import org.outerj.daisy.repository.user.User; 26 import org.outerj.daisy.emailnotifier.EmailSubscriptionManager; 27 import org.apache.cocoon.components.flow.apples.StatelessAppleController; 28 import org.apache.cocoon.components.flow.apples.AppleRequest; 29 import org.apache.cocoon.components.flow.apples.AppleResponse; 30 import org.apache.cocoon.environment.Request; 31 import org.apache.cocoon.forms.util.I18nMessage; 32 import org.apache.cocoon.forms.formmodel.Form; 33 import org.apache.cocoon.forms.FormContext; 34 import org.apache.cocoon.xml.SaxBuffer; 35 import org.apache.excalibur.xml.sax.XMLizable; 36 import org.outerx.daisy.x10.SearchResultDocument; 37 38 import java.util.Map ; 39 import java.util.HashMap ; 40 import java.util.Locale ; 41 42 public class DocumentApple extends AbstractDocumentApple implements StatelessAppleController { 43 private String publishType; 44 private Request request; 45 private AppleResponse appleResponse; 46 47 protected void processDocumentRequest(AppleRequest appleRequest, AppleResponse appleResponse) throws Exception { 48 this.request = appleRequest.getCocoonRequest(); 49 this.appleResponse = appleResponse; 50 51 String whatToDo = appleRequest.getSitemapParameter("whatToDo"); 52 publishType = appleRequest.getSitemapParameter("publishType", null); 53 54 if (whatToDo.equals("handleDocumentRequest")) { 55 handleDocumentRequest(); 56 } else if (whatToDo.equals("handleVersionRequest")) { 57 handleVersionRequest(); 58 } else if (whatToDo.equals("showVersionOverview")) { 59 showVersionOverview(); 60 } else if (whatToDo.equals("showReferrers")) { 61 showReferrers(); 62 } else if (whatToDo.equals("showDiff")) { 63 showDiff(); 64 } else { 65 throw new Exception ("Invalid value for whatToDo sitemap parameter."); 66 } 67 } 68 69 protected boolean needsInitialisation() { 70 return true; 71 } 72 73 protected void handleDocumentRequest() throws Exception { 74 Map displayParams = new HashMap (); 75 String showComments = request.getParameter("showComments"); 76 if (showComments != null && showComments.equals("true")) 77 displayParams.put("showComments", "true"); 78 79 String action = request.getParameter("action"); 80 if (action == null) { 81 showDocument(null, displayParams); 82 } else if (action.equals("delete")) { 83 handleDocumentDeleteRequest(); 84 } else if (action.equals("changeOwner")) { 85 handleChangeOwnerRequest(); 86 } else if (action.equals("subscribe") || action.equals("unsubscribe")) { 87 if (request.getMethod().equals("POST")) { 88 User user = getRepository().getUserManager().getUser(getRepository().getUserId(), false); 89 if (!user.isUpdateableByUser()) 93 throw new Exception ("User " + user.getLogin() + " (ID: " + user.getId() + ") is not allowed to subscribe for document notifications."); 94 EmailSubscriptionManager subscriptionManager = (EmailSubscriptionManager)getRepository().getExtension("EmailSubscriptionManager"); 95 XMLizable message; 96 if (action.equals("subscribe")) { 97 subscriptionManager.addDocumentSubscription(new VariantKey(getDocumentId(), getBranchId(), getLanguageId())); 98 message = new I18nMessage("subscription-added"); 99 if (user.getEmail() == null || user.getEmail().length() < 1) { 100 message = new MultiXMLizable(message, new I18nMessage("subscription-missing-email")); 101 } 102 } else { 103 message = new I18nMessage("subscription-deleted"); 104 subscriptionManager.deleteDocumentSubscription(new VariantKey(getDocumentId(), getBranchId(), getLanguageId())); 105 } 106 showDocument(message, displayParams); 107 } else { 108 throw new HttpMethodNotAllowedException(request.getMethod()); 109 } 110 } else if (action.equals("addComment")) { 111 if (request.getMethod().equals("POST")) { 112 113 if (getRepository().getUserLogin().equals("guest")) 115 throw new Exception ("The guest user is not allowed to add comments."); 116 117 String text = request.getParameter("commentText"); 118 String visibilityParam = request.getParameter("commentVisibility"); 119 if (text == null || text.trim().equals("")) { 120 appleResponse.redirectTo(EncodingUtil.encodePath(getMountPoint() + "/" + getSiteConf().getName() + "/" + getRequestedNavigationPath()) + ".html?showComments=true" + getVariantParams() + "#daisycommenteditor"); 122 } else { 123 CommentVisibility visibility; 124 if (visibilityParam == null) { 125 visibility = CommentVisibility.PUBLIC; 126 } else if (visibilityParam.equals("public")) { 127 visibility = CommentVisibility.PUBLIC; 128 } else if (visibilityParam.equals("editors")) { 129 visibility = CommentVisibility.EDITORS; 130 } else if (visibilityParam.equals("private")) { 131 visibility = CommentVisibility.PRIVATE; 132 } else { 133 throw new Exception ("Invalid value for commentVisibility parameter: " + visibilityParam); 134 } 135 Comment newComment = getRepository().getCommentManager().addComment(getDocumentId(), getBranchId(), getLanguageId(), visibility, text); 136 appleResponse.redirectTo(EncodingUtil.encodePath(getMountPoint() + "/" + getSiteConf().getName() + "/" + getRequestedNavigationPath()) + ".html?showComments=true" + getVariantParams() + "#daisycomment" + newComment.getId()); 137 } 138 } else { 139 throw new HttpMethodNotAllowedException(request.getMethod()); 140 } 141 } else if (action.equals("deleteComment")) { 142 if (request.getMethod().equals("POST")) { 143 long commentId = RequestUtil.getLongParameter(request, "commentId"); 144 getRepository().getCommentManager().deleteComment(getDocumentId(), getBranchId(), getLanguageId(), commentId); 145 appleResponse.redirectTo(EncodingUtil.encodePath(getMountPoint() + "/" + getSiteConf().getName() + "/" + getRequestedNavigationPath()) + ".html?showComments=true" + getVariantParams() + "#daisycomments"); 146 } else { 147 throw new HttpMethodNotAllowedException(request.getMethod()); 148 } 149 } else if (action.equals("addToBasket")) { 150 if (request.getMethod().equals("POST")) { 151 DocumentBasketEntry entry = new DocumentBasketEntry(getDocumentId(), getBranch(), getLanguage(), getVersionId(), ""); 152 DocumentBasketHelper.updateDocumentNames(entry, request, getRepository()); 153 DocumentBasketHelper.getDocumentBasket(request, true).appendEntry(entry); 154 showDocument(new I18nMessage("document-in-basket"), displayParams); 155 } else { 156 throw new HttpMethodNotAllowedException(request.getMethod()); 157 } 158 } else { 159 throw new Exception ("Unsupported action parameter value: \"" + action + "\"."); 160 } 161 } 162 163 private void handleDocumentDeleteRequest() throws Exception { 164 String type = request.getParameter("type"); 165 if (type == null) { 166 Document document = getRepository().getDocument(getDocumentId(), getBranchId(), getLanguageId(), true); 167 String query = "select id, branch, language, name where LinksToVariant(" + getDocumentId() + "," + getBranchId() + "," + getLanguageId() + ",1,1) option search_last_version = 'true'"; 168 Repository repository = getRepository(); 169 SearchResultDocument linkingDocs = repository.getQueryManager().performQuery(query, WikiHelper.getLocale(request)); 170 AclResultInfo aclResultInfo = repository.getAccessManager().getAclInfoOnLive(repository.getUserId(), repository.getActiveRoleIds(), getDocumentId(), getBranchId(), getLanguageId()); 171 MultiXMLizable data = new MultiXMLizable( 172 new XmlObjectXMLizable(document.getXml()), 173 new XmlObjectXMLizable(linkingDocs), 174 new XmlObjectXMLizable(aclResultInfo.getXml())); 175 176 Map viewData = new HashMap (); 177 viewData.put("pageContext", new PageContext(getMountPoint(), getSiteConf(), getRepository(), getLayoutType(), getSkin(), getContext())); 178 viewData.put("pageXml", data); 179 viewData.put("navigationPath", getRequestedNavigationPath()); 180 viewData.put("variantParams", getVariantParams()); 181 viewData.put("variantQueryString", getVariantQueryString()); 182 appleResponse.sendPage("DeleteDocumentPipe", viewData); 183 } else { 184 if (request.getMethod().equals("POST")) { 185 if (type.equals("variant-permanent")) { 186 getRepository().deleteVariant(getDocumentId(), getBranchId(), getLanguageId()); 187 } else if (type.equals("permanent")) { 188 getRepository().deleteDocument(getDocumentId()); 189 } else if (type.equals("retire")) { 190 Document document = getRepository().getDocument(getDocumentId(), getBranchId(), getLanguageId(), true); 191 document.setRetired(true); 192 document.save(false); 193 } else { 194 throw new Exception ("Unsupported value for type parameter: " + type); 195 } 196 appleResponse.redirectTo(EncodingUtil.encodePath(getMountPoint() + "/" + getSiteConf().getName() + "/" + getSiteConf().getHomePage())); 197 } else { 198 throw new HttpMethodNotAllowedException(request.getMethod()); 199 } 200 } 201 } 202 203 private void handleChangeOwnerRequest() throws Exception { 204 Form form = FormHelper.createForm(getServiceManager(), "resources/form/changeowner_definition.xml"); 205 Locale locale = WikiHelper.getLocale(request); 206 207 if (request.getMethod().equals("POST")) { 208 FormContext formContext = new FormContext(request, locale); 209 boolean finished = form.process(formContext); 210 if (finished) { 211 long newOwnerId = ((Long )form.getChild("newOwnerId").getValue()).longValue(); 212 Document document = getRepository().getDocument(getDocumentId(), getBranchId(), getLanguageId(), true); 213 document.setOwner(newOwnerId); 214 document.save(); 215 appleResponse.redirectTo(EncodingUtil.encodePath(getMountPoint() + "/" + getSiteConf().getName() + "/" + getRequestedNavigationPath() + ".html") + getVariantQueryString()); 216 return; 217 } 218 } 219 220 Document document = getRepository().getDocument(getDocumentId(), getBranchId(), getLanguageId(), true); 221 Map viewData = new HashMap (); 222 viewData.put("pageContext", new PageContext(getMountPoint(), getSiteConf(), getRepository(), getLayoutType(), getSkin(), getContext())); 223 viewData.put("document", document); 224 viewData.put("documentPath", getMountPoint() + "/" + getSiteConf().getName() + "/" + getRequestedNavigationPath()); 225 viewData.put("CocoonFormsInstance", form); 226 viewData.put("variantParams", getVariantParams()); 227 viewData.put("variantQueryString", getVariantQueryString()); 228 viewData.put("locale", locale); 229 appleResponse.sendPage("Form-changeowner-Pipe", viewData); 230 } 231 232 235 protected void showDocument(XMLizable message, Map displayParams) throws Exception { 236 String navigationType = request.getParameter("navigationType"); 238 String defaultLayoutType = navigationType != null ? navigationType : getLayoutType(); 239 String layoutType = "xslfo".equals(publishType) ? "plain" : defaultLayoutType; 241 PageContext pageContext = new PageContext(getMountPoint(), getSiteConf(), getRepository(), layoutType, getSkin(), getContext()); 242 Map viewData = new HashMap (); 243 viewData.put("pageContext", pageContext); 244 viewData.put("variantParams", getVariantParams()); 245 viewData.put("variantQueryString", getVariantQueryString()); 246 viewData.put("documentId", String.valueOf(getDocumentId())); 247 viewData.put("branch", String.valueOf(getBranchId())); 248 viewData.put("language", String.valueOf(getLanguageId())); 249 viewData.put("version", getVersionName(getVersionId())); 250 viewData.put("localeAsString", request.getAttribute("localeAsString")); 251 viewData.put("activePath", getRequestedNavigationPath()); 252 if (message != null) 253 viewData.put("pageMessage", message); 254 if (displayParams != null) 255 viewData.put("displayParams", displayParams.entrySet()); 256 257 SaxBuffer publisherResponse; 258 try { 259 publisherResponse = performPublisherRequest("documentpage", viewData); 260 } catch (DocumentVariantNotFoundException e) { 261 showVariantNotFoundPage(getDocumentId()); 262 return; 263 } 264 265 viewData.put("publisherResponse", publisherResponse); 266 267 appleResponse.sendPage(publishType + "-DocumentPipe", viewData); 268 } 269 270 private String getVersionName(long versionId) { 271 if (versionId == -3) 272 return "default"; 273 if (versionId == -1) 274 return "live"; 275 else if (versionId == -2) 276 return "last"; 277 else 278 return String.valueOf(versionId); 279 } 280 281 protected void showVersionOverview() throws Exception { 282 Map viewData = new HashMap (); 283 viewData.put("pageContext", new PageContext(getMountPoint(), getSiteConf(), getRepository(), getLayoutType(), getSkin(), getContext())); 284 viewData.put("variantParams", getVariantParams()); 285 viewData.put("variantQueryString", getVariantQueryString()); 286 viewData.put("documentId", String.valueOf(getDocumentId())); 287 viewData.put("branch", String.valueOf(getBranchId())); 288 viewData.put("language", String.valueOf(getLanguageId())); 289 viewData.put("localeAsString", request.getAttribute("localeAsString")); 290 viewData.put("activePath", getRequestedNavigationPath()); 291 292 SaxBuffer publisherResponse = performPublisherRequest("versionspage", viewData); 293 viewData.put("pageXml", publisherResponse); 294 295 appleResponse.sendPage("VersionOverviewPipe", viewData); 296 } 297 298 protected void handleVersionRequest() throws Exception { 299 String action = request.getParameter("action"); 300 301 if (action == null) { 302 showDocument(null, null); 303 } else { 304 if (request.getMethod().equals("POST")) { 305 if (action.equals("changeState")) { 306 String newStateString = request.getParameter("newState"); 307 if (newStateString == null) 308 throw new Exception ("Missing request parameter \"newState\"."); 309 VersionState newState = VersionState.fromString(newStateString); 310 Document document = getRepository().getDocument(getDocumentId(), getBranchId(), getLanguageId(), true); 311 Version version; 312 if (getVersionId() == -1) 313 version = document.getLiveVersion(); 314 else if (getVersionId() == -2) 315 version = document.getLastVersion(); 316 else 317 version = document.getVersion(getVersionId()); 318 version.setState(newState); 319 320 String returnTo = request.getParameter("returnTo"); 321 if (returnTo != null) 322 appleResponse.redirectTo(returnTo); 323 else 324 appleResponse.redirectTo(EncodingUtil.encodePath(getMountPoint() + "/" + getSiteConf().getName() + "/" + getRequestedNavigationPath() + "/versions.html") + getVariantQueryString()); 325 } else { 326 throw new Exception ("Invalid value for action parameter: " + action); 327 } 328 } else { 329 throw new HttpMethodNotAllowedException(request.getMethod()); 330 } 331 } 332 } 333 334 protected void showReferrers() throws Exception { 335 boolean linksInLastVersion = RequestUtil.getBooleanParameter(request, "linksInLastVersion", false); 336 boolean linksInLiveVersion = RequestUtil.getBooleanParameter(request, "linksInLiveVersion", false); 337 338 if (!linksInLastVersion && !linksInLiveVersion) { 339 linksInLastVersion = true; 340 linksInLiveVersion = true; 341 } 342 343 StringBuffer query = new StringBuffer (); 344 query.append("select id, branch, language, name where LinksToVariant(").append(getDocumentId()); 345 query.append(',').append(getBranchId()).append(',').append(getLanguageId()).append(','); 346 query.append(linksInLastVersion ? '1' : '0'); 347 query.append(','); 348 query.append(linksInLiveVersion ? '1' : '0'); 349 query.append(") option search_last_version = 'true'"); 350 351 Map viewData = new HashMap (); 352 viewData.put("pageContext", new PageContext(getMountPoint(), getSiteConf(), getRepository(), getLayoutType(), getSkin(), getContext())); 353 viewData.put("variantParams", getVariantParams()); 354 viewData.put("variantQueryString", getVariantQueryString()); 355 viewData.put("referrersQuery", query.toString()); 356 viewData.put("linksInLiveVersion", String.valueOf(linksInLiveVersion)); 357 viewData.put("linksInLastVersion", String.valueOf(linksInLastVersion)); 358 viewData.put("documentId", String.valueOf(getDocumentId())); 359 viewData.put("branch", String.valueOf(getBranchId())); 360 viewData.put("language", String.valueOf(getLanguageId())); 361 viewData.put("localeAsString", request.getAttribute("localeAsString")); 362 viewData.put("activePath", getRequestedNavigationPath()); 363 364 SaxBuffer publisherResponse = performPublisherRequest("referrerspage", viewData); 365 viewData.put("publisherResponse", publisherResponse); 366 367 appleResponse.sendPage("ReferrersPipe", viewData); 368 } 369 370 protected void showDiff() throws Exception { 371 long otherVersionId = RequestUtil.getLongParameter(request, "otherVersion"); 372 long otherBranchId = RequestUtil.getBranchId(request.getParameter("otherBranch"), getBranchId(), getRepository()); 373 long otherLanguageId = RequestUtil.getLanguageId(request.getParameter("otherLanguage"), getLanguageId(), getRepository()); 374 375 Map viewData = new HashMap (); 376 viewData.put("pageContext", new PageContext(getMountPoint(), getSiteConf(), getRepository(), getLayoutType(), getSkin(), getContext())); 377 viewData.put("variantParams", getVariantParams()); 378 viewData.put("variantQueryString", getVariantQueryString()); 379 viewData.put("documentId", String.valueOf(getDocumentId())); 380 viewData.put("branch", String.valueOf(getBranchId())); 381 viewData.put("language", String.valueOf(getLanguageId())); 382 viewData.put("version", getVersionName(getVersionId())); 383 viewData.put("localeAsString", request.getAttribute("localeAsString")); 384 viewData.put("activePath", getRequestedNavigationPath()); 385 viewData.put("otherBranch", String.valueOf(otherBranchId)); 386 viewData.put("otherLanguage", String.valueOf(otherLanguageId)); 387 viewData.put("otherVersionId", String.valueOf(otherVersionId)); 388 389 SaxBuffer publisherResponse = performPublisherRequest("diffpage", viewData); 390 viewData.put("pageXml", publisherResponse); 391 392 appleResponse.sendPage("DiffPipe", viewData); 393 } 394 395 private void showVariantNotFoundPage(long documentId) throws Exception { 396 AvailableVariants variants = getRepository().getAvailableVariants(documentId); 397 398 Map viewData = new HashMap (); 399 viewData.put("pageXml", new MultiXMLizable(new XmlObjectXMLizable(variants.getXml(true)), new TaggedMessage("documentId", String.valueOf(documentId)))); 400 viewData.put("pageContext", new PageContext(getMountPoint(), getSiteConf(), getRepository(), getLayoutType(), getSkin(), getContext())); 401 appleResponse.sendPage("AlternativeVariantsPipe", viewData); 402 } 403 404 private SaxBuffer performPublisherRequest(String name, Map params) throws Exception { 405 String pipe = name + "_pubreq.xml"; 406 WikiPublisherHelper wikiPublisherHelper = new WikiPublisherHelper(request, getContext(), getServiceManager()); 407 return wikiPublisherHelper.performPublisherRequest(pipe, params, publishType); 408 } 409 } 410 | Popular Tags |