/*Copyright (c) 2016 "hbz"

This file is part of zettel.

etikett is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package models;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.typesafe.config.ConfigFactory;
import play.data.validation.ValidationError;
import services.ZettelModel;

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

/**
 * @author Jan Schnasse
 *
 */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Article extends ZettelModel {

	/**
	 * The id under which this model is registered in the ZettelRegister
	 */
	public final static String id = "katalog:article";

	@Override
	protected String getType() {
		return "http://hbz-nrw.de/regal#Article";
	}

	/**
	 * The help text url must provide help texts in a certain html form. For each
	 * field a <div id="fieldName"> must be provided to contain the help text for
	 * the corresponding field.
	 * 
	 * @return return a url with help texts for the form
	 */
	public static String getHelpTextUrl() {
		String url = ConfigFactory.load().getString("zettel.article.helpText");
		return url;
	}

	@Override
	public List<ValidationError> validate() {
		List<ValidationError> errors = new ArrayList<>();
		validateAuthorship(errors);
		validateSimpleFields(errors);
		validateListFields(errors);
		return errors.isEmpty() ? null : errors;
	}

	private void validateListFields(List<ValidationError> errors) {
		if (containsNothing(getDdc())) {
			setDdc(new ArrayList<>());
			errors.add(new ValidationError("ddc",
					"Bitte orden Sie Ihre Daten einem Dewey Schlagwort zu!"));
		}
	}

	private void validateSimpleFields(List<ValidationError> errors) {
		addErrorMessage("title", "Bitte vergeben Sie einen Titel!",
				() -> getTitle(), errors);
		addErrorMessage("license", "Bitte vergeben Sie eine Lizenz!",
				() -> getLicense(), errors);
		addErrorMessage("professionalGroup",
				"Bitte orden Sie Ihre Daten einer Fachgruppe zu!",
				() -> getProfessionalGroup(), errors);
		addErrorMessage("language",
				"Welche Sprache passt am ehesten zu Ihrer Eingabe?",
				() -> getLanguage(), errors);
		addErrorMessage("medium", "Bitte ordnen Sie ihre Eingabe einem Medium zu!",
				() -> getMedium(), errors);
	}

	private void validateAuthorship(List<ValidationError> errors) {
		if (containsNothing(getCreator())) {
			setCreator(new ArrayList<>());
		}
		if (containsNothing(getContributor())) {
			setContributor(new ArrayList<>());
		}
		if (containsNothing(getCreatorName())) {
			setCreatorName(new ArrayList<>());
		}
		if (containsNothing(getContributorName())) {
			setContributorName(new ArrayList<>());
		}
		if (getCreator().isEmpty() && getContributor().isEmpty()
				&& getCreatorName().isEmpty() && getContributorName().isEmpty()) {
			errors.add(new ValidationError("creator",
					"Bitte geben Sie einen Autor oder Beteiligten an!"));
			errors.add(new ValidationError("contributor",
					"Bitte geben Sie einen Autor oder Beteiligten an!"));
			errors.add(new ValidationError("creatorName",
					"Bitte geben Sie einen Autor oder Beteiligten an!"));
			errors.add(new ValidationError("contributorName",
					"Bitte geben Sie einen Autor oder Beteiligten an!"));
		}
	}
}
@@ -839,16 +839,46 @@
 nError;%0A
+import services.ZettelFields;%0A
 import s
@@ -1791,16 +1791,42 @@
 st%3C%3E();%0A
+%09%09validateStatus(errors);%0A
 %09%09valida
@@ -1952,24 +1952,285 @@
 errors;%0A%09%7D%0A%0A
+%09private void validateStatus(List%3CValidationError%3E errors) %7B%0A%09%09addErrorMessage(%22publicationStatus%22,%0A%09%09%09%09String.format(%22Bitte vergeben Sie einen %25s!%22, ZettelFields.publicationStatusZF.getLabel()),%0A%09%09%09%09() -%3E getTitle(), errors);%0A%09%09// reviewStatus is optional%0A%09%7D%0A%0A
 %09private voi

Validate Status section of Article form