// Path: tanaguru-survey-api/src/main/java/org/opens/tanaguru/survey/exception/ForbiddenUserException.java
// public class ForbiddenUserException extends RuntimeException{
// 
//     private String parameter = null;
//     public String getParameter() {
//         return parameter;
//     }
// 
//     public void setParameter(String parameter) {
//         this.parameter = parameter;
//     }
// 
//     private static final String FORBIDDEN_USER_EXCEPTION_MSG_KEY = "forbidden-user-exception-msg-key";
//     private static final String NULL_USER_EXCEPTION_MSG_KEY = "null-user-exception-msg-key";
// 
//     public String getExceptionMsgKey() {
//         if (parameter != null) {
//             return FORBIDDEN_USER_EXCEPTION_MSG_KEY;
//         }
//         return NULL_USER_EXCEPTION_MSG_KEY;
//     }
// 
//     private static final long serialVersionUID = 6868141361082941720L;
// 
//     public ForbiddenUserException(String parameter) {
//         this.parameter = parameter;
//     }
// 
//     public ForbiddenUserException() {}
// 
// }
// 
// Path: tanaguru-survey-api/src/main/java/org/opens/tanaguru/survey/view/data/DetailedSurveyList.java
// public interface DetailedSurveyList extends SurveyList{
// 
//     /**
//      *
//      * @return
//      */
//     Collection<Contract> getContractCollection();
//     
//     /**
//      *
//      * @param contractList
//      */
//     void setContractCollection(Collection<Contract> contractCollection);
// 
//     /**
//      *
//      * @return
//      */
//     Collection<ContractResult> getTopContractCollection();
// 
//     /**
//      * 
//      * @param contractInfoCollection
//      */
//     void setTopContractCollection(Collection<ContractResult> contractCollection);
// 
// }
// 
// Path: tanaguru-survey-impl/src/main/java/org/opens/tanaguru/survey/view/data/DetailedSurveyListImpl.java
// public final class DetailedSurveyListImpl extends SurveyListImpl implements DetailedSurveyList{
// 
//     private Collection<Contract> contractCollection = new ArrayList<Contract>();
//     @Override
//     public Collection<Contract> getContractCollection() {
//         return contractCollection;
//     }
// 
//     @Override
//     public void setContractCollection(Collection<Contract> contractCollection) {
//         if (contractCollection != null) {
//             for (Contract contract : contractCollection) {
//                 if (contract.getUrl() != null && !contract.getUrl().isEmpty()) {
//                     this.contractCollection.add(contract);
//                 }
//             }
//         }
//     }
// 
//     private Collection<ContractResult> topContractResultCollection = new LinkedList<ContractResult>();
//     @Override
//     public Collection<ContractResult> getTopContractCollection() {
//         return topContractResultCollection;
//     }
// 
//     @Override
//     public void setTopContractCollection(Collection<ContractResult> contractResultCollection) {
//         if (contractResultCollection != null) {
//             this.topContractResultCollection.addAll(contractResultCollection);
//         }
//     }
// 
//     /**
//      * Default constructor
//      */
//     public DetailedSurveyListImpl() {}
// 
//     /**
//      * 
//      * @param id
//      * @param name
//      * @param label
//      * @param description
//      * @param contractCollection
//      */
//     public DetailedSurveyListImpl(
//             Long id,
//             String name,
//             String label,
//             Collection<Contract> contractCollection,
//             Collection<ContractResult> topContractCollection,
//             Integer surveyListAverage) {
//         setId(id);
//         setName(name);
//         setLabel(label);
//         setContractCollection(contractCollection);
//         setTopContractCollection(topContractCollection);
//         setSurveyListAverage(surveyListAverage);
//     }
// 
// }


import org.opens.tanaguru.survey.view.data.DetailedSurveyList;
import org.opens.tanaguru.survey.view.data.DetailedSurveyListImpl;
import org.opens.tgol.entity.contract.Contract;
import org.opens.tgol.entity.user.User;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.opens.tanaguru.survey.exception.ForbiddenUserException;
/*
 * Tanaguru - Automated webpage assessment
 * Copyright (C) 2008-2011  Open-S Company
 *
 * This file is part of Tanaguru.
 *
 * Tanaguru 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/>.
 *
 * Contact us by mail: open-s AT open-s DOT com
 */
package org.opens.tanaguru.survey.view.data.factory;


/**
 *
 * @author jkowalczyk
 */
public final class DetailedSurveyListFactoryImpl extends SurveyListFactoryImpl implements DetailedSurveyListFactory {

    private static final Logger LOGGER = Logger.getLogger(DetailedSurveyListFactoryImpl.class);

    private int nbOfTopElements = 0;
    public void setNbOfTopElements(int nbOfTopElements) {
        this.nbOfTopElements = nbOfTopElements;
    }

    @Override
    public DetailedSurveyList createDetailedSurveyList(
            Long surveyListId,
            boolean addContractList) {
        if (surveyListId == null || surveyListId <= 0) {
            LOGGER.warn("try to create surveyList from null user.");
            throw new ForbiddenUserException();
        }
        User user = getUserDataServiceDecorator().read(surveyListId);
        if (user == null) {
            LOGGER.warn("try to create surveyList from null user.");
            throw new ForbiddenUserException();
        }
        if (!user.getEmail1().startsWith(getUserListPrefix())) {
            LOGGER.warn("try to create surveyList from forbidden user " +surveyListId +". Throws ForbiddenUserException." );
            throw new ForbiddenUserException(user.getEmail1());
        }
        if (isFieldNotEmpty(user.getName()) && isFieldNotEmpty(user.getFirstName()) ) {
            throw new ForbiddenUserException(user.getEmail1());
        }
// Path: tanaguru-survey-api/src/main/java/org/opens/tanaguru/survey/exception/ForbiddenUserException.java
// public class ForbiddenUserException extends RuntimeException{
// 
//     private String parameter = null;
//     public String getParameter() {
//         return parameter;
//     }
// 
//     public void setParameter(String parameter) {
//         this.parameter = parameter;
//     }
// 
//     private static final String FORBIDDEN_USER_EXCEPTION_MSG_KEY = "forbidden-user-exception-msg-key";
//     private static final String NULL_USER_EXCEPTION_MSG_KEY = "null-user-exception-msg-key";
// 
//     public String getExceptionMsgKey() {
//         if (parameter != null) {
//             return FORBIDDEN_USER_EXCEPTION_MSG_KEY;
//         }
//         return NULL_USER_EXCEPTION_MSG_KEY;
//     }
// 
//     private static final long serialVersionUID = 6868141361082941720L;
// 
//     public ForbiddenUserException(String parameter) {
//         this.parameter = parameter;
//     }
// 
//     public ForbiddenUserException() {}
// 
// }
// 
// Path: tanaguru-survey-api/src/main/java/org/opens/tanaguru/survey/view/data/DetailedSurveyList.java
// public interface DetailedSurveyList extends SurveyList{
// 
//     /**
//      *
//      * @return
//      */
//     Collection<Contract> getContractCollection();
//     
//     /**
//      *
//      * @param contractList
//      */
//     void setContractCollection(Collection<Contract> contractCollection);
// 
//     /**
//      *
//      * @return
//      */
//     Collection<ContractResult> getTopContractCollection();
// 
//     /**
//      * 
//      * @param contractInfoCollection
//      */
//     void setTopContractCollection(Collection<ContractResult> contractCollection);
// 
// }
// 
// Path: tanaguru-survey-impl/src/main/java/org/opens/tanaguru/survey/view/data/DetailedSurveyListImpl.java
// public final class DetailedSurveyListImpl extends SurveyListImpl implements DetailedSurveyList{
// 
//     private Collection<Contract> contractCollection = new ArrayList<Contract>();
//     @Override
//     public Collection<Contract> getContractCollection() {
//         return contractCollection;
//     }
// 
//     @Override
//     public void setContractCollection(Collection<Contract> contractCollection) {
//         if (contractCollection != null) {
//             for (Contract contract : contractCollection) {
//                 if (contract.getUrl() != null && !contract.getUrl().isEmpty()) {
//                     this.contractCollection.add(contract);
//                 }
//             }
//         }
//     }
// 
//     private Collection<ContractResult> topContractResultCollection = new LinkedList<ContractResult>();
//     @Override
//     public Collection<ContractResult> getTopContractCollection() {
//         return topContractResultCollection;
//     }
// 
//     @Override
//     public void setTopContractCollection(Collection<ContractResult> contractResultCollection) {
//         if (contractResultCollection != null) {
//             this.topContractResultCollection.addAll(contractResultCollection);
//         }
//     }
// 
//     /**
//      * Default constructor
//      */
//     public DetailedSurveyListImpl() {}
// 
//     /**
//      * 
//      * @param id
//      * @param name
//      * @param label
//      * @param description
//      * @param contractCollection
//      */
//     public DetailedSurveyListImpl(
//             Long id,
//             String name,
//             String label,
//             Collection<Contract> contractCollection,
//             Collection<ContractResult> topContractCollection,
//             Integer surveyListAverage) {
//         setId(id);
//         setName(name);
//         setLabel(label);
//         setContractCollection(contractCollection);
//         setTopContractCollection(topContractCollection);
//         setSurveyListAverage(surveyListAverage);
//     }
// 
// }


// Path: tanaguru-survey-impl/src/main/java/org/opens/tanaguru/survey/view/data/factory/DetailedSurveyListFactoryImpl.java
import org.opens.tanaguru.survey.view.data.DetailedSurveyList;
import org.opens.tanaguru.survey.view.data.DetailedSurveyListImpl;
import org.opens.tgol.entity.contract.Contract;
import org.opens.tgol.entity.user.User;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.opens.tanaguru.survey.exception.ForbiddenUserException;

/*
 * Tanaguru - Automated webpage assessment
 * Copyright (C) 2008-2011  Open-S Company
 *
 * This file is part of Tanaguru.
 *
 * Tanaguru 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/>.
 *
 * Contact us by mail: open-s AT open-s DOT com
 */
package org.opens.tanaguru.survey.view.data.factory;


/**
 *
 * @author jkowalczyk
 */
public final class DetailedSurveyListFactoryImpl extends SurveyListFactoryImpl implements DetailedSurveyListFactory {

    private static final Logger LOGGER = Logger.getLogger(DetailedSurveyListFactoryImpl.class);

    private int nbOfTopElements = 0;
    public void setNbOfTopElements(int nbOfTopElements) {
        this.nbOfTopElements = nbOfTopElements;
    }

    @Override
    public DetailedSurveyList createDetailedSurveyList(
            Long surveyListId,
            boolean addContractList) {
        if (surveyListId == null || surveyListId <= 0) {
            LOGGER.warn("try to create surveyList from null user.");
            throw new ForbiddenUserException();
        }
        User user = getUserDataServiceDecorator().read(surveyListId);
        if (user == null) {
            LOGGER.warn("try to create surveyList from null user.");
            throw new ForbiddenUserException();
        }
        if (!user.getEmail1().startsWith(getUserListPrefix())) {
            LOGGER.warn("try to create surveyList from forbidden user " +surveyListId +". Throws ForbiddenUserException." );
            throw new ForbiddenUserException(user.getEmail1());
        }
        if (isFieldNotEmpty(user.getName()) && isFieldNotEmpty(user.getFirstName()) ) {
            throw new ForbiddenUserException(user.getEmail1());
        }
        DetailedSurveyList detailedSurvey = new DetailedSurveyListImpl();