OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev 93 Rev 144
Line 23... Line 23...
23
import java.text.DecimalFormat;
23
import java.text.DecimalFormat;
24
import java.text.Format;
24
import java.text.Format;
25
import java.text.ParseException;
25
import java.text.ParseException;
26
import java.text.ParsePosition;
26
import java.text.ParsePosition;
27
import java.text.SimpleDateFormat;
27
import java.text.SimpleDateFormat;
28
import java.util.ArrayList;
-
 
29
import java.util.Arrays;
28
import java.util.Arrays;
30
import java.util.Calendar;
29
import java.util.Calendar;
31
import java.util.Collections;
-
 
32
import java.util.Date;
30
import java.util.Date;
33
import java.util.List;
31
import java.util.List;
34
 
32
 
35
public class ReceiptCode {
33
public class ReceiptCode {
36
 
34
 
Line 51... Line 49...
51
        final TemplateNXProps nxprops = (TemplateNXProps) TemplateNXProps.getInstance();
49
        final TemplateNXProps nxprops = (TemplateNXProps) TemplateNXProps.getInstance();
52
        return new File(nxprops.getDefaultStringValue(), archived ? "Tickets archivés" : "Tickets");
50
        return new File(nxprops.getDefaultStringValue(), archived ? "Tickets archivés" : "Tickets");
53
    }
51
    }
54
 
52
 
55
    static public final File getDayDir(final Calendar cal, final boolean create) {
53
    static public final File getDayDir(final Calendar cal, final boolean create) {
-
 
54
        return getDayDir(getRootDir(), cal, create);
-
 
55
    }
-
 
56
 
-
 
57
    static public final File getDayDir(final File rootDir, final Calendar cal, final boolean create) {
56
        final int j = cal.get(Calendar.DAY_OF_MONTH);
58
        final int j = cal.get(Calendar.DAY_OF_MONTH);
57
        final int m = cal.get(Calendar.MONTH) + 1;
59
        final int m = cal.get(Calendar.MONTH) + 1;
58
        final int a = cal.get(Calendar.YEAR);
60
        final int a = cal.get(Calendar.YEAR);
59
        final List<String> dirs = Arrays.asList(DIGIT4_FORMAT.format(a), DIGIT2_FORMAT.format(m), DIGIT2_FORMAT.format(j));
61
        final List<String> dirs = Arrays.asList(DIGIT4_FORMAT.format(a), DIGIT2_FORMAT.format(m), DIGIT2_FORMAT.format(j));
60
        assert dirs.size() == DIR_DEPTH;
62
        assert dirs.size() == DIR_DEPTH;
61
        File res = getRootDir();
63
        File res = rootDir;
62
        for (final String dir : dirs) {
64
        for (final String dir : dirs) {
63
            res = new File(res, dir);
65
            res = new File(res, dir);
64
        }
66
        }
65
        if (create) {
67
        if (create) {
66
            try {
68
            try {
67
                FileUtils.mkdir_p(res);
69
                FileUtils.mkdir_p(res);
68
            } catch (IOException e) {
70
            } catch (final IOException e) {
69
                ExceptionHandler.handle("Impossible de créer le dossier des tickets.\n\n" + res.getAbsolutePath());
71
                ExceptionHandler.handle("Impossible de créer le dossier des tickets.\n\n" + res.getAbsolutePath());
70
            }
72
            }
71
        }
73
        }
72
        return res;
74
        return res;
73
    }
75
    }
Line 77... Line 79...
77
    }
79
    }
78
 
80
 
79
    protected static FileFilter createFF(final String prefix, final boolean includeDeleted, final boolean includeImported) {
81
    protected static FileFilter createFF(final String prefix, final boolean includeDeleted, final boolean includeImported) {
80
        return new FileFilter() {
82
        return new FileFilter() {
81
            @Override
83
            @Override
82
            public boolean accept(File f) {
84
            public boolean accept(final File f) {
83
                if (!f.isFile() || !f.getName().startsWith(prefix))
85
                if (!f.isFile() || !f.getName().startsWith(prefix))
84
                    return false;
86
                    return false;
85
                return f.getName().endsWith(EXT) || (includeDeleted && f.getName().endsWith(DELETED_EXT)) || (includeImported && f.getName().endsWith(IMPORTED_EXT));
87
                return f.getName().endsWith(EXT) || (includeDeleted && f.getName().endsWith(DELETED_EXT)) || (includeImported && f.getName().endsWith(IMPORTED_EXT));
86
            }
88
            }
87
        };
89
        };
Line 99... Line 101...
99
        final String toRm = getEnd(name, Arrays.asList(EXT, DELETED_EXT, IMPORTED_EXT));
101
        final String toRm = getEnd(name, Arrays.asList(EXT, DELETED_EXT, IMPORTED_EXT));
100
        if (toRm == null)
102
        if (toRm == null)
101
            return null;
103
            return null;
102
        try {
104
        try {
103
            return new ReceiptCode(name.substring(0, name.length() - toRm.length()));
105
            return new ReceiptCode(name.substring(0, name.length() - toRm.length()));
104
        } catch (ParseException e) {
106
        } catch (final ParseException e) {
105
            return null;
107
            return null;
106
        }
108
        }
107
    }
109
    }
108
 
110
 
109
    static public void archiveCompletelyImported() throws IOException {
111
    static public void archiveCompletelyImported() throws IOException {
110
        final File archiveDir = getRootDir(true);
112
        final File archiveDir = getRootDir(true);
111
        FileUtils.mkdir_p(archiveDir);
113
        FileUtils.mkdir_p(archiveDir);
112
        final File rootDir = getRootDir(false);
114
        final File rootDir = getRootDir(false);
113
        final List<File> dirs = FileUtils.list(rootDir, DIR_DEPTH, FileUtils.DIR_FILTER);
115
        final List<File> dirs = FileUtils.list(rootDir, DIR_DEPTH, FileUtils.DIR_FILTER);
114
        // don't archive today otherwise number will be wrong (see Ticket.initNumber())
116
        // don't archive today otherwise number will be wrong (see Ticket.initNumber())
115
        final File todayDir = getDayDir(Calendar.getInstance(), false);
117
        final File todayDir = getDayDir(rootDir, Calendar.getInstance(), false);
116
        for (final File dir : dirs) {
118
        for (final File dir : dirs) {
117
            // if all receipts are deleted or imported : archive
119
            // if all receipts are deleted or imported : archive
118
            if (!todayDir.equals(dir) && dir.listFiles(createFF("", false, false)).length == 0) {
120
            if (!todayDir.equals(dir) && dir.listFiles(createFF("", false, false)).length == 0) {
119
                final File destDir = new File(archiveDir, FileUtils.relative(rootDir, dir));
121
                final File destDir = new File(archiveDir, FileUtils.relative(rootDir, dir));
120
                FileUtils.mkParentDirs(destDir);
122
                FileUtils.mkParentDirs(destDir);
Line 237... Line 239...
237
 
239
 
238
    public final File getDir(final boolean create) {
240
    public final File getDir(final boolean create) {
239
        return getDayDir(getDay(), create);
241
        return getDayDir(getDay(), create);
240
    }
242
    }
241
 
243
 
-
 
244
    @Deprecated
-
 
245
    // use RegisterFiles
242
    public final File getFile() {
246
    public final File getFile() {
243
        return new File(getDir(true), getFileName());
247
        return new File(getDir(true), getFileName());
244
    }
248
    }
245
 
249
 
246
    public final String getFileName() {
250
    public final String getFileName() {
Line 259... Line 263...
259
        final File f = getFile();
263
        final File f = getFile();
260
        if (!f.renameTo(new File(f.getParentFile(), f.getName() + suffix)))
264
        if (!f.renameTo(new File(f.getParentFile(), f.getName() + suffix)))
261
            throw new IOException("Couldn't rename " + f);
265
            throw new IOException("Couldn't rename " + f);
262
    }
266
    }
263
 
267
 
264
    public final List<ReceiptCode> getSameDayCodes(final boolean includeAll) {
-
 
265
        final File dir = getDir(false);
-
 
266
        final File[] listFiles = dir.listFiles(createFF(getCodePrefix(), includeAll, includeAll));
-
 
267
        if (listFiles == null)
268
    @Override
268
            return Collections.emptyList();
269
    public String toString() {
269
 
-
 
270
        final List<ReceiptCode> res = new ArrayList<ReceiptCode>(listFiles.length);
270
        return this.getClass().getSimpleName() + " " + this.getCode();
271
        for (final File f : listFiles) {
-
 
272
            res.add(fromFile(f));
-
 
273
        }
-
 
274
        return res;
-
 
275
    }
271
    }
276
}
272
}