/*
 * 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 com.google.common.base.Strings;
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(Strings.nullToEmpty(readString("label", assetNode)));
    Node captionNode = readNode("caption", assetNode);
    asset.setDescription((captionNode != null) ? buildTextWithMarkup(captionNode) : "");

    return asset;
  }

}

@@ -2613,40 +2613,382 @@
 t);%0A
+%0A
     
-asset.setContextElement(asse
+Node contextNode = assetNode;%0A    if (%22graphic%22.equals(assetNode.getNodeName())) %7B%0A      // Ambra treats %22graphic%22 as a special case and uses the parent node instead.%0A      // TODO: Ambra bug? Just using contextElement=%22graphic%22 makes more sense and is consistent with other cases.%0A      contextNode = assetNode.getParentNode();%0A    %7D%0A    asset.setContextElement(contex
 tNod

Handle special case for contextElement of "graphic" assets