}
logger.debug("Starting creation of RecurredOffer (with new Order) for RecurringOffer: " + JDOHelper.getObjectId(recurringOffer));
PersistenceManager pm = getPersistenceManager();
Trader trader = Trader.getTrader(pm);
Order order = trader.createOrder(recurringOffer.getVendor(),
recurringOffer.getCustomer(), null, recurringOffer.getCurrency());
logger.debug("Created Order: " + JDOHelper.getObjectId(order));
User user = SecurityReflector.getUserDescriptor().getUser(pm);
String offerIDPrefix = recurringOffer.getOfferIDPrefix();
Collection<Segment> recurringSegments = new HashSet<Segment>();
for (Article article : recurringOffer.getArticles()) {
recurringSegments.add(article.getSegment());
}
// create the new segment for the order
for (Segment segment : recurringSegments) {
trader.createSegment(order, segment.getSegmentType());
}
RecurredOffer recurredOffer = createRecurredOffer(recurringOffer,user, order, offerIDPrefix);
logger.debug("Created RecurredOffer: " + JDOHelper.getObjectId(recurredOffer));
// Loop over all articles in the given offer and group
// them by SegmentType and ProductType-class
Map<SegmentType, Map<Class<? extends ProductType>, Set<Article>>> segmentTypes2PTClass2Articles = new HashMap<SegmentType, Map<Class<? extends ProductType>, Set<Article>>>();
Set<Article> articles = new HashSet<Article>();
for (Article recurringArticle : recurringOffer.getArticles()) {
SegmentType segmentType = recurringArticle.getSegment().getSegmentType();
Map<Class<? extends ProductType>, Set<Article>> ptClass2Articles = segmentTypes2PTClass2Articles.get(segmentType);
if (ptClass2Articles == null) {
ptClass2Articles = new HashMap<Class<? extends ProductType>, Set<Article>>();
segmentTypes2PTClass2Articles.put(segmentType, ptClass2Articles);
}
Class<? extends ProductType> productTypeClass = recurringArticle.getProductType().getClass();
articles = ptClass2Articles.get(productTypeClass);
if (articles == null) {
articles = new HashSet<Article>();
ptClass2Articles.put(productTypeClass, articles);
}
articles.add(recurringArticle);
}
if (logger.isDebugEnabled()) {
logger.debug("Grouped articles in RecurringOffer:");
for (Map.Entry<SegmentType, Map<Class<? extends ProductType>, Set<Article>>> segmentTypeEntry : segmentTypes2PTClass2Articles.entrySet()) {
logger.debug(" SegmentType: " + JDOHelper.getObjectId(segmentTypeEntry.getKey()));
for (Map.Entry<Class<? extends ProductType>, Set<Article>> productTypeEntry : segmentTypeEntry.getValue().entrySet()) {
logger.debug(" ProductType class: " + productTypeEntry.getKey());
for (Article article : productTypeEntry.getValue()) {
logger.debug(" Article: " + JDOHelper.getObjectId(article));
}
}
}
}
// loop over the segments added to the order
for (Segment segment : order.getSegments()) {
logger.debug("Creating articles for RecurredOffer for SegmentType " + JDOHelper.getObjectId(segment.getSegmentType()));
Map<Class<? extends ProductType>, Set<Article>> collected = segmentTypes2PTClass2Articles.get(segment.getSegmentType());
if (collected != null) { // it is possible that there are segments with no articles in the RecurringOffer
// add each segment to the RecurredOffer
recurredOffer.addSegment(segment);