1 13 package info.magnolia.cms.gui.control; 14 15 import info.magnolia.cms.beans.config.ContentRepository; 16 import info.magnolia.cms.beans.runtime.Document; 17 import info.magnolia.cms.beans.runtime.MultipartForm; 18 import info.magnolia.cms.core.Content; 19 import info.magnolia.cms.core.HierarchyManager; 20 import info.magnolia.cms.core.ItemType; 21 import info.magnolia.cms.core.NodeData; 22 import info.magnolia.cms.core.Path; 23 import info.magnolia.cms.gui.dialog.DialogSuper; 24 import info.magnolia.cms.gui.misc.FileProperties; 25 import info.magnolia.cms.security.AccessDeniedException; 26 import info.magnolia.cms.security.Digester; 27 import info.magnolia.cms.security.SessionAccessControl; 28 import info.magnolia.cms.util.LinkUtil; 29 30 import java.util.Calendar ; 31 import java.util.GregorianCalendar ; 32 33 import javax.jcr.PathNotFoundException; 34 import javax.jcr.PropertyType; 35 import javax.jcr.RepositoryException; 36 import javax.jcr.Value; 37 import javax.jcr.ValueFactory; 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpSession ; 40 41 import org.apache.commons.codec.binary.Base64; 42 import org.apache.commons.lang.BooleanUtils; 43 import org.apache.commons.lang.StringUtils; 44 import org.apache.commons.lang.exception.NestableRuntimeException; 45 import org.apache.log4j.Logger; 46 47 48 78 public class Save extends ControlSuper { 79 80 83 private static Logger log = Logger.getLogger(Save.class); 84 85 88 private MultipartForm form; 89 90 93 private boolean create; 94 95 private ItemType creationItemType = ItemType.CONTENT; 96 97 100 private String repository = ContentRepository.WEBSITE; 101 102 105 public Save() { 106 } 107 108 113 public Save(MultipartForm form, HttpServletRequest request) { 114 this.setForm(form); 115 this.setRequest(request); 116 this.setPath(form.getParameter("mgnlPath")); this.setNodeCollectionName(form.getParameter("mgnlNodeCollection")); this.setNodeName(form.getParameter("mgnlNode")); this.setParagraph(form.getParameter("mgnlParagraph")); this.setRepository(form.getParameter("mgnlRepository")); } 122 123 126 public void save() { 127 String [] saveInfos = getForm().getParameterValues("mgnlSaveInfo"); String nodeCollectionName = this.getNodeCollectionName(null); 129 String nodeName = this.getNodeName(null); 130 String path = this.getPath(); 131 HttpServletRequest request = this.getRequest(); 132 133 HierarchyManager hm = SessionAccessControl.getHierarchyManager(request, this.getRepository()); 134 try { 135 Content page = null; 136 try { 137 page = hm.getContent(path); 138 } 139 catch (RepositoryException e) { 140 if (isCreate()) { 141 String parentPath = StringUtils.substringBeforeLast(path, "/"); String label = StringUtils.substringAfterLast(path, "/"); if (StringUtils.isEmpty(parentPath)) { 144 page = hm.getRoot(); 145 } 146 else { 147 page = hm.getContent(parentPath); 148 } 149 page = page.createContent(label, creationItemType); 150 } 151 else { 152 log.error("tried to save a not existing node. use create = true to force creation"); } 154 } 155 156 Content nodeCollection = null; 158 if (nodeCollectionName != null) { 159 try { 160 nodeCollection = page.getContent(nodeCollectionName); 161 } 162 catch (RepositoryException re) { 163 nodeCollection = page.createContent(nodeCollectionName, ItemType.CONTENTNODE); 165 if (log.isDebugEnabled()) { 166 log.debug("Create - " + nodeCollection.getHandle()); } 168 } 169 } 170 else { 171 nodeCollection = page; 172 } 173 174 Content node = null; 176 if (nodeName != null) { 177 try { 178 node = nodeCollection.getContent(nodeName); 179 } 180 catch (RepositoryException re) { 181 if (nodeName.equals("mgnlNew")) { nodeName = Path.getUniqueLabel(hm, nodeCollection.getHandle(), "0"); } 185 node = nodeCollection.createContent(nodeName, ItemType.CONTENTNODE); 186 node.createNodeData("paragraph").setValue(this.getParagraph()); node.getMetaData().setSequencePosition(); 188 } 189 } 190 else { 191 node = nodeCollection; 192 } 193 node.updateMetaData(request); 195 page.updateMetaData(request); 196 for (int i = 0; i < saveInfos.length; i++) { 198 String saveInfo = saveInfos[i]; 199 processSaveInfo(node, saveInfo); 200 } 201 if (log.isDebugEnabled()) { 202 log.debug("Saving - " + path); } 204 hm.save(); 205 } 206 catch (RepositoryException re) { 207 log.error(re.getMessage(), re); 208 } 209 this.removeSessionAttributes(); 210 } 211 212 220 private void processSaveInfo(Content node, String saveInfo) throws PathNotFoundException, RepositoryException, 221 AccessDeniedException { 222 223 String name; 224 int type = type = PropertyType.STRING; 225 int valueType = ControlSuper.VALUETYPE_SINGLE; 226 int isRichEditValue = 0; 227 int encoding = ControlSuper.ENCODING_NO; 228 String [] values = {StringUtils.EMPTY}; 229 if (StringUtils.contains(saveInfo, ',')) { 230 String [] info = saveInfo.split(","); name = info[0]; 232 if (info.length >= 2) { 233 type = PropertyType.valueFromName(info[1]); 234 } 235 if (info.length >= 3) { 236 valueType = Integer.valueOf(info[2]).intValue(); 237 } 238 if (info.length >= 4) { 239 isRichEditValue = Integer.valueOf(info[3]).intValue(); 240 } 241 if (info.length >= 5) { 242 encoding = Integer.valueOf(info[4]).intValue(); 243 } 244 } 245 else { 246 name = saveInfo; 247 } 248 if (type == PropertyType.BINARY) { 249 processBinary(node, name); 250 } 251 else { 252 values = getForm().getParameterValues(name); 253 if (valueType == ControlSuper.VALUETYPE_MULTIPLE) { 254 processMultiple(node, name, type, values); 255 } 256 else { 257 processCommon(node, name, type, isRichEditValue, encoding, values); 258 } 259 } 260 } 261 262 274 private void processCommon(Content node, String name, int type, int isRichEditValue, int encoding, String [] values) 275 throws PathNotFoundException, RepositoryException, AccessDeniedException { 276 String valueStr = StringUtils.EMPTY; 277 if (values != null) { 278 valueStr = values[0]; } 280 NodeData data = node.getNodeData(name); 282 if (isRichEditValue != ControlSuper.RICHEDIT_NONE) { 283 valueStr = this.getRichEditValueStr(valueStr, isRichEditValue); 284 } 285 boolean remove = false; 287 boolean write = false; 288 if (encoding == ControlSuper.ENCODING_BASE64) { 289 if (StringUtils.isNotBlank(valueStr)) { 290 valueStr = new String (Base64.encodeBase64(valueStr.getBytes())); 291 write = true; 292 } 293 } 294 else if (encoding == ControlSuper.ENCODING_UNIX) { 295 if (StringUtils.isNotEmpty(valueStr)) { 296 valueStr = Digester.getSHA1Hex(valueStr); 297 write = true; 298 } 299 } 300 else { 301 if (values == null || StringUtils.isEmpty(valueStr)) { 303 remove = true; 304 } 305 else { 306 write = true; 307 } 308 } 309 if (remove) { 310 if (data.isExist()) { 312 node.deleteNodeData(name); 313 } 314 } 315 else if (write) { 316 Value value = this.getValue(valueStr, type); 317 if (value != null) { 318 if (data.isExist()) { 319 data.setValue(value); 320 } 321 else { 322 node.createNodeData(name, value); 323 } 324 } 325 } 326 } 327 328 337 private void processMultiple(Content node, String name, int type, String [] values) throws RepositoryException, 338 PathNotFoundException, AccessDeniedException { 339 try { 341 node.delete(name); 342 } 343 catch (PathNotFoundException e) { 344 log.debug("Exception caught: " + e.getMessage(), e); } 346 if (values != null && values.length != 0) { 347 Content multiNode = node.createContent(name, ItemType.CONTENTNODE); 348 try { 349 multiNode.deleteNodeData("creationdate"); } 352 catch (RepositoryException re) { 353 log.debug("Exception caught: " + re.getMessage(), re); } 355 for (int j = 0; j < values.length; j++) { 356 String valueStr = values[j]; 357 Value value = this.getValue(valueStr, type); 358 multiNode.createNodeData(Integer.toString(j)).setValue(value); 359 } 360 } 361 } 362 363 371 private void processBinary(Content node, String name) throws PathNotFoundException, RepositoryException, 372 AccessDeniedException { 373 Document doc = getForm().getDocument(name); 374 if (doc == null && getForm().getParameter(name + "_" + File.REMOVE) != null) { try { 376 node.delete(name + "_" + FileProperties.PROPERTIES_CONTENTNODE); } 378 catch (RepositoryException re) { 379 log.debug("Exception caught: " + re.getMessage(), re); } 381 try { 382 node.deleteNodeData(name); 383 } 384 catch (RepositoryException re) { 385 log.debug("Exception caught: " + re.getMessage(), re); } 387 388 } 389 else { 390 Content propNode = null; 391 try { 392 propNode = node.getContent(name + "_" + FileProperties.PROPERTIES_CONTENTNODE); } 394 catch (RepositoryException re) { 395 try { 396 if (doc != null) { 397 propNode = node.createContent(name + "_" + FileProperties.PROPERTIES_CONTENTNODE, ItemType.CONTENTNODE); 399 } 400 } 401 catch (RepositoryException re2) { 402 log.debug("Exception caught: " + re2.getMessage(), re2); } 404 } 405 if (doc != null) { 406 NodeData data = node.getNodeData(name); 407 if (!data.isExist()) { 408 data = node.createNodeData(name); 409 if (log.isDebugEnabled()) { 410 log.debug("creating under - " + node.getHandle()); log.debug("creating node data for binary store - " + name); } 413 } 414 data.setValue(doc.getStream()); 415 log.debug("Node data updated"); } 417 if (propNode != null) { 418 NodeData propData; 419 String fileName = getForm().getParameter(name + "_" + FileProperties.PROPERTY_FILENAME); if (fileName == null || fileName.equals(StringUtils.EMPTY)) { 421 fileName = doc.getFileName(); 422 } 423 propData = propNode.getNodeData(FileProperties.PROPERTY_FILENAME); 424 if (!propData.isExist()) { 425 propData = propNode.createNodeData(FileProperties.PROPERTY_FILENAME); 426 } 427 propData.setValue(fileName); 428 if (doc != null) { 429 propData = propNode.getNodeData(FileProperties.PROPERTY_CONTENTTYPE); 430 if (!propData.isExist()) { 431 propData = propNode.createNodeData(FileProperties.PROPERTY_CONTENTTYPE); 432 } 433 propData.setValue(doc.getType()); 434 propData = propNode.getNodeData(FileProperties.PROPERTY_SIZE); 435 if (!propData.isExist()) { 436 propData = propNode.createNodeData(FileProperties.PROPERTY_SIZE); 437 } 438 propData.setValue(doc.getLength()); 439 propData = propNode.getNodeData(FileProperties.PROPERTY_EXTENSION); 440 if (!propData.isExist()) { 441 propData = propNode.createNodeData(FileProperties.PROPERTY_EXTENSION); 442 } 443 propData.setValue(doc.getExtension()); 444 String template = getForm().getParameter(name + "_" + FileProperties.PROPERTY_TEMPLATE); if (StringUtils.isNotEmpty(template)) { 446 propData = propNode.getNodeData(FileProperties.PROPERTY_TEMPLATE); 447 if (!propData.isExist()) { 448 propData = propNode.createNodeData(FileProperties.PROPERTY_TEMPLATE); 449 } 450 propData.setValue(template); 451 } 452 else { 453 try { 454 propNode.deleteNodeData(FileProperties.PROPERTY_TEMPLATE); 455 } 456 catch (PathNotFoundException e) { 457 log.debug("Exception caught: " + e.getMessage(), e); } 459 } 460 doc.delete(); 461 } 462 } 463 } 464 } 465 466 public void removeSessionAttributes() { 467 HttpSession session = this.getRequest().getSession(); 468 MultipartForm form = getForm(); 469 String [] toRemove = form.getParameterValues(DialogSuper.SESSION_ATTRIBUTENAME_DIALOGOBJECT_REMOVE); 470 if (toRemove != null) { 471 for (int i = 0; i < toRemove.length; i++) { 472 session.removeAttribute(toRemove[i]); 473 } 475 } 476 } 477 478 public Value getValue(String s) { 479 return this.getValue(s, PropertyType.STRING); 480 } 481 482 public Value getValue(long l) { 483 HierarchyManager hm = SessionAccessControl.getHierarchyManager(this.getRequest(), this.getRepository()); 484 ValueFactory valueFactory; 485 try { 486 valueFactory = hm.getWorkspace().getSession().getValueFactory(); 487 } 488 catch (RepositoryException e) { 489 throw new NestableRuntimeException(e); 490 } 491 return valueFactory.createValue(l); 492 } 493 494 public Value getValue(String valueStr, int type) { 495 496 ValueFactory valueFactory = null; 497 498 HierarchyManager hm = SessionAccessControl.getHierarchyManager(this.getRequest(), this.getRepository()); 499 try { 500 valueFactory = hm.getWorkspace().getSession().getValueFactory(); 501 } 502 catch (RepositoryException e) { 503 throw new NestableRuntimeException(e); 504 } 505 506 Value value = null; 507 if (type == PropertyType.STRING) { 508 value = valueFactory.createValue(valueStr); 509 } 510 else if (type == PropertyType.BOOLEAN) { 511 value = valueFactory.createValue(BooleanUtils.toBoolean(valueStr)); 512 } 513 else if (type == PropertyType.DOUBLE) { 514 try { 515 value = valueFactory.createValue(Double.parseDouble(valueStr)); 516 } 517 catch (NumberFormatException e) { 518 value = valueFactory.createValue(0d); 519 } 520 } 521 else if (type == PropertyType.LONG) { 522 try { 523 value = valueFactory.createValue(Long.parseLong(valueStr)); 524 } 525 catch (NumberFormatException e) { 526 value = valueFactory.createValue(0L); 527 } 528 } 529 else if (type == PropertyType.DATE) { 530 try { 531 Calendar date = new GregorianCalendar (); 532 try { 533 String newDateAndTime = valueStr; 534 String [] dateAndTimeTokens = newDateAndTime.split("T"); String newDate = dateAndTimeTokens[0]; 536 String [] dateTokens = newDate.split("-"); int hour = 0; 538 int minute = 0; 539 int second = 0; 540 int year = Integer.parseInt(dateTokens[0]); 541 int month = Integer.parseInt(dateTokens[1]) - 1; 542 int day = Integer.parseInt(dateTokens[2]); 543 if (dateAndTimeTokens.length > 1) { 544 String newTime = dateAndTimeTokens[1]; 545 String [] timeTokens = newTime.split(":"); hour = Integer.parseInt(timeTokens[0]); 547 minute = Integer.parseInt(timeTokens[1]); 548 second = Integer.parseInt(timeTokens[2]); 549 } 550 date.set(year, month, day, hour, minute, second); 551 } 552 catch (Exception e) { 554 } 556 value = value = valueFactory.createValue(date); 557 } 558 catch (Exception e) { 559 log.debug("Exception caught: " + e.getMessage(), e); } 561 } 562 return value; 563 } 564 565 571 protected String getRichEditValueStr(String value, int isRichEditValue) { 572 573 String valueStr = LinkUtil.convertAbsoluteLinksToUUIDs(value); 575 switch (isRichEditValue) { 576 case ControlSuper.RICHEDIT_KUPU : 577 case ControlSuper.RICHEDIT_FCK : 578 valueStr = StringUtils.replace(valueStr, "\r\n", " "); valueStr = StringUtils.replace(valueStr, "\n", " "); 581 valueStr = StringUtils.replace(valueStr, "</br>", StringUtils.EMPTY); valueStr = StringUtils.replace(valueStr, "<P><BR>", "<P>"); 585 valueStr = StringUtils.replace(valueStr, "<br>", "\n "); valueStr = StringUtils.replace(valueStr, "<BR>", "\n "); valueStr = StringUtils.replace(valueStr, "<br/>", "\n "); 589 valueStr = replacePByBr(valueStr, "p"); 592 break; 596 default : 597 break; 598 } 599 return valueStr; 600 601 } 602 603 608 protected static String replacePByBr(final String value, String tagName) { 609 610 if (StringUtils.isBlank(value)) { 611 return value; 612 } 613 614 String fixedValue = value; 615 616 String pre = "<" + tagName + ">"; String post = "</" + tagName + ">"; 619 if (fixedValue.endsWith(post)) { 621 fixedValue = StringUtils.substringBeforeLast(fixedValue, post); 622 } 623 624 fixedValue = StringUtils.replace(fixedValue, pre + " " + post, "\n "); fixedValue = StringUtils.replace(fixedValue, pre, StringUtils.EMPTY); 626 fixedValue = StringUtils.replace(fixedValue, post, "\n\n "); 628 if (!tagName.equals(tagName.toUpperCase())) { 629 fixedValue = replacePByBr(fixedValue, tagName.toUpperCase()); 630 } 631 return fixedValue; 632 } 633 634 public boolean isCreate() { 635 return create; 636 } 637 638 public void setCreate(boolean create) { 639 this.create = create; 640 } 641 642 public ItemType getCreationItemType() { 643 return creationItemType; 644 } 645 646 public void setCreationItemType(ItemType creationItemType) { 647 this.creationItemType = creationItemType; 648 } 649 650 653 protected MultipartForm getForm() { 654 return form; 655 } 656 657 661 protected void setForm(MultipartForm form) { 662 this.form = form; 663 } 664 665 669 protected void setRepository(String repository) { 670 this.repository = repository; 671 } 672 673 677 protected String getRepository() { 678 return repository; 679 } 680 681 } | Popular Tags |