/*
* 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 org.internna.iwebmvc.boot.tasks;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
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.model.AddressType;
import org.internna.iwebmvc.model.I18nText;
/**
* Generates data for entity Country during boot time.
*
* @author Jose Noheda
* @since 2.0
*/
public class GenerateAddressTypesTableDataStartupTask implements StartupTask {
protected Log logger = LogFactory.getLog(getClass());
protected DAO dao;
private Map<Locale, Properties> addressTypes;
public void setDao(DAO dao) {
this.dao = dao;
}
public void setAddressTypes(Map<Locale, Properties> addressTypes) {
this.addressTypes = addressTypes;
}
@Override public void execute() {
if (logger.isInfoEnabled()) logger.info("Executing startup task [GenerateAddressTypesTableDataStartupTask]");
Long count = (Long) dao.executeQuery("SELECT COUNT(a) FROM AddressType a").get(0);
if (count <= 0) {
if (logger.isInfoEnabled()) logger.info("AddressType table is empty. Generating data...");
Properties allTypes = addressTypes.entrySet().iterator().next().getValue();
for (String code : allTypes.stringPropertyNames()) {
if (logger.isDebugEnabled()) logger.debug("Creating address type [" + code + "]");
try {
AddressType type = new AddressType();
type.setType(new I18nText());
for (Locale locale : addressTypes.keySet())
type.getType().add(locale, addressTypes.get(locale).getProperty(code));
dao.create(type);
} catch (Exception ex) {
if (logger.isDebugEnabled()) logger.debug("Address type [" + code + "] could not be saved: " + ex.getMessage());
}
}
}
}
@Override public String getTaskName() {
return "Generate data for AddressType domain entity";
}
@Override
public int order() {
return FRAMEWORK_ORDER + 4;
}
}