/*
* 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.Map.Entry;
import java.util.Properties;
import java.util.Set;
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.Currency;
import org.internna.iwebmvc.model.I18nText;
import org.springframework.transaction.annotation.Transactional;
/**
* Generates data for entity Currency during boot time.
*
* @author Jose Noheda
* @since 2.0
*/
@Transactional
public class GenerateCurrencyTableDataStartupTask implements StartupTask {
protected Log logger = LogFactory.getLog(getClass());
protected DAO dao;
private String defaultCurrency;
private Map<Locale, Properties> ISO4217;
public void setDao(DAO dao) {
this.dao = dao;
}
public void setISO4217(Map<Locale, Properties> currencies) {
this.ISO4217 = currencies;
}
public void setDefaultCurrency(String defaultCurrency) {
this.defaultCurrency = defaultCurrency;
}
@Override public void execute() {
if (logger.isInfoEnabled()) logger.info("Executing startup task [GenerateCurrencyTableDataStartupTask]");
Long count = (Long) dao.executeQuery("SELECT COUNT(c) FROM Currency c").get(0);
if (count <= 0) {
if (logger.isInfoEnabled()) logger.info("Currency table is empty. Generating data...");
count = 0L;
Set<Entry<Locale, Properties>> iso4217 = ISO4217.entrySet();
Entry<Locale, Properties>[] entries = iso4217.toArray(new Entry[0]);
String[] names = entries[0].getValue().stringPropertyNames().toArray(new String[0]);
if (logger.isInfoEnabled()) logger.info("Starting currencies loop (" + names.length + ")");
for (String code : names) {
if (logger.isDebugEnabled()) logger.debug("Creating currency [" + code + "]");
try {
java.util.Currency currency = java.util.Currency.getInstance(code);
if (currency != null) {
Currency c = new Currency();
c.setCurrency(currency);
c.setName(new I18nText());
for (int index = 0; index < entries.length; index++)
c.getName().add(entries[index].getKey(), entries[index].getValue().getProperty(code));
c.setExchangeRate(1.0);
c.setDefaultCurrency(code.equals(defaultCurrency));
dao.create(c, false);
if (count++ % 25 == 0) {
dao.flush();
dao.clear();
}
}
} catch (Exception ex) {
if (logger.isDebugEnabled()) logger.debug("Currency [" + code + "] could not be saved. " + ex.getMessage());
}
}
if (logger.isInfoEnabled()) logger.info("Ending currencies loop");
}
}
@Override public String getTaskName() {
return "Generate data for Currency domain entity";
}
@Override
public int order() {
return FRAMEWORK_ORDER + 2;
}
}