1    /*
2     * Copyright 2007 :torweg free software group
3     *
4     * This program is free software: you can redistribute it and/or modify
5     * it under the terms of the GNU General Public License as published by
6     * the Free Software Foundation, either version 3 of the License, or
7     * (at your option) any later version.
8     * 
9     * This program is distributed in the hope that it will be useful,
10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    * GNU General Public License for more details.
13    * 
14    * You should have received a copy of the GNU General Public License
15    * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16    * 
17    */
18   package org.torweg.pulse.component.util.retailer.imports;
19   
20   import java.io.File;
21   import java.util.List;
22   
23   import org.hibernate.Session;
24   import org.hibernate.criterion.MatchMode;
25   import org.hibernate.criterion.Restrictions;
26   import org.torweg.pulse.component.util.model.Retailer;
27   import org.torweg.pulse.vfs.VirtualFile;
28   
29   /**
30    * Subclasses of {@code AbstractRetailerImporter} are used for importing
31    * {@code Retailer}s from files.
32    * 
33    * @author Christian Schatt, Daniel Dietz
34    * @version $Revision: 1581 $
35    */
36   public abstract class AbstractRetailerImporter {
37   
38       /**
39        * Imports the {@code Retailer}s from the given {@code File} and
40        * stores them in the database.
41        * 
42        * @param session
43        *            the {@code Session} used for accessing the database.
44        * @param file
45        *            the {@code File} containing the data.
46        * @param countryCode
47        *            the country the {@code Retailer}s have to be from.
48        * @param conf
49        *            the current {@code ImportRetailerConfiguration}
50        * 
51        * @return the amount of stored {@code Retailer}s.
52        * 
53        * @throws RetailerImporterException
54        *             if an exception occurs while importing the
55        *             {@code Retailer}s.
56        */
57       protected abstract int importAllRetailersFromFile(final Session session,
58               final File file, final String countryCode,
59               final ImportRetailerConfiguration conf)
60               throws RetailerImporterException;
61   
62       /**
63        * Returns the logo file of the given {@code Retailer}. If none can be
64        * found, {@code null} will be returned.
65        * 
66        * @param session
67        *            the {@code Session} used for accessing the database.
68        * @param retailer
69        *            the {@code Retailer} whose logo file is sought after.
70        * @param logoFolderPath
71        *            the path to the folder containing the logo files.
72        * @return the logo file of the given {@code Retailer} or
73        *         {@code null}.
74        */
75       @SuppressWarnings("unchecked")
76       protected final VirtualFile getRetailerLogo(final Session session,
77               final Retailer retailer, final String logoFolderPath) {
78           if (retailer.getCustomerNumber() == null || logoFolderPath == null) {
79               return null;
80           }
81           String firstPathPart = logoFolderPath + retailer.getCountry() + '_'
82                   + retailer.getCustomerNumber() + '.';
83           List<VirtualFile> logos = (List<VirtualFile>) session.createCriteria(
84                   VirtualFile.class).add(
85                   Restrictions.like("path", firstPathPart, MatchMode.START))
86                   .list();
87           if (logos.isEmpty()) {
88               return null;
89           }
90           return logos.get(0);
91       }
92   
93   }
94