Пример #1
0
 // this reproduces the logic found in the cms out tag.
 @Override
 public String getAsString() throws TemplateModelException {
   String handle = binaryNodeData.getHandle();
   String filename = binaryNodeData.getAttribute(FileProperties.PROPERTY_FILENAME);
   String ext = binaryNodeData.getAttribute(FileProperties.PROPERTY_EXTENSION);
   return handle + "/" + filename + ((StringUtils.isEmpty(ext)) ? "" : "." + ext);
 }
  public String renameNode(String newLabel)
      throws AccessDeniedException, ExchangeException, PathNotFoundException, RepositoryException {
    String returnValue;
    String parentPath = StringUtils.substringBeforeLast(this.getPath(), "/"); // $NON-NLS-1$
    newLabel = Path.getValidatedLabel(newLabel);

    // don't rename if it uses the same name as the current
    if (this.getPath().endsWith("/" + newLabel)) {
      return newLabel;
    }

    String dest = parentPath + "/" + newLabel; // $NON-NLS-1$
    if (getHierarchyManager().isExist(dest)) {
      newLabel = Path.getUniqueLabel(getHierarchyManager(), parentPath, newLabel);
      dest = parentPath + "/" + newLabel; // $NON-NLS-1$
    }
    this.deActivateNode(this.getPath());

    if (log.isInfoEnabled()) {
      log.info("Moving node from " + this.getPath() + " to " + dest); // $NON-NLS-1$ //$NON-NLS-2$
    }
    if (getHierarchyManager().isNodeData(this.getPath())) {
      Content parentPage = getHierarchyManager().getContent(parentPath);
      NodeData newNodeData = parentPage.createNodeData(newLabel);
      NodeData existingNodeData = getHierarchyManager().getNodeData(this.getPath());
      newNodeData.setValue(existingNodeData.getString());
      existingNodeData.delete();
      dest = parentPath;
    } else {
      // we can't rename a node. we must move
      // we must place the node at the same position
      Content current = getHierarchyManager().getContent(this.getPath());
      Content parent = current.getParent();
      String placedBefore = null;
      for (Iterator iter = parent.getChildren(current.getNodeTypeName()).iterator();
          iter.hasNext(); ) {
        Content child = (Content) iter.next();
        if (child.getHandle().equals(this.getPath())) {
          if (iter.hasNext()) {
            child = (Content) iter.next();
            placedBefore = child.getName();
          }
        }
      }

      getHierarchyManager().moveTo(this.getPath(), dest);

      // now set at the same place as before
      if (placedBefore != null) {
        parent.orderBefore(newLabel, placedBefore);
      }
    }

    Content newPage = getHierarchyManager().getContent(dest);
    returnValue = newLabel;
    newPage.updateMetaData();
    newPage.save();

    return returnValue;
  }
Пример #3
0
  @Override
  public TemplateModel get(String key) throws TemplateModelException {
    Object result = null;

    if (key.startsWith("@")) {
      if (key.equals("@handle")) {
        result = binaryNodeData.getHandle();
      }
    } else if (key.equals(FileProperties.CONTENT_TYPE)) {
      result = binaryNodeData.getAttribute(FileProperties.PROPERTY_CONTENTTYPE);
    } else if (key.equals(FileProperties.NAME)) {
      String filename = binaryNodeData.getAttribute(FileProperties.PROPERTY_FILENAME);
      String ext = binaryNodeData.getAttribute(FileProperties.PROPERTY_EXTENSION);
      result = filename + ((StringUtils.isEmpty(ext)) ? "" : "." + ext);
    } else if (key.equals(FileProperties.PROPERTY_FILENAME)) {
      result = binaryNodeData.getAttribute(FileProperties.PROPERTY_FILENAME);
    } else if (key.equals(FileProperties.PROPERTY_EXTENSION)) {
      result = binaryNodeData.getAttribute(FileProperties.PROPERTY_EXTENSION);
    } else if (key.equals(FileProperties.PROPERTY_LASTMODIFIED)) {
      try {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        result = format.parse(binaryNodeData.getAttribute(FileProperties.PROPERTY_LASTMODIFIED));
      } catch (ParseException e) {
        // do nothing.
      }
    } else {
      result = binaryNodeData.getAttribute(key);
    }
    return wrapper.wrap(result);
  }
Пример #4
0
  /**
   * Test method for {@link info.magnolia.nodebuilder.ContentOps#setBinaryNodeData(java.lang.String,
   * java.lang.String, long, java.io.InputStream)} .
   */
  @Test
  public void testSetBinaryNodeData() throws IOException {
    MockContent content = new MockContent(NEW_CONTENT_NAME);
    byte[] bytes = {'C', 'O', 'N', 'T', 'E', 'N', 'T'};

    ContentOps.setBinaryNodeData(
            NODEDATA_NAME, "test.jpg", bytes.length, new ByteArrayInputStream(bytes))
        .exec(content, ERROR_HANDLER);

    NodeData nodeData = content.getNodeData(NODEDATA_NAME);

    assertEquals("test", nodeData.getAttribute(FileProperties.PROPERTY_FILENAME));
    assertEquals("jpg", nodeData.getAttribute(FileProperties.PROPERTY_EXTENSION));
    assertEquals(String.valueOf(bytes.length), nodeData.getAttribute(FileProperties.PROPERTY_SIZE));
    InputStream stream = nodeData.getStream();
    assertEquals(new String(bytes), IOUtils.toString(stream));
  }
Пример #5
0
 @Override
 public TemplateCollectionModel keys() throws TemplateModelException {
   Iterator<String> result = null;
   try {
     result = binaryNodeData.getAttributeNames().iterator();
   } catch (RepositoryException e) {
     // don't care
   }
   return (TemplateCollectionModel) wrapper.wrap(result);
 }
Пример #6
0
  /**
   * Data is fetch into the repository to get the different parameters of the email
   *
   * @param id the id to find under the template section of the repository
   * @return a new <code>MgnlMail</code> instance, with the template set
   * @throws Exception if fails
   */
  public MgnlEmail getEmailFromTemplate(String id, Map map) throws Exception {
    if (id == null) {
      return new SimpleEmail(getSession());
    }
    HierarchyManager hm = ContentRepository.getHierarchyManager(ContentRepository.CONFIG);
    String nodeTemplatePath = templatePath + "/" + id;
    if (!hm.isExist(nodeTemplatePath)) {
      throw new MailException("Template:[" + id + "] configuration was not found in repository");
    }

    Content node = hm.getContent(nodeTemplatePath);

    // type
    NodeData typeNode = node.getNodeData(MAIL_TYPE);
    String type = typeNode.getValue().getString();

    MgnlEmail mail = getEmailFromType(type);

    // body
    NodeData bodyNode = node.getNodeData(MAIL_BODY);
    String body = bodyNode.getValue().getString();
    mail.setBodyFromResourceFile(body, map);

    // from
    NodeData fromNode = node.getNodeData(MAIL_FROM);
    String from = fromNode.getValue().getString();
    mail.setFrom(from);

    // subject
    NodeData subjectNode = node.getNodeData(MAIL_SUBJECT);
    String subject = subjectNode.getValue().getString();
    mail.setSubject(subject);

    String attachNodePath = node.getHandle() + "/" + MAIL_ATTACHMENT;
    if (hm.isExist(attachNodePath)) {
      Content attachments = hm.getContent(attachNodePath);
      Collection atts = attachments.getChildren();
      Iterator iter = atts.iterator();
      while (iter.hasNext()) {
        Content att = (Content) iter.next();
        String cid = att.getNodeData("cid").getString();
        String url = att.getNodeData("url").getString();
        MailAttachment a = new MailAttachment(new URL(url), cid);
        mail.addAttachment(a);
      }
    }

    // parameters
    mail.setParameters(map);

    return mail;
  }
Пример #7
0
  @Override
  public int size() throws TemplateModelException {
    int result = 0;

    try {
      result = binaryNodeData.getAttributeNames().size();
    } catch (RepositoryException e) {
      // don't care
    }

    return result;
  }
Пример #8
0
 @Override
 public TemplateCollectionModel values() throws TemplateModelException {
   ArrayList<String> result = new ArrayList<String>();
   try {
     Iterator<String> iter = binaryNodeData.getAttributeNames().iterator();
     while (iter.hasNext()) {
       result.add(iter.next());
     }
   } catch (RepositoryException e) {
     // don't care
   }
   return (TemplateCollectionModel) wrapper.wrap(result.iterator());
 }