@Override
    public Attachment store(Attachment a) throws IOException {

        if (null == getAttachment(a.getID())) {
            InputStream is = a.getInputStream();

            if (null == is) {
                throw new IllegalArgumentException("The InputStream of Attachment could not be null!");
            }
            String filename = a.getName();
            String fName = filename;
            String fType = "";
            if (filename.lastIndexOf(".") != -1) {
                fName = filename.substring(0, filename.lastIndexOf("."));
                fType = filename.substring(filename.lastIndexOf(".") + 1);
            }
            if(null==a.getUploadTime())
                a.setUploadTime(Calendar.getInstance().getTime());

            String date = String.format("%1$tY-%1$tm-%1$td", a.getUploadTime());
            String year = String.format("%1$tY", a.getUploadTime());
            String subDir = year + "/" + date;
            File afolder = new File(repoDirectory, subDir);
            if ((!afolder.exists()) && (!afolder.mkdirs())) {
                throw new RuntimeException("Can't create the attachment folder");
            }
            Path af = java.nio.file.Paths.get(repositoryPath, subDir, filename);
            int i = 1;
            while (Files.exists(af)) {
                filename = fName + "(" + i + ")." + fType;
                i++;
                af = java.nio.file.Paths.get(repositoryPath, subDir, filename);
            }
            String relPath = subDir + "/" + filename;
            int size = (int) Files.copy(is, af, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
            is.close();
            DBAttachment dba=null;
            if (a instanceof DBFileAttachment) {
                dba = ((DBFileAttachment) a).dba;
                
                

            } else {
                dba = new DBAttachment(a.getID());
                dba.setName(a.getName());
                dba.setAttacher(a.getAttacher());
                dba.setBelonger(a.getBelonger());
                dba.setContentType(a.getContentType());                
                dba.setUploadTime(a.getUploadTime());
              
                

            }
            dba.setRelPath(relPath);
            dba.setSize(size);
            this.edit(dba);
            return this.getAttachment(a.getID());

        }
        return null;
    }