/*
 * Copyright (c) 2006-2012 by Public Library of Science
 * http://plos.org
 * http://ambraproject.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.ambraproject.rhino.content.xml;

import com.google.common.base.Preconditions;
import org.ambraproject.models.ArticleAsset;
import org.ambraproject.rhino.identity.AssetIdentity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;

import java.util.List;

/**
 * Contains a whole article as an NLM-format XML document and extracts metadata for one asset.
 */
public class AssetXml extends AbstractArticleXml<ArticleAsset> {

  private static final Logger log = LoggerFactory.getLogger(AssetXml.class);

  private final AssetIdentity assetId;

  /**
   * The Node passed to this constructor may be a full document or just the asset node. In the former case, this class
   * will search the document for the identified asset node. The latter case is more efficient.
   *
   * @param xml
   * @param assetId
   */
  public AssetXml(Node xml, AssetIdentity assetId) {
    super(xml);
    this.assetId = Preconditions.checkNotNull(assetId);
  }

  @Override
  public ArticleAsset build(ArticleAsset obj) throws XmlContentException {
    List<AssetNode> allAssetNodes = findAllAssetNodes();
    Node matchingAssetNode = findMatchingAsset(allAssetNodes);
    return parseAsset(matchingAssetNode, obj);
  }

  /*
   * TODO: Query directly for the correct node instead of finding all of them and iterating
   */
  private Node findMatchingAsset(List<AssetNode> assetNodes) throws XmlContentException {
    final String targetDoi = assetId.getIdentifier();
    for (AssetNode assetNode : assetNodes) {
      if (targetDoi.equals(assetNode.getDoi())) {
        return assetNode.getNode();
      }
    }

    String errorMsg = "Article XML does not have an asset with DOI=" + targetDoi;
    throw new XmlContentException(errorMsg);
  }

  private ArticleAsset parseAsset(Node assetNode, ArticleAsset asset) {
    asset.setDoi(assetId.getKey());
    AssetIdentity.setNoFile(asset);
    asset.setContextElement(assetNode.getNodeName());

    asset.setTitle(readString("label", assetNode));
    Node captionNode = readNode("caption", assetNode);
    if (captionNode != null) {
      asset.setDescription(buildTextWithMarkup(captionNode));
    }

    return asset;
  }

}

@@ -751,16 +751,55 @@
 itions;%0A
+import com.google.common.base.Strings;%0A
 import o
@@ -2683,16 +2683,36 @@
 etTitle(
+Strings.nullToEmpty(
 readStri
@@ -2730,24 +2730,25 @@
  assetNode))
+)
 ;%0A    Node c

Use an empty string for asset title when its node is omitted