/*
* Copyright 2002-2007 the original author or authors.
*
* 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 demoapp.support.boot.tasks;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.internna.iwebmvc.boot.StartupTask;
import org.internna.iwebmvc.dao.DAO;
import org.internna.iwebmvc.dao.SecurityDAO;
import org.internna.iwebmvc.model.I18nText;
import org.internna.iwebmvc.model.Poll;
import org.internna.iwebmvc.model.PollOption;
import org.internna.iwebmvc.model.PollVote;
import org.internna.iwebmvc.model.security.UserImpl;
import org.springframework.beans.factory.annotation.Required;
/**
* Generates data for entity Poll during boot time.
*
* @author Jose Noheda
* @since 2.0
*/
public class GeneratePollTableDataStartupTask implements StartupTask {
protected Log logger = LogFactory.getLog(getClass());
protected DAO dao;
protected SecurityDAO securityDAO;
@Required public void setDao(DAO dao) {
this.dao = dao;
}
@Required public void setSecurityDAO(SecurityDAO securityDAO) {
this.securityDAO = securityDAO;
}
@Override public void execute() {
if (logger.isInfoEnabled()) logger.info("Executing startup task [GeneratePollTableDataStartupTask]");
if (dao.first(Poll.class) == null) {
if (logger.isInfoEnabled()) logger.info("Poll table is empty. Generating test data...");
Poll clubPoll = new Poll();
clubPoll.setQuestion(new I18nText());
clubPoll.getQuestion().add(new Locale("en"), "What's your favorite european football club?");
clubPoll.getQuestion().add(new Locale("es"), "�Cual es tu equipo de f�tbol favorito de Europa?");
clubPoll.setStoreVotes(true);
clubPoll.setRandomizeOptions(true);
clubPoll.setAllowAnonymousVotes(true);
List<PollOption> options = new ArrayList<PollOption>(5);
options.add(new PollOption());
options.get(0).setPoll(clubPoll);
options.get(0).setOrder(0);
options.get(0).setTotalVotes(2);
options.get(0).setAnswer(new I18nText());
options.get(0).getAnswer().add(new Locale("en"), "Real Madrid");
options.get(0).getAnswer().add(new Locale("es"), "Real Madrid C.F.");
List<PollVote> votes = new ArrayList<PollVote>(2);
votes.add(new PollVote());
votes.get(0).setIP("127.0.0.4");
votes.get(0).setOption(options.get(0));
votes.add(new PollVote());
votes.get(1).setIP("127.0.0.2");
votes.get(1).setOption(options.get(0));
options.get(0).setVotes(votes);
options.add(new PollOption());
options.get(1).setPoll(clubPoll);
options.get(1).setOrder(1);
options.get(1).setTotalVotes(1);
options.get(1).setAnswer(new I18nText());
options.get(1).getAnswer().add(new Locale("en"), "AC Milan");
options.get(1).getAnswer().add(new Locale("es"), "AC Milan");
List<PollVote> votes2 = new ArrayList<PollVote>(1);
votes2.add(new PollVote());
votes2.get(0).setIP("192.168.1.8");
votes2.get(0).setOption(options.get(1));
options.get(1).setVotes(votes2);
options.add(new PollOption());
options.get(2).setPoll(clubPoll);
options.get(2).setOrder(2);
options.get(2).setTotalVotes(2);
options.get(2).setAnswer(new I18nText());
options.get(2).getAnswer().add(new Locale("en"), "Manchester United");
options.get(2).getAnswer().add(new Locale("es"), "Manchester United");
List<PollVote> votes3 = new ArrayList<PollVote>(2);
votes3.add(new PollVote());
votes3.get(0).setIP("192.168.1.2");
votes3.get(0).setOption(options.get(2));
votes3.add(new PollVote());
votes3.get(1).setAuthor((UserImpl) securityDAO.findUser("iwebmvc"));
votes3.get(1).setOption(options.get(2));
options.get(2).setVotes(votes3);
options.add(new PollOption());
options.get(3).setPoll(clubPoll);
options.get(3).setOrder(3);
options.get(3).setTotalVotes(0);
options.get(3).setAnswer(new I18nText());
options.get(3).getAnswer().add(new Locale("en"), "Bayern Munich");
options.get(3).getAnswer().add(new Locale("es"), "Bayern Munich");
options.add(new PollOption());
options.get(4).setPoll(clubPoll);
options.get(4).setOrder(4);
options.get(4).setTotalVotes(1);
options.get(4).setAnswer(new I18nText());
options.get(4).getAnswer().add(new Locale("en"), "PSV Eindhoven");
options.get(4).getAnswer().add(new Locale("es"), "PSV Eindhoven");
List<PollVote> votes4 = new ArrayList<PollVote>(1);
votes4.add(new PollVote());
votes4.get(0).setIP("192.168.1.5");
votes4.get(0).setOption(options.get(4));
options.get(4).setVotes(votes4);
clubPoll.setOptions(options);
dao.create(clubPoll);
}
}
@Override public int order() {
return APPLICATION_ORDER + 15;
}
@Override public String getTaskName() {
return "Generate test data for Poll domain entity";
}
}