/*
 * Copyright 2015 Adaptris Ltd.
 * 
 * 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 com.adaptris.core;

import java.util.ArrayList;
import java.util.List;

import javax.validation.constraints.NotNull;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adaptris.annotation.DisplayOrder;
import com.adaptris.core.util.Args;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

/**
 * Implementation of {@link ProduceDestination} which concatenates message metadata to create a dynamic destination name.
 * 
 * @config metadata-destination
 */
@XStreamAlias("metadata-destination")
@DisplayOrder(order = {"keys"})
public class MetadataDestination implements MessageDrivenDestination {

  private transient Logger log = LoggerFactory.getLogger(this.getClass().getName());
  @NotNull
  @XStreamImplicit(itemFieldName = "key")
  private List<String> keys;

  /**
   * <p>
   * Creates a new instance.
   * </p>
   */
  public MetadataDestination() {
    keys = new ArrayList<String>();
  }

  /**
   * <p>
   * Adds a key to the end of the list of keys to look for in the metadata.
   * </p>
   * @param s a key to look for in the metadata
   */
  public void addKey(String s) {
    keys.add(s);
  }


  /**
   * <p>
   * Returns the <code>List</code> of keys that are used to construct the destination name.
   * </p>
   *
   * @return the <code>List</code> of keys that are used to construct the destination name
   */
  public List<String> getKeys() {
    return keys;
  }

  /**
   * <p>
   * Sets the <code>List</code> of keys that are used to construct the destination name.
   * </p>
   *
   * @param l the <code>List</code> of keys that are used to construct the destination name
   */
  public void setKeys(List<String> l) {
    keys = Args.notNull(l, "metadataKeys");
  }

  /**
   * <p>
   * Creates a new <code>ProduceDestination</code> by concatenating
   * the values of configurable metadata from the <code>AdaptrisMessage</code>.
   * </p>
   * @param msg the message to process
   * @return the <code>String</code> destination name
   * @throws CoreException wrapping any underlying <code>Exception</code>s
   */
  public String getDestination(AdaptrisMessage msg) throws CoreException {

    String destinationName = "";
    for (String key : keys) {
      if (msg.containsKey(key)) {
        destinationName += StringUtils.defaultIfEmpty(msg.getMetadataValue(key), "");
      }
    }
    if (StringUtils.isBlank(destinationName)) {
      destinationName = null;
    }
    log.debug("dynamic destination [" + destinationName + "]");

    return destinationName;
  }
}

@@ -2914,44 +2914,8 @@
 ) %7B%0A
-      if (msg.containsKey(key)) %7B%0A  
     
@@ -3002,72 +3002,59 @@
     
-  
 %7D%0A    
-%7D%0A    if (StringUtils.isBlank(destinationName)) %7B%0A      
+destinationName = StringUtils.defaultIfEmpty(
 dest
@@ -3068,23 +3068,17 @@
 Name
- =
+,
  null
+)
 ;%0A
-    %7D%0A
     
@@ -3113,11 +3113,13 @@
 on %5B
-%22 +
+%7B%7D%5D%22,
  des
@@ -3134,18 +3134,11 @@
 Name
- + %22%5D%22
 );%0A
-%0A
     

Refactor to remove deprecated method use; in the end need less ifs.