OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 151 Rev 174
Line 76... Line 76...
76
 
76
 
77
    private static final String REGISTER_DIRNAME = "register";
77
    private static final String REGISTER_DIRNAME = "register";
78
    public static final String STRUCT_VERSION_2013 = "v20131206";
78
    public static final String STRUCT_VERSION_2013 = "v20131206";
79
    public static final String STRUCT_VERSION = "v20171220";
79
    public static final String STRUCT_VERSION = "v20171220";
80
 
80
 
-
 
81
    private static final String PREVIOUS_SUBDIR = "previous";
-
 
82
    private static final String CURRENT_SUBDIR = "current";
-
 
83
    private static final String STAGING_SUBDIR = "staging";
-
 
84
 
81
    private static final String LOG_FILENAME = "log.xml";
85
    private static final String LOG_FILENAME = "log.xml";
82
    static final String HASH_SUFFIX = ".hash";
86
    static final String HASH_SUFFIX = ".hash";
83
    private static final String LOG_HASH_FILENAME = LOG_FILENAME + HASH_SUFFIX;
87
    private static final String LOG_HASH_FILENAME = LOG_FILENAME + HASH_SUFFIX;
84
 
88
 
85
    static private final Comparator<Path> FILENAME_COMPARATOR = new Comparator<Path>() {
89
    static private final Comparator<Path> FILENAME_COMPARATOR = new Comparator<Path>() {
Line 331... Line 335...
331
        }
335
        }
332
        return res;
336
        return res;
333
    }
337
    }
334
 
338
 
335
    private final Path getDayDirToUse(final Path dayDir) throws IOException {
339
    private final Path getDayDirToUse(final Path dayDir) throws IOException {
336
        for (final Path subdir : new Path[] { dayDir.resolve("current"), dayDir.resolve("previous") }) {
340
        for (final Path subdir : new Path[] { dayDir.resolve(CURRENT_SUBDIR), dayDir.resolve(PREVIOUS_SUBDIR) }) {
337
            if (Files.exists(subdir)) {
341
            if (Files.exists(subdir)) {
338
                final ValidState validity = getDayDirValidity(subdir);
342
                final ValidState validity = getDayDirValidity(subdir);
339
                if (validity.isValid())
343
                if (validity.isValid())
340
                    return subdir;
344
                    return subdir;
341
                else
345
                else
Line 411... Line 415...
411
                // e.g. 2017/12/21/current/
415
                // e.g. 2017/12/21/current/
412
                final Path dayDirToUse = input.getDayDirToUse(dayDir);
416
                final Path dayDirToUse = input.getDayDirToUse(dayDir);
413
                if (dayDirToUse != null)
417
                if (dayDirToUse != null)
414
                    throw new IllegalStateException(cal.getTime() + " already open");
418
                    throw new IllegalStateException(cal.getTime() + " already open");
415
 
419
 
416
                final Path stagingDir = dayDir.resolve("staging");
420
                final Path stagingDir = dayDir.resolve(STAGING_SUBDIR);
417
                final Path currentDir = stagingDir.resolveSibling("current");
421
                final Path currentDir = stagingDir.resolveSibling(CURRENT_SUBDIR);
418
                final Path prevDir = stagingDir.resolveSibling("previous");
422
                final Path prevDir = stagingDir.resolveSibling(PREVIOUS_SUBDIR);
419
                FileUtils.rm_R(stagingDir);
423
                FileUtils.rm_R(stagingDir);
420
                FileUtils.rm_R(currentDir);
424
                FileUtils.rm_R(currentDir);
421
                FileUtils.rm_R(prevDir);
425
                FileUtils.rm_R(prevDir);
422
                Files.createDirectory(stagingDir);
426
                Files.createDirectory(stagingDir);
423
 
427
 
424
                final Element rootElem = RegisterLog.createRootElement();
428
                final Element rootElem = RegisterLog.createRootElement();
425
                rootElem.addContent(new RegisterLogEntry.RegisterEntry(EventType.REGISTER_OPENING, cal.getTime(), userID, input.getPosID(), lastLocalHash, prevDate).toXML());
429
                rootElem.addContent(new RegisterLogEntry.RegisterEntry(EventType.REGISTER_OPENING, cal.getTime(), userID, input.getPosID(), lastLocalHash, prevDate).toXML());
426
                save(new Document(rootElem), stagingDir.resolve(LOG_FILENAME));
430
                save(new Document(rootElem), stagingDir.resolve(LOG_FILENAME));
427
 
431
 
-
 
432
                for (int i = 0; i < 5; i++) {
-
 
433
                    try {
428
                Files.move(stagingDir, currentDir, StandardCopyOption.ATOMIC_MOVE);
434
                        Files.move(stagingDir, currentDir, StandardCopyOption.ATOMIC_MOVE);
-
 
435
                        break;
-
 
436
                    } catch (Exception e) {
-
 
437
                        e.printStackTrace();
-
 
438
                        try {
-
 
439
                            // Retry 5 times, because can fail under Windows
-
 
440
                            Thread.sleep(1000);
-
 
441
                        } catch (InterruptedException e1) {
-
 
442
                            e1.printStackTrace();
-
 
443
                        }
-
 
444
                    }
-
 
445
                }
429
 
446
 
430
                POSConfiguration.getLogger().log(Level.INFO, "Finished opening of FS state for register {0}", registerDB);
447
                POSConfiguration.getLogger().log(Level.INFO, "Finished opening of FS state for register {0}", registerDB);
431
 
448
 
432
                // TODO parse and validate before moving into place
449
                // TODO parse and validate before moving into place
433
                try {
450
                try {
Line 452... Line 469...
452
            POSConfiguration.getLogger().log(Level.FINE, "Begin " + this.logMsg + " for register {0}", input.getPosID());
469
            POSConfiguration.getLogger().log(Level.FINE, "Begin " + this.logMsg + " for register {0}", input.getPosID());
453
            final RegisterLog lastLog = checkStatus(needsClosed());
470
            final RegisterLog lastLog = checkStatus(needsClosed());
454
 
471
 
455
            // e.g. 2017/12/21/current/
472
            // e.g. 2017/12/21/current/
456
            final Path toUse = lastLog.getLogFile().getParent();
473
            final Path toUse = lastLog.getLogFile().getParent();
457
            final Path stagingDir = toUse.resolveSibling("staging");
474
            final Path stagingDir = toUse.resolveSibling(STAGING_SUBDIR);
458
            final Path currentDir = stagingDir.resolveSibling("current");
475
            final Path currentDir = stagingDir.resolveSibling(CURRENT_SUBDIR);
459
            final Path prevDir = stagingDir.resolveSibling("previous");
476
            final Path prevDir = stagingDir.resolveSibling(PREVIOUS_SUBDIR);
460
 
477
 
461
            FileUtils.rm_R(stagingDir);
478
            FileUtils.rm_R(stagingDir);
462
            FileUtils.copyDirectory(toUse, stagingDir, input.useHardLinks, StandardCopyOption.COPY_ATTRIBUTES);
479
            FileUtils.copyDirectory(toUse, stagingDir, input.useHardLinks, StandardCopyOption.COPY_ATTRIBUTES);
463
 
480
 
464
            final I intermediateRes = updateDir(stagingDir, lastLog);
481
            final I intermediateRes = updateDir(stagingDir, lastLog);