OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | Rev 177 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 151 Rev 156
Line 12... Line 12...
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.sql;
14
 package org.openconcerto.sql;
15
 
15
 
16
import org.openconcerto.sql.element.SQLElementDirectory;
16
import org.openconcerto.sql.element.SQLElementDirectory;
-
 
17
import org.openconcerto.sql.element.SQLElementNamesFromXML;
17
import org.openconcerto.sql.model.DBRoot;
18
import org.openconcerto.sql.model.DBRoot;
18
import org.openconcerto.sql.model.DBStructureItem;
19
import org.openconcerto.sql.model.DBStructureItem;
19
import org.openconcerto.sql.model.DBSystemRoot;
20
import org.openconcerto.sql.model.DBSystemRoot;
20
import org.openconcerto.sql.model.FieldMapper;
21
import org.openconcerto.sql.model.FieldMapper;
21
import org.openconcerto.sql.model.HierarchyLevel;
22
import org.openconcerto.sql.model.HierarchyLevel;
Line 34... Line 35...
34
import org.openconcerto.utils.LogUtils;
35
import org.openconcerto.utils.LogUtils;
35
import org.openconcerto.utils.MultipleOutputStream;
36
import org.openconcerto.utils.MultipleOutputStream;
36
import org.openconcerto.utils.NetUtils;
37
import org.openconcerto.utils.NetUtils;
37
import org.openconcerto.utils.ProductInfo;
38
import org.openconcerto.utils.ProductInfo;
38
import org.openconcerto.utils.RTInterruptedException;
39
import org.openconcerto.utils.RTInterruptedException;
-
 
40
import org.openconcerto.utils.ReflectUtils;
39
import org.openconcerto.utils.StreamUtils;
41
import org.openconcerto.utils.StreamUtils;
40
import org.openconcerto.utils.Tuple2;
42
import org.openconcerto.utils.Tuple2;
41
import org.openconcerto.utils.Value;
43
import org.openconcerto.utils.Value;
42
import org.openconcerto.utils.cc.IClosure;
44
import org.openconcerto.utils.cc.IClosure;
43
import org.openconcerto.utils.cc.IPredicate;
45
import org.openconcerto.utils.cc.IPredicate;
Line 370... Line 372...
370
    public String getDefaultBase() {
372
    public String getDefaultBase() {
371
        final boolean rootIsBase = this.getSystem().getDBRootLevel().equals(HierarchyLevel.SQLBASE);
373
        final boolean rootIsBase = this.getSystem().getDBRootLevel().equals(HierarchyLevel.SQLBASE);
372
        return rootIsBase ? this.getRootName() : this.getSystemRootName();
374
        return rootIsBase ? this.getRootName() : this.getSystemRootName();
373
    }
375
    }
374
 
376
 
-
 
377
    private final String toClassName(final String rsrcName) {
-
 
378
        if (rsrcName.charAt(0) == '/')
-
 
379
            return rsrcName.substring(1).replace('/', '.');
-
 
380
        else
-
 
381
            return this.getResourceWD().getPackage().getName() + '.' + rsrcName.replace('/', '.');
-
 
382
    }
-
 
383
 
375
    /**
384
    /**
376
     * Return the correct stream depending on file mode. If file mode is
385
     * Return the correct stream depending on file mode. If file mode is
377
     * {@link FileMode#NORMAL_FILE} it will first check if a file named <code>name</code> exists,
386
     * {@link FileMode#NORMAL_FILE} it will first check if a file named <code>name</code> exists,
378
     * otherwise it will look in the jar.
387
     * otherwise it will look in the jar.
379
     * 
388
     * 
Line 381... Line 390...
381
     * @return the corresponding stream, or <code>null</code> if not found.
390
     * @return the corresponding stream, or <code>null</code> if not found.
382
     */
391
     */
383
    public final InputStream getStream(final String name) {
392
    public final InputStream getStream(final String name) {
384
        final File f = getFile(name);
393
        final File f = getFile(name);
385
        if (mustUseClassloader(f)) {
394
        if (mustUseClassloader(f)) {
386
            return this.getClass().getResourceAsStream(name);
395
            return getResourceWD().getResourceAsStream(name);
387
        } else
396
        } else
388
            try {
397
            try {
389
                return new FileInputStream(f);
398
                return new FileInputStream(f);
390
            } catch (final FileNotFoundException e) {
399
            } catch (final FileNotFoundException e) {
391
                return null;
400
                return null;
392
            }
401
            }
393
    }
402
    }
394
 
403
 
-
 
404
    // the "working directory" where relative names are resolved
-
 
405
    protected Class<? extends PropsConfiguration> getResourceWD() {
-
 
406
        return this.getClass();
-
 
407
    }
-
 
408
 
395
    private File getFile(final String name) {
409
    private File getFile(final String name) {
396
        return new File(name.startsWith("/") ? name.substring(1) : name);
410
        return new File(name.startsWith("/") ? name.substring(1) : name);
397
    }
411
    }
398
 
412
 
399
    private boolean mustUseClassloader(final File f) {
413
    private boolean mustUseClassloader(final File f) {
Line 401... Line 415...
401
    }
415
    }
402
 
416
 
403
    public final String getResource(final String name) {
417
    public final String getResource(final String name) {
404
        final File f = getFile(name);
418
        final File f = getFile(name);
405
        if (mustUseClassloader(f)) {
419
        if (mustUseClassloader(f)) {
406
            return this.getClass().getResource(name).toExternalForm();
420
            return this.getResourceWD().getResource(name).toExternalForm();
407
        } else {
421
        } else {
408
            return f.getAbsolutePath();
422
            return f.getAbsolutePath();
409
        }
423
        }
410
    }
424
    }
411
 
425
 
Line 965... Line 979...
965
 
979
 
966
    protected SQLElementDirectory createDirectory() {
980
    protected SQLElementDirectory createDirectory() {
967
        return new SQLElementDirectory();
981
        return new SQLElementDirectory();
968
    }
982
    }
969
 
983
 
970
    // items will be passed to #getStream(String)
984
    // Use resource name to be able to use absolute (beginning with /) or relative path (to this
-
 
985
    // class)
971
    protected List<String> getMappings() {
986
    protected List<String> getMappings() {
972
        return Arrays.asList("mapping", "mapping-" + this.getProperty("customer"));
987
        return Arrays.asList("mapping", "mapping-" + this.getProperty("customer"));
973
    }
988
    }
974
 
989
 
975
    protected SQLFieldTranslator createTranslator(final SQLElementDirectory dir) {
990
    protected SQLFieldTranslator createTranslator(final SQLElementDirectory dir) {
976
        final List<String> mappings = getMappings();
991
        final List<String> mappings = getMappings();
977
        if (mappings.size() == 0)
992
        if (mappings.size() == 0)
978
            throw new IllegalStateException("empty mappings");
993
            throw new IllegalStateException("empty mappings");
979
 
994
 
980
        final SQLFieldTranslator trns = new SQLFieldTranslator(this.getRoot(), null, dir);
995
        final SQLFieldTranslator trns = new SQLFieldTranslator(this.getRoot(), dir);
981
        // perhaps listen to UserProps (as in TM)
996
        // perhaps listen to UserProps (as in TM)
982
        return loadTranslations(trns, this.getRoot(), mappings);
997
        return loadTranslations(trns, this.getRoot(), mappings);
983
    }
998
    }
984
 
999
 
985
    protected final SQLFieldTranslator loadTranslations(final SQLFieldTranslator trns, final DBRoot root, final List<String> mappings) {
1000
    protected final SQLFieldTranslator loadTranslations(final SQLFieldTranslator trns, final DBRoot root, final List<String> mappings) {
Line 993... Line 1008...
993
            final List<Locale> langs = cntrl.getCandidateLocales(fakeBaseName, targetLocale);
1008
            final List<Locale> langs = cntrl.getCandidateLocales(fakeBaseName, targetLocale);
994
            // SQLFieldTranslator overwrite, so we need to load from general to specific
1009
            // SQLFieldTranslator overwrite, so we need to load from general to specific
995
            final ListIterator<Locale> listIterator = CollectionUtils.getListIterator(langs, true);
1010
            final ListIterator<Locale> listIterator = CollectionUtils.getListIterator(langs, true);
996
            while (listIterator.hasNext()) {
1011
            while (listIterator.hasNext()) {
997
                final Locale lang = listIterator.next();
1012
                final Locale lang = listIterator.next();
-
 
1013
                final SQLElementNamesFromXML elemNames = new SQLElementNamesFromXML(lang);
998
                found |= loadTranslations(trns, PropsConfiguration.class.getResourceAsStream(cntrl.toBundleName("mapping", lang) + ".xml"), root);
1014
                found |= loadTranslations(trns, PropsConfiguration.class.getResourceAsStream(cntrl.toBundleName("mapping", lang) + ".xml"), root, elemNames);
999
                for (final String m : mappings) {
1015
                for (final String m : mappings) {
-
 
1016
                    final String bundleName = cntrl.toBundleName(m, lang);
1000
                    found |= loadTranslations(trns, this.getStream(cntrl.toBundleName(m, lang) + ".xml"), root);
1017
                    found |= loadTranslations(trns, this.getStream(bundleName + ".xml"), root, elemNames);
-
 
1018
                    final Class<? extends TranslatorFiller> loadedClass = ReflectUtils.getSubclass(toClassName(bundleName), TranslatorFiller.class);
-
 
1019
                    if (loadedClass != null) {
-
 
1020
                        try {
-
 
1021
                            ReflectUtils.createInstance(loadedClass, this).fill(trns);
-
 
1022
                        } catch (Exception e) {
-
 
1023
                            Log.get().log(Level.WARNING, "Couldn't use " + loadedClass, e);
-
 
1024
                        }
-
 
1025
                    }
1001
                }
1026
                }
1002
            }
1027
            }
1003
        }
1028
        }
1004
        return trns;
1029
        return trns;
1005
    }
1030
    }
1006
 
1031
 
-
 
1032
    @FunctionalInterface
-
 
1033
    static public interface TranslatorFiller {
-
 
1034
        void fill(final SQLFieldTranslator t);
-
 
1035
    }
-
 
1036
 
-
 
1037
    static public abstract class AbstractTranslatorFiller implements TranslatorFiller {
-
 
1038
        private final PropsConfiguration conf;
-
 
1039
 
-
 
1040
        public AbstractTranslatorFiller(final PropsConfiguration conf) {
-
 
1041
            this.conf = conf;
-
 
1042
        }
-
 
1043
 
-
 
1044
        protected final PropsConfiguration getConf() {
-
 
1045
            return this.conf;
-
 
1046
        }
-
 
1047
    }
-
 
1048
 
1007
    private final boolean loadTranslations(final SQLFieldTranslator trns, final InputStream in, final DBRoot root) {
1049
    private final boolean loadTranslations(final SQLFieldTranslator trns, final InputStream in, final DBRoot root, final SQLElementNamesFromXML elemNames) {
1008
        final boolean res = in != null;
1050
        final boolean res = in != null;
1009
        // do not force to have one mapping for each client and each locale
1051
        // do not force to have one mapping for each client and each locale
1010
        if (res)
1052
        if (res)
1011
            trns.load(root, in);
1053
            trns.load(root, in, elemNames);
1012
        return res;
1054
        return res;
1013
    }
1055
    }
1014
 
1056
 
1015
    protected File createWD() {
1057
    protected File createWD() {
1016
        return new File(this.getProperty("wd"));
1058
        return new File(this.getProperty("wd"));
Line 1292... Line 1334...
1292
            return this.filter;
1334
            return this.filter;
1293
        }
1335
        }
1294
    }
1336
    }
1295
 
1337
 
1296
    @Override
1338
    @Override
1297
    public final SQLFieldTranslator getTranslator() {
-
 
1298
        return this.getDirectory().getTranslator();
-
 
1299
    }
-
 
1300
 
-
 
1301
    @Override
-
 
1302
    public final SQLElementDirectory getDirectory() {
1339
    public final SQLElementDirectory getDirectory() {
1303
        return this.directory.get();
1340
        return this.directory.get();
1304
    }
1341
    }
1305
 
1342
 
1306
    public final ProductInfo getProductInfo() {
1343
    public final ProductInfo getProductInfo() {