infoType, infoKey, subType -> FileInfo.Relation

master
mjkhan21 2 years ago
parent e67f4f0984
commit 641d3dedcf

@ -5,6 +5,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -24,6 +25,14 @@ import cokr.xit.foundation.Assert;
* @author mjkhan * @author mjkhan
*/ */
public class FileInfo { public class FileInfo {
/**
* @author mjkhan
*/
public static class Relation {
private String infoType;
private String infoKey;
private String subType;
/**objs (-) infoKey . /**objs (-) infoKey .
* @param objs * @param objs
* @return infoKey * @return infoKey
@ -38,50 +47,97 @@ public class FileInfo {
.collect(Collectors.joining("-")); .collect(Collectors.joining("-"));
} }
/** .
* @return
*/
public String getInfoType() {
return infoType;
}
/** .
* @param infoType
* @return Relation
*/
public Relation setInfoType(String infoType) {
this.infoType = infoType;
return this;
}
/** .
* @return
*/
public String getInfoKey() {
return infoKey;
}
/** .
* @param infoType
* @return Relation
*/
public Relation setInfoKey(String infoKey) {
this.infoKey = infoKey;
return this;
}
/** .
* @return
*/
public String getSubType() {
return subType;
}
/** .
* @param infoType
* @return Relation
*/
public Relation setSubType(String subType) {
this.subType = subType;
return this;
}
/** , , fileInfo .
* @param fileInfo FileInfo
*/
public void setInfo(FileInfo fileInfo) {
fileInfo.setInfoType(Assert.notEmpty(infoType, "infoType"));
fileInfo.setInfoKey(Assert.notEmpty(infoKey, "infoKey"));
fileInfo.setSubType(subType);
}
}
/** /**
* @author mjkhan * @author mjkhan
*/ */
public static class DataHolder { public static class DataHolder {
private String infoType; private Relation relation;
private String infoKey;
private String filename; private String filename;
private byte[] data; private byte[] data;
/** DataHolder . /** DataHolder .
* @param infoType * @param relation
* @param infoKey
* @param filename * @param filename
* @param data * @param data
*/ */
public DataHolder(String infoType, String infoKey, String filename, byte[] data) { public DataHolder(Relation relation, String filename, byte[] data) {
this.infoType = infoType; this.relation = relation;
this.infoKey = infoKey;
this.filename = filename; this.filename = filename;
this.data = data != null ? data : new byte[0]; this.data = data != null ? data : new byte[0];
} }
/** DataHolder . /** DataHolder .
* @param infoType * @param relation
* @param infoKey
* @param filename * @param filename
* @param data * @param data
*/ */
public DataHolder(String infoType, String infoKey, String filename, String data) { public DataHolder(Relation relation, String filename, String data) {
this(infoType, infoKey, filename, !Assert.isEmpty(data) ? Base64.getDecoder().decode(data.getBytes()) : null); this(relation, filename, !Assert.isEmpty(data) ? Base64.getDecoder().decode(data.getBytes()) : null);
}
/** .
* @return
*/
public String getInfoType() {
return infoType;
} }
/** . /** .
* @return * @return
*/ */
public String getInfoKey() { public Relation getRelation() {
return infoKey; return relation;
} }
/** . /** .
@ -121,14 +177,11 @@ public class FileInfo {
*/ */
public static class Factory extends AbstractComponent { public static class Factory extends AbstractComponent {
/** , FileInfo . /** , FileInfo .
* @param infoType * @param relation
* @param infoKey
* @param files * @param files
* @return FileInfo * @return FileInfo
*/ */
public List<FileInfo> createFileInfos(String infoType, String infoKey, Iterable<File> files) { public List<FileInfo> createFileInfos(Relation relation, Iterable<File> files) {
notEmpty(infoType, "infoType");
notEmpty(infoKey, "infoKey");
if (isEmpty(files)) if (isEmpty(files))
return Collections.emptyList(); return Collections.emptyList();
@ -138,8 +191,7 @@ public class FileInfo {
if (file == null) continue; if (file == null) continue;
FileInfo info = new FileInfo(); FileInfo info = new FileInfo();
info.setInfoType(infoType); relation.setInfo(info);
info.setInfoKey(infoKey);
String filename = file.getName(); String filename = file.getName();
info.setName(filename); info.setName(filename);
InputStream input = new FileInputStream(file); InputStream input = new FileInputStream(file);
@ -158,23 +210,21 @@ public class FileInfo {
} }
/** , FileInfo . /** , FileInfo .
* @param infoType * @param relation
* @param infoKey
* @param files * @param files
* @return FileInfo * @return FileInfo
*/ */
public List<FileInfo> createFileInfos(String infoType, String infoKey, File... files) { public List<FileInfo> createFileInfos(Relation relation, File... files) {
return createFileInfos(infoType, infoKey, Arrays.asList(files)); return createFileInfos(relation, Arrays.asList(files));
} }
/** , FileInfo . /** , FileInfo .
* @param infoType * @param relation
* @param infoKey
* @param file * @param file
* @return FileInfo * @return FileInfo
*/ */
public FileInfo create(String infoType, String infoKey, File file) { public FileInfo create(Relation relation, File file) {
List<FileInfo> fileInfos = createFileInfos(infoType, infoKey, file); List<FileInfo> fileInfos = createFileInfos(relation, file);
return !fileInfos.isEmpty() ? fileInfos.get(0) : null; return !fileInfos.isEmpty() ? fileInfos.get(0) : null;
} }
@ -191,8 +241,7 @@ public class FileInfo {
if (holder == null) continue; if (holder == null) continue;
FileInfo info = new FileInfo(); FileInfo info = new FileInfo();
info.setInfoType(holder.infoType); holder.getRelation().setInfo(info);
info.setInfoKey(holder.infoKey);
String filename = holder.getFilename(); String filename = holder.getFilename();
info.setName(filename); info.setName(filename);
InputStream input = new ByteArrayInputStream(holder.data); InputStream input = new ByteArrayInputStream(holder.data);
@ -327,9 +376,11 @@ public class FileInfo {
/** . /** .
* @param subType * @param subType
* @return FileInfo
*/ */
public void setSubType(String subType) { public FileInfo setSubType(String subType) {
this.subType = subType; this.subType = subType;
return this;
} }
/** . /** .
@ -530,18 +581,18 @@ public class FileInfo {
return this; return this;
} }
/**FileInfo File . /**OutputStream File .
* @param out OutputStream
* @return * @return
* <ul><li> true</li> * <ul><li> true</li>
* <li> false</li> * <li> false</li>
* </ul> * </ul>
*/ */
public boolean write() { public boolean write(OutputStream out) {
if (Assert.isEmpty(path)) return false; if (Assert.isEmpty(path) || out == null) return false;
try ( try (
InputStream in = input; InputStream in = input;
FileOutputStream out = new FileOutputStream(path);
) { ) {
FileCopyUtils.copy(in, out); FileCopyUtils.copy(in, out);
return true; return true;
@ -550,6 +601,20 @@ public class FileInfo {
} }
} }
/**FileInfo File .
* @return
* <ul><li> true</li>
* <li> false</li>
* </ul>
*/
public boolean write() {
try (FileOutputStream out = new FileOutputStream(path)) {
return write(out);
} catch(Exception e) {
throw Assert.runtimeException(e);
}
}
/**FileInfo File . /**FileInfo File .
* @return * @return
* <ul><li> true</li> * <ul><li> true</li>

@ -51,6 +51,7 @@ public class FileController extends AbstractController {
/** . /** .
* @param infoType * @param infoType
* @param infoKey * @param infoKey
* @param infoKey
* @param uploads * @param uploads
* @return ModelAndView * @return ModelAndView
* <pre><code>{ * <pre><code>{
@ -62,9 +63,13 @@ public class FileController extends AbstractController {
public ModelAndView create( public ModelAndView create(
String infoType, String infoType,
String infoKey, String infoKey,
String subType,
MultipartFile[] uploads MultipartFile[] uploads
) { ) {
List<FileInfo> files = new FileInfoFactory().makeFileInfos(infoType, infoKey, uploads); List<FileInfo> files = new FileInfoFactory().makeFileInfos(
new FileInfo.Relation().setInfoType(infoType).setInfoKey(infoKey).setSubType(subType),
uploads
);
int affected = fileService.create(files); int affected = fileService.create(files);
return new ModelAndView("jsonView") return new ModelAndView("jsonView")
.addObject("affected", affected) .addObject("affected", affected)

@ -14,12 +14,11 @@ import cokr.xit.base.file.FileInfo;
*/ */
public class FileInfoFactory extends FileInfo.Factory { public class FileInfoFactory extends FileInfo.Factory {
/** , FileInfo . /** , FileInfo .
* @param infoType * @param relation
* @param infoKey
* @param multipartFiles * @param multipartFiles
* @return FileInfo * @return FileInfo
*/ */
public List<FileInfo> makeFileInfos(String infoType, String infoKey, Iterable<MultipartFile> multipartFiles) { public List<FileInfo> makeFileInfos(FileInfo.Relation relation, Iterable<MultipartFile> multipartFiles) {
if (isEmpty(multipartFiles)) if (isEmpty(multipartFiles))
return Collections.emptyList(); return Collections.emptyList();
@ -35,8 +34,7 @@ public class FileInfoFactory extends FileInfo.Factory {
info.setSize(file.getSize()); info.setSize(file.getSize());
info.setInputStream(file.getInputStream()); info.setInputStream(file.getInputStream());
info.setSortOrder(result.size()); info.setSortOrder(result.size());
info.setInfoType(infoType); relation.setInfo(info);
info.setInfoKey(infoKey);
result.add(info); result.add(info);
} catch (Exception e) { } catch (Exception e) {
throw runtimeException(e); throw runtimeException(e);
@ -45,23 +43,21 @@ public class FileInfoFactory extends FileInfo.Factory {
} }
/** , FileInfo . /** , FileInfo .
* @param infoType * @param relation
* @param infoKey
* @param multipartFiles * @param multipartFiles
* @return FileInfo * @return FileInfo
*/ */
public List<FileInfo> makeFileInfos(String infoType, String infoKey, MultipartFile... multipartFiles) { public List<FileInfo> makeFileInfos(FileInfo.Relation relation, MultipartFile... multipartFiles) {
return makeFileInfos(infoType, infoKey, Arrays.asList(multipartFiles)); return makeFileInfos(relation, Arrays.asList(multipartFiles));
} }
/** , FileInfo . /** , FileInfo .
* @param infoType * @param relation
* @param infoKey
* @param multipartFile * @param multipartFile
* @return FileInfo * @return FileInfo
*/ */
public FileInfo make(String infoType, String infoKey, MultipartFile multipartFile) { public FileInfo make(FileInfo.Relation relation, MultipartFile multipartFile) {
List<FileInfo> files = makeFileInfos(infoType, infoKey, multipartFile); List<FileInfo> files = makeFileInfos(relation, multipartFile);
return !files.isEmpty() ? files.get(0) : null; return !files.isEmpty() ? files.get(0) : null;
} }
} }

@ -27,8 +27,9 @@ public class FileServiceTest extends TestSupport {
String infoType = "00"; String infoType = "00";
String infoKey = "info-key-00"; String infoKey = "info-key-00";
FileQuery req = new FileQuery().setInfoType(infoType); FileQuery req = new FileQuery().setInfoType(infoType);
FileInfo.Relation relation = new FileInfo.Relation().setInfoType(infoType).setInfoKey(infoKey);
List<FileInfo> fileInfos = new FileInfo.Factory().createFileInfos(infoType, infoKey, files); List<FileInfo> fileInfos = new FileInfo.Factory().createFileInfos(relation, files);
Assertions.assertTrue(service.create(fileInfos) > 0); Assertions.assertTrue(service.create(fileInfos) > 0);
fileInfos.forEach(System.out::println); fileInfos.forEach(System.out::println);

@ -20,7 +20,7 @@
</resultMap> </resultMap>
<sql id="fileDirs">SELECT INF_TYPE <sql id="fileDirs">SELECT INF_TYPE
, CONCAT('D://workspace/xit/base/files/', DIR, TO_CHAR(CURRENT_DATE, 'YYYY/MM/DD/')) DIR , CONCAT('C://workspace/xit/base/files/', DIR, TO_CHAR(CURRENT_DATE, 'YYYY/MM/DD/')) DIR
FROM ( FROM (
SELECT '00' INF_TYPE, 'attachment/' DIR UNION SELECT '00' INF_TYPE, 'attachment/' DIR UNION
SELECT '01' INF_TYPE, 'document/' DIR UNION SELECT '01' INF_TYPE, 'document/' DIR UNION
@ -29,12 +29,12 @@
<sql id="selectFiles"> <sql id="selectFiles">
<if test="fileIDs != null"> <if test="fileIDs != null">
SELECT A.*, REPLACE(FILE_PATH, 'D://workspace/xit/base', '') URL SELECT A.*, REPLACE(FILE_PATH, 'C://workspace/xit/base', '') URL
FROM TBL_FILE A FROM TBL_FILE A
WHERE FILE_ID IN (<foreach collection="fileIDs" item="fileID" separator=",">#{fileID}</foreach>) WHERE FILE_ID IN (<foreach collection="fileIDs" item="fileID" separator=",">#{fileID}</foreach>)
ORDER BY FILE_ID</if> ORDER BY FILE_ID</if>
<if test="fileIDs == null"><include refid="utility.paging-prefix" /> <if test="fileIDs == null"><include refid="utility.paging-prefix" />
SELECT A.*, REPLACE(FILE_PATH, 'D://workspace/xit/base', '') URL SELECT A.*, REPLACE(FILE_PATH, 'C://workspace/xit/base', '') URL
FROM TBL_FILE A FROM TBL_FILE A
<where> <where>
<if test="infoType != null"> AND A.INF_TYPE = #{infoType}</if> <if test="infoType != null"> AND A.INF_TYPE = #{infoType}</if>

Loading…
Cancel
Save