package no.kantega.publishing.common.ao;

import no.kantega.publishing.api.content.ContentIdentifier;
import no.kantega.publishing.api.path.PathEntry;
import no.kantega.publishing.api.path.PathEntryService;
import no.kantega.publishing.common.service.impl.PathWorker;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class PathEntryServiceLegacyImpl extends NamedParameterJdbcDaoSupport implements PathEntryService {
    private static final Logger log = LoggerFactory.getLogger(PathEntryServiceLegacyImpl.class);

    @Override
    public List<PathEntry> getPathEntriesByContentIdentifier(ContentIdentifier contentIdentifier) {
        return PathWorker.getPathByContentId(contentIdentifier);
    }

    @Override
    public List<PathEntry> getPathEntriesByAssociationIdInclusive(Integer associationId) {
        List<PathEntry> pathEntries = Collections.emptyList();

        try {
            String path = getNamedParameterJdbcTemplate().queryForObject("select Path from associations where UniqueId = :associationId",
                    Collections.singletonMap("associationId", associationId), String.class);
            String replaceSlashAddCurrent = StringUtils.removeStart(path, "/").replace("/", ",") + associationId;
            Map<String,Object> associationIds = new HashMap<>();
            associationIds.put("associationIds", Arrays.asList(replaceSlashAddCurrent.split(",")));
            pathEntries = getNamedParameterJdbcTemplate().query("select contentversion.Title, associations.AssociationId from content, contentversion, associations  where content.ContentId = contentversion.ContentId and contentversion.IsActive = 1 and content.contentId = associations.contentId and associations.AssociationId in (:associationIds) order by associations.AssociationId", associationIds, rowMapper);
        } catch (DataAccessException e) {
            log.error( e.getMessage());
        }
        return pathEntries;
    }

    private final RowMapper<PathEntry> rowMapper = new RowMapper<PathEntry>() {
        @Override
        public PathEntry mapRow(ResultSet resultSet, int i) throws SQLException {
            return new PathEntry(resultSet.getInt("AssociationId"), resultSet.getString("Title"));
        }
    };
}

@@ -619,9 +619,89 @@
 til.
-*
+Arrays;%0Aimport java.util.Collections;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0Ap
@@ -1670,55 +1670,47 @@
 s = 
-new HashMap%3C%3E();%0A            associationIds.put
+Collections.%3CString,Object%3EsingletonMap
 (%22as
@@ -1771,24 +1771,25 @@
 lit(%22,%22)));%0A
+%0A
             
@@ -1896,16 +1896,43 @@
 iationId
+, content.contentTemplateId
  from co
@@ -2535,22 +2535,37 @@
         
-return
+PathEntry pathEntry =
  new Pat
@@ -2633,24 +2633,137 @@
 (%22Title%22));%0A
+            pathEntry.setContentTemplateId(resultSet.getInt(%22contentTemplateId%22));%0A            return pathEntry;%0A
         %7D%0A  

Set contentTemplateId for PathEntry.