/*
 * Copyright 2014 Tagbangers, Inc.
 *
 * 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.wallride.web.support;

import org.springframework.data.domain.Page;
import org.thymeleaf.context.IProcessingContext;
import org.wallride.domain.Article;
import org.wallride.model.ArticleSearchRequest;
import org.wallride.support.ArticleUtils;

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

public class Articles {

	private IProcessingContext processingContext;
	private ArticleUtils articleUtils;

	public Articles(IProcessingContext processingContext, ArticleUtils articleUtils) {
		this.processingContext = processingContext;
		this.articleUtils = articleUtils;
	}

	public List<Article> search(Condition condition) {
		Page<Article> result = articleUtils.search(condition.buildArticleSearchRequest(), condition.size);
		return new ArrayList<>(result.getContent());
	}

	public Condition condition() {
		return new Condition();
	}

	class Condition {

		private int size = 1;

		private String keyword;
		private Collection<Long> categoryIds;
		private Collection<String> categoryCodes;
		private Collection<String> tagNames;
		private Long authorId;
//		private Post.Status status;
//		private LocalDateTime dateFrom;
//		private LocalDateTime dateTo;

		public Condition size(int size) {
			this.size = size;
			return this;
		}

		public Condition keyword(String keyword) {
			this.keyword = keyword;
			return this;
		}

		public Condition categoryIds(Long... values) {
			List<Long> categoryIds = new ArrayList<>();
			for (Long value : values) {
				categoryIds.add(value);
			}
			this.categoryIds = categoryIds;
			return this;
		}

		public Condition categoryCodes(String... values) {
			List<String> categoryCodes = new ArrayList<>();
			for (String value : values) {
				categoryCodes.add(value);
			}
			this.categoryCodes = categoryCodes;
			return this;
		}

		public Condition tagNames(String... values) {
			List<String> tagNames = new ArrayList<>();
			for (String value : values) {
				tagNames.add(value);
			}
			this.tagNames = tagNames;
			return this;
		}

		public Condition authorId(Long authorId) {
			this.authorId = authorId;
			return this;
		}

		private ArticleSearchRequest buildArticleSearchRequest() {
			ArticleSearchRequest request = new ArticleSearchRequest(processingContext.getContext().getLocale().getLanguage())
					.withKeyword(this.keyword)
					.withCategoryIds(this.categoryIds)
					.withCategoryCodes(this.categoryCodes)
					.withTagNames(this.tagNames)
					.withAuthorId(this.authorId);
			return request;
		}
	}
}
@@ -1996,19 +1996,16 @@
 category
-Ids
 (Long...
@@ -2007,32 +2007,32 @@
 ng... values) %7B%0A
+
 %09%09%09List%3CLong%3E ca
@@ -2209,21 +2209,16 @@
 category
-Codes
 (String.
@@ -2431,21 +2431,16 @@
 tion tag
-Names
 (String.
@@ -2603,32 +2603,32 @@
 turn this;%0A%09%09%7D%0A%0A
+
 %09%09public Conditi
@@ -2636,18 +2636,16 @@
 n author
-Id
 (Long au

Fix Articles