/*
  * NOTE FOR DEVELOPERS:
  *
  * Never modify or reference this class directly. All methods that expect a journal article image model instance should use the {@link JournalArticleImage} interface instead.
  */
 @Override
 public void persist() throws SystemException {
   if (this.isNew()) {
     JournalArticleImageLocalServiceUtil.addJournalArticleImage(this);
   } else {
     JournalArticleImageLocalServiceUtil.updateJournalArticleImage(this);
   }
 }
  @Test
  public void testActionableDynamicQuery() throws Exception {
    final IntegerWrapper count = new IntegerWrapper();

    ActionableDynamicQuery actionableDynamicQuery =
        JournalArticleImageLocalServiceUtil.getActionableDynamicQuery();

    actionableDynamicQuery.setPerformActionMethod(
        new ActionableDynamicQuery.PerformActionMethod() {
          @Override
          public void performAction(Object object) {
            JournalArticleImage journalArticleImage = (JournalArticleImage) object;

            Assert.assertNotNull(journalArticleImage);

            count.increment();
          }
        });

    actionableDynamicQuery.performActions();

    Assert.assertEquals(count.getValue(), _persistence.countAll());
  }
  protected void format(
      String oldCompanyId,
      long newCompanyId,
      long groupId,
      String articleId,
      double version,
      Element root)
      throws Exception {

    Iterator<Element> itr = root.elements().iterator();

    while (itr.hasNext()) {
      Element el = itr.next();

      Element dynamicContent = el.element("dynamic-content");

      String elInstanceId = StringPool.BLANK;
      String elName = el.attributeValue("name", StringPool.BLANK);
      String elType = el.attributeValue("type", StringPool.BLANK);
      String elLanguage = StringPool.BLANK;

      if (dynamicContent != null) {
        elLanguage = dynamicContent.attributeValue("language-id", StringPool.BLANK);

        if (!elLanguage.equals(StringPool.BLANK)) {
          elLanguage = "_" + elLanguage;
        }
      }

      if (elType.equals("image") || elType.equals("text")) {
        String oldImageId = dynamicContent.getText();

        if (oldImageId.startsWith(_IMG_ID_PATH)
            || oldImageId.startsWith("@portal_url@" + _IMG_ID_PATH)
            || oldImageId.startsWith("http://@portal_url@" + _IMG_ID_PATH)
            || oldImageId.startsWith("https://@portal_url@" + _IMG_ID_PATH)) {

          int pos = oldImageId.indexOf(_IMG_ID_PATH);

          String preOldImageId = oldImageId.substring(0, pos);

          oldImageId = oldImageId.substring(pos + _IMG_ID_PATH.length(), oldImageId.length());

          String newImageId = getNewImageId(oldCompanyId, oldImageId);

          dynamicContent.setText(preOldImageId + _IMG_ID_PATH + newImageId);

          if (elType.equals("image")) {
            dynamicContent.addAttribute("id", newImageId);

            long articleImageId = GetterUtil.getLong(newImageId);

            JournalArticleImageLocalServiceUtil.addArticleImageId(
                articleImageId, groupId, articleId, version, elInstanceId, elName, elLanguage);
          }
        }
      }

      format(oldCompanyId, newCompanyId, groupId, articleId, version, el);
    }
  }
  protected void format(
      long groupId,
      String articleId,
      double version,
      String previewArticleId,
      Element root,
      UploadServletRequest uploadRequest)
      throws Exception {

    Iterator<Element> itr = root.elements().iterator();

    while (itr.hasNext()) {
      Element el = itr.next();

      Element dynamicContent = el.element("dynamic-content");

      String elInstanceId = el.attributeValue("instance-id", StringPool.BLANK);
      String elName = el.attributeValue("name", StringPool.BLANK);
      String elType = el.attributeValue("type", StringPool.BLANK);
      String elContent = StringPool.BLANK;
      String elLanguage = StringPool.BLANK;

      if (dynamicContent != null) {
        elContent = dynamicContent.getTextTrim();

        elLanguage = dynamicContent.attributeValue("language-id", StringPool.BLANK);

        if (!elLanguage.equals(StringPool.BLANK)) {
          elLanguage = "_" + elLanguage;
        }
      }

      if (elType.equals("image") && Validator.isNull(elContent)) {
        File file = uploadRequest.getFile("structure_image_" + elName + elLanguage);
        byte[] bytes = FileUtil.getBytes(file);

        if ((bytes != null) && (bytes.length > 0)) {
          long imageId =
              JournalArticleImageLocalServiceUtil.getArticleImageId(
                  groupId, previewArticleId, version, elInstanceId, elName, elLanguage, true);

          String token = ImageServletTokenUtil.getToken(imageId);

          dynamicContent.setText("/image/journal/article?img_id=" + imageId + "&t=" + token);

          ImageLocalServiceUtil.updateImage(imageId, bytes);
        } else {
          if (Validator.isNotNull(articleId)) {
            long imageId =
                JournalArticleImageLocalServiceUtil.getArticleImageId(
                    groupId, articleId, version, elInstanceId, elName, elLanguage);

            String token = ImageServletTokenUtil.getToken(imageId);

            dynamicContent.setText("/image/journal/article?img_id=" + imageId + "&t=" + token);
          }
        }
      }

      format(groupId, articleId, version, previewArticleId, el, uploadRequest);
    }
  }