src/main/java/suwu/daopuerp/data/fileservice/FileServiceImpl.java
/**
     * save the entity
     *
     * @param entity the entity object
     * @return the entity if success else return null
     */
save the entity
@param entity the entity object
@return the entity if success else return null
save the entity @param entity the entity object @return the entity if success else return null
@Override
    public T saveTuple(T entity) {
        Class<? extends Entity> clazz = entity.getClass();
        String tableName = AnnotationUtil.getTableName(clazz);
        ArrayList<String> fileContent = new ArrayList<>();
        String id = AnnotationUtil.getKey(clazz);
        boolean isKeyAutoGenerated = AnnotationUtil.isKeyAutoGenerated(clazz);
        JSONObject json = JSONObject.fromObject(entity);
        Field idField = null;
        Field[] fields = ReflectionUtil.getAllFields(clazz);
        ArrayList<String> columns = AnnotationUtil.getAllColumnName(clazz);
        for (int i = 0; i < columns.size(); i++) {
            if (fields[i].getAnnotation(Id.class) != null) {
                idField = fields[i];
                idField.setAccessible(true);
            }
            Object object = json.remove(fields[i].getName());
            json.put(columns.get(i), object);
        }
        int maxId = 0;
        boolean isUpdate = false;
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
                new FileInputStream(savePath + tableName + fileType)))) {
            String jsonLine;
            while ((jsonLine = bufferedReader.readLine()) != null) {
                JSONObject jsonObject = JSONObject.fromObject(jsonLine);
                if (isKeyAutoGenerated && (Integer) jsonObject.get(id) > maxId) {
                    maxId = (Integer) jsonObject.get(id);
                }
                if (jsonObject.get(id).equals(json.get(id))) {
                    jsonLine = json.toString();
                    isUpdate = true;
                }
                fileContent.add(jsonLine);
            }
            if (!isUpdate) {
                if (isKeyAutoGenerated) {
                    json.element(id, maxId + 1);
                    idField.set(entity, maxId + 1);
                }
                fileContent.add(json.toString());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return null;
        }
        for (int i = 0; i < columns.size(); i++) {
            if (fields[i].getAnnotation(JsonSerialize.class) != null) {
                try {
                    if (AnnotationUtil.isKeyAutoGenerated(clazz)) {
                        int serId;
                        if (isUpdate) {
                            serId = (int) json.get(id);
                        } else {
                            serId = maxId + 1;
                        }
                        fields[i].setAccessible(true);
                        FileOutputStream fileOut =
                                new FileOutputStream(PathUtil.getSerPath() + columns.get(i) + "_" + serId);
                        ObjectOutputStream out = new ObjectOutputStream(fileOut);
                        out.writeObject(fields[i].get(entity));
                        out.close();
                        fileOut.close();
                    } else {
                        String serId = (String) json.get(id);
                        fields[i].setAccessible(true);
                        FileOutputStream fileOut =
                                new FileOutputStream(PathUtil.getSerPath() + columns.get(i) + "_" + serId);
                        ObjectOutputStream out = new ObjectOutputStream(fileOut);
                        out.writeObject(fields[i].get(entity));
                        out.close();
                        fileOut.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        try (FileWriter writer = new FileWriter(savePath + tableName + fileType)) {
            writer.write("");
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try (FileWriter writer = new FileWriter(savePath + tableName + fileType, true)) {
            for (String tuple : fileContent) {
                writer.write(tuple);
                writer.write(System.lineSeparator());
                writer.flush();
            }
            return entity;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
@ Override public T saveTuple ( T entity ) { Class < ? extends Entity > clazz = entity . getClass ( ) ; String tableName = AnnotationUtil . getTableName ( clazz ) ; ArrayList < String > fileContent = new ArrayList < > ( ) ; String id = AnnotationUtil . getKey ( clazz ) ; boolean isKeyAutoGenerated = AnnotationUtil . isKeyAutoGenerated ( clazz ) ; JSONObject json = JSONObject . fromObject ( entity ) ; Field idField = null ; Field [ ] fields = ReflectionUtil . getAllFields ( clazz ) ; ArrayList < String > columns = AnnotationUtil . getAllColumnName ( clazz ) ; for ( int i = 0 ; i < columns . size ( ) ; i ++ ) { if ( fields [ i ] . getAnnotation ( Id . class ) != null ) { idField = fields [ i ] ; idField . setAccessible ( true ) ; } Object object = json . remove ( fields [ i ] . getName ( ) ) ; json . put ( columns . get ( i ) , object ) ; } int maxId = 0 ; boolean isUpdate = false ; try ( BufferedReader bufferedReader = new BufferedReader ( new InputStreamReader ( new FileInputStream ( savePath + tableName + fileType ) ) ) ) { String jsonLine ; while ( ( jsonLine = bufferedReader . readLine ( ) ) != null ) { JSONObject jsonObject = JSONObject . fromObject ( jsonLine ) ; if ( isKeyAutoGenerated && ( Integer ) jsonObject . get ( id ) > maxId ) { maxId = ( Integer ) jsonObject . get ( id ) ; } if ( jsonObject . get ( id ) . equals ( json . get ( id ) ) ) { jsonLine = json . toString ( ) ; isUpdate = true ; } fileContent . add ( jsonLine ) ; } if ( ! isUpdate ) { if ( isKeyAutoGenerated ) { json . element ( id , maxId + 1 ) ; idField . set ( entity , maxId + 1 ) ; } fileContent . add ( json . toString ( ) ) ; } } catch ( FileNotFoundException e ) { e . printStackTrace ( ) ; return null ; } catch ( IOException e ) { e . printStackTrace ( ) ; return null ; } catch ( IllegalAccessException e ) { e . printStackTrace ( ) ; return null ; } for ( int i = 0 ; i < columns . size ( ) ; i ++ ) { if ( fields [ i ] . getAnnotation ( JsonSerialize . class ) != null ) { try { if ( AnnotationUtil . isKeyAutoGenerated ( clazz ) ) { int serId ; if ( isUpdate ) { serId = ( int ) json . get ( id ) ; } else { serId = maxId + 1 ; } fields [ i ] . setAccessible ( true ) ; FileOutputStream fileOut = new FileOutputStream ( PathUtil . getSerPath ( ) + columns . get ( i ) + "_" + serId ) ; ObjectOutputStream out = new ObjectOutputStream ( fileOut ) ; out . writeObject ( fields [ i ] . get ( entity ) ) ; out . close ( ) ; fileOut . close ( ) ; } else { String serId = ( String ) json . get ( id ) ; fields [ i ] . setAccessible ( true ) ; FileOutputStream fileOut = new FileOutputStream ( PathUtil . getSerPath ( ) + columns . get ( i ) + "_" + serId ) ; ObjectOutputStream out = new ObjectOutputStream ( fileOut ) ; out . writeObject ( fields [ i ] . get ( entity ) ) ; out . close ( ) ; fileOut . close ( ) ; } } catch ( IOException e ) { e . printStackTrace ( ) ; } catch ( IllegalAccessException e ) { e . printStackTrace ( ) ; } } } try ( FileWriter writer = new FileWriter ( savePath + tableName + fileType ) ) { writer . write ( "" ) ; writer . flush ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } try ( FileWriter writer = new FileWriter ( savePath + tableName + fileType , true ) ) { for ( String tuple : fileContent ) { writer . write ( tuple ) ; writer . write ( System . lineSeparator ( ) ) ; writer . flush ( ) ; } return entity ; } catch ( IOException e ) { e . printStackTrace ( ) ; return null ; } }
save the entity
 @param entity the entity object
 @return the entity if success else return null
save the entity @param entity the entity object @return the entity if success else return null
//将json的key变为column的name,建立序列化文件 //如果是update那么使用参数id，如果不是，那么创建新id //如果是update那么使用参数id，如果不是，那么创建新id