/*
* 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 demoapp.domain.Name;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
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.model.Amount;
import org.internna.iwebmvc.model.Currency;
import org.internna.iwebmvc.model.Document;
import org.internna.iwebmvc.model.I18nText;
import org.internna.iwebmvc.model.Rating;
import org.internna.iwebmvc.model.Sex;
import org.internna.iwebmvc.utils.StringUtils;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.util.FileCopyUtils;
import demoapp.domain.Order;
import demoapp.domain.Person;
import org.internna.iwebmvc.model.Link;
import org.internna.iwebmvc.model.MultiDocument;
/**
* Generates data for entity Order during boot time.
*
* @author Jose Noheda
* @since 2.0
*/
public class GenerateOrderTableDataStartupTask implements StartupTask {
protected Log logger = LogFactory.getLog(getClass());
protected DAO dao;
private List<Locale> supportedLocales;
@Required public void setDao(DAO dao) {
this.dao = dao;
}
@Required public void setSupportedLocales(List<Locale> supportedLocales) {
this.supportedLocales = supportedLocales;
}
@Override public void execute() {
if (logger.isInfoEnabled()) logger.info("Executing startup task [GenerateOrderTableDataStartupTask]");
if (dao.first(Order.class) == null) {
if (logger.isInfoEnabled()) logger.info("Order table is empty. Generating test data...");
Document photoJohn = null, house = null, car = null, dog = null,photoMary = null;
try {
photoJohn = new Document();
photoJohn.setIdentifier("Picture of John");
photoJohn.setMimeType("image/jpeg");
photoJohn.setCreation(new Date());
photoJohn.setWidth(325);
photoJohn.setHeight(326);
photoJohn.setTemporal(false);
photoJohn.setLastModified(photoJohn.getCreation());
photoJohn.setContents(FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("/john.jpg")));
house = new Document();
house.setIdentifier("John's House");
house.setMimeType("image/jpeg");
house.setCreation(new Date());
house.setWidth(600);
house.setHeight(400);
house.setTemporal(false);
house.setLastModified(photoJohn.getCreation());
house.setContents(FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("/house_john.jpg")));
car = new Document();
car.setIdentifier("John's car");
car.setMimeType("image/jpeg");
car.setCreation(new Date());
car.setWidth(400);
car.setHeight(300);
car.setTemporal(false);
car.setLastModified(photoJohn.getCreation());
car.setContents(FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("/car_john.jpg")));
dog = new Document();
dog.setIdentifier("John's pet");
dog.setMimeType("image/jpeg");
dog.setCreation(new Date());
dog.setWidth(400);
dog.setHeight(365);
dog.setTemporal(false);
dog.setLastModified(photoJohn.getCreation());
dog.setContents(FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("/dog_john.jpg")));
photoMary = new Document();
photoMary.setIdentifier("Picture of Mary");
photoMary.setMimeType("image/jpeg");
photoMary.setCreation(new Date());
photoMary.setWidth(340);
photoMary.setHeight(425);
photoMary.setTemporal(false);
photoMary.setLastModified(photoMary.getCreation());
photoMary.setContents(FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("/mary.jpg")));
} catch (Exception ex) {
if (logger.isInfoEnabled()) logger.info("Could not generate images data (person crud widget): " + ex.getMessage());
}
List<Sex> sex = dao.findByNamedQuery("Sex.findAll", 0, 2, null);
Person m = new Person();
m.setName(new Name());
m.getName().setName("John Smith");
m.setSex(sex.get(0));
m.setPhotos(new MultiDocument().addDocument(photoJohn).addDocument(house).addDocument(car).addDocument(dog));
m.setBlog(new Link());
m.getBlog().setReadableText(new I18nText());
m.getBlog().getReadableText().add(new Locale("en"), "My tech blog");
m.getBlog().getReadableText().add(new Locale("es"), "Mi blog de tecnolog�a");
m.getBlog().setUrl("http://internna.blogspot.com");
m.getBlog().setTooltipText(new I18nText());
m.getBlog().getTooltipText().add(new Locale("en"), "A must read!");
m.getBlog().getTooltipText().add(new Locale("es"), "�De lectura obligada!");
dao.create(m, false);
Person f = new Person();
f.setName(new Name());
f.getName().setName("Mary Poppins");
f.setSex(sex.get(1));
f.setPhotos(new MultiDocument().addDocument(photoMary));
dao.create(f, false);
Calendar calendar = Calendar.getInstance();
int num = 1000;
boolean emitted = false;
Currency eur = Currency.obtain("EUR", dao), usd = Currency.obtain("USD", dao);
int count = 0;
for (int i = 0; i < (30 + Math.round(Math.random() * 20)); i++) {
emitted = !emitted;
Order o = new Order();
o.setAmount(new Amount());
double aux = Math.random();
try {
o.setIdentifier(new I18nText());
for (Locale l : supportedLocales) o.getIdentifier().add(l, l + ":" + StringUtils.SHA1(String.valueOf(aux)));
} catch (Exception e) {};
o.getAmount().setAmount(aux * 1024 * 1024);
o.getAmount().setCurrency(aux > 0.5 ? usd : eur);
o.setOrderNumber(num++);
o.setPriority(new Rating());
o.getPriority().setRating(Double.valueOf(Math.round(aux * 10) % 5));
o.setEmitted(emitted);
calendar.add(Calendar.DATE, (int) Math.round(aux * 10));
o.setEmissionDate(calendar.getTime());
try {
o.setClient(aux > 0.5 ? m : f);
dao.create(o, false);
if (count++ % 25 == 0) {
dao.flush();
dao.clear();
}
} catch (Exception e) {
logger.debug("Failed to add an order: " + o);
}
}
Order o = dao.first(Order.class);
Document approved = new Document();
approved.setIdentifier("Seal of approval");
approved.setMimeType("image/jpeg");
approved.setCreation(new Date());
approved.setWidth(300);
approved.setHeight(315);
approved.setTemporal(false);
approved.setLastModified(approved.getCreation());
try {
approved.setContents(FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("/seal_approved.jpg")));
o.setSeal(approved);
dao.update(o);
} catch (IOException ex) {
if (logger.isInfoEnabled()) logger.info("Could not save seal of approval image (thumbnail demo): " + ex.getMessage());
}
}
}
@Override public int order() {
return APPLICATION_ORDER + 10;
}
@Override public String getTaskName() {
return "Generate test data for Order domain entity";
}
}