提交 0f5bacd4 编写于 作者: O o2null

Merge branch 'fix/generalfile' into 'wrdp'

修改generalfile

See merge request o2oa/o2oa!2229
package com.x.general.assemble.control.jaxrs.general;
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.project.config.StorageMapping;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.jaxrs.WoFile;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.general.assemble.control.ThisApplication;
import com.x.general.core.entity.general.File;
public class ActionGet extends BaseAction {
private static Logger logger = LoggerFactory.getLogger(ActionGet.class);
protected ActionResult<Wo> execute(EffectivePerson effectivePerson, String flag) throws Exception {
logger.debug(effectivePerson, "flag:{}.", flag);
ActionResult<Wo> result = new ActionResult<>();
Wo wo = null;
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
File generalFile = emc.find(flag, File.class);
if(generalFile!=null){
StorageMapping gfMapping = ThisApplication.context().storageMappings().get(File.class,
generalFile.getStorage());
wo = new Wo(generalFile.readContent(gfMapping), this.contentType(false, generalFile.getName()),
this.contentDisposition(false, generalFile.getName()));
result.setData(wo);
generalFile.deleteContent(gfMapping);
emc.beginTransaction(File.class);
emc.delete(File.class, generalFile.getId());
emc.commit();
} else {
throw new ExceptionInputFileObject(flag);
}
}
result.setData(wo);
return result;
}
public static class Wo extends WoFile {
public Wo(byte[] bytes, String contentType, String contentDisposition) {
super(bytes, contentType, contentDisposition);
}
}
}
package com.x.general.assemble.control.jaxrs.general;
import com.x.base.core.project.jaxrs.StandardJaxrsAction;
abstract class BaseAction extends StandardJaxrsAction {
}
package com.x.general.assemble.control.jaxrs.general;
import com.x.base.core.project.exception.PromptException;
public class ExceptionInputFileObject extends PromptException {
private static final long serialVersionUID = 9085364457175859374L;
ExceptionInputFileObject(String flag) {
super("对象不存在:{}.", flag);
}
}
\ No newline at end of file
package com.x.general.assemble.control.jaxrs.general;
import com.x.base.core.project.annotation.JaxrsDescribe;
import com.x.base.core.project.annotation.JaxrsMethodDescribe;
import com.x.base.core.project.annotation.JaxrsParameterDescribe;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.jaxrs.ResponseFactory;
import com.x.base.core.project.jaxrs.StandardJaxrsAction;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
@Path("generalfile")
@JaxrsDescribe("获取附件")
public class FileAction extends StandardJaxrsAction {
private static Logger logger = LoggerFactory.getLogger(FileAction.class);
@JaxrsMethodDescribe(value = "获取附件.", action = ActionGet.class)
@GET
@Path("flag/{flag}")
@Consumes(MediaType.APPLICATION_JSON)
public void getResult(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request,
@JaxrsParameterDescribe("附件标记") @PathParam("flag") String flag) {
ActionResult<ActionGet.Wo> result = new ActionResult<>();
EffectivePerson effectivePerson = this.effectivePerson(request);
try {
result = new ActionGet().execute(effectivePerson, flag);
} catch (Exception e) {
logger.error(e, effectivePerson, request, null);
result.error(e);
}
asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
}
}
\ No newline at end of file
package com.x.general.core.entity.general;
import com.x.base.core.entity.JpaObject;
import com.x.base.core.entity.Storage;
import com.x.base.core.entity.StorageObject;
import com.x.base.core.entity.StorageType;
import com.x.base.core.entity.annotation.CheckPersist;
import com.x.base.core.entity.annotation.ContainerEntity;
import com.x.base.core.project.annotation.FieldDescribe;
import com.x.base.core.project.tools.DateTools;
import com.x.general.core.entity.PersistenceProperties;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.openjpa.persistence.jdbc.Index;
import javax.persistence.*;
import java.util.Date;
@ContainerEntity(dumpSize = 10, type = ContainerEntity.Type.content, reference = ContainerEntity.Reference.strong)
@Entity
@Table(name = PersistenceProperties.General.File.table, uniqueConstraints = {
@UniqueConstraint(name = PersistenceProperties.General.File.table + JpaObject.IndexNameMiddle
+ JpaObject.DefaultUniqueConstraintSuffix, columnNames = { JpaObject.IDCOLUMN,
JpaObject.CREATETIMECOLUMN, JpaObject.UPDATETIMECOLUMN, JpaObject.SEQUENCECOLUMN }) })
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Storage(type = StorageType.general)
public class File extends StorageObject {
private static final long serialVersionUID = -8883987079043800355L;
private static final String TABLE = PersistenceProperties.General.File.table;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@FieldDescribe("数据库主键,自动生成.")
@Id
@Column(length = length_id, name = ColumnNamePrefix + id_FIELDNAME)
private String id = createId();
/* 以上为 JpaObject 默认字段 */
public void onPersist() throws Exception {
this.lastUpdateTime = new Date();
/* 如果扩展名为空去掉null */
this.extension = StringUtils.trimToEmpty(extension);
}
/* 更新运行方法 */
public File() {
}
public File(String storage, String name, String person) throws Exception {
if (StringUtils.isEmpty(storage)) {
throw new Exception("storage can not be empty.");
}
if (StringUtils.isEmpty(name)) {
throw new Exception("name can not be empty.");
}
if (StringUtils.isEmpty(person)) {
throw new Exception("person can not be empty.");
}
this.storage = storage;
Date now = new Date();
this.setCreateTime(now);
this.lastUpdateTime = now;
this.name = name;
this.extension = StringUtils.lowerCase(FilenameUtils.getExtension(name));
this.person = person;
if (null == this.extension) {
throw new Exception("extension can not be null.");
}
}
@Override
public String path() throws Exception {
if (StringUtils.isEmpty(id)) {
throw new Exception("id can not be empty.");
}
String str = DateTools.compactDate(this.getCreateTime());
str += PATHSEPARATOR;
str += this.id;
str += StringUtils.isEmpty(this.extension) ? "" : ("." + this.extension);
return str;
}
@Override
public String getStorage() {
return storage;
}
@Override
public void setStorage(String storage) {
this.storage = storage;
}
@Override
public Long getLength() {
return length;
}
@Override
public void setLength(Long length) {
this.length = length;
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public String getExtension() {
return extension;
}
@Override
public void setExtension(String extension) {
this.extension = extension;
}
@Override
public Date getLastUpdateTime() {
return lastUpdateTime;
}
@Override
public void setLastUpdateTime(Date lastUpdateTime) {
this.lastUpdateTime = lastUpdateTime;
}
@Override
public Boolean getDeepPath() {
return BooleanUtils.isTrue(this.deepPath);
}
@Override
public void setDeepPath(Boolean deepPath) {
this.deepPath = deepPath;
}
public static final String person_FIELDNAME = "person";
@FieldDescribe("上传用户.")
@Column(length = length_255B, name = ColumnNamePrefix + person_FIELDNAME)
@Index(name = TABLE + IndexNameMiddle + person_FIELDNAME)
@CheckPersist(allowEmpty = true)
private String person;
public static final String name_FIELDNAME = "name";
@FieldDescribe("文件名称.")
@Column(length = length_255B, name = ColumnNamePrefix + name_FIELDNAME)
@Index(name = TABLE + IndexNameMiddle + name_FIELDNAME)
@CheckPersist(allowEmpty = false)
private String name;
public static final String extension_FIELDNAME = "extension";
@FieldDescribe("扩展名,必须要有扩展名的文件才允许上传.")
@Column(length = JpaObject.length_64B, name = ColumnNamePrefix + extension_FIELDNAME)
@CheckPersist(allowEmpty = false, fileNameString = true)
private String extension;
public static final String storage_FIELDNAME = "storage";
@FieldDescribe("存储器的名称,也就是多个存放节点的名字.")
@Column(length = JpaObject.length_64B, name = ColumnNamePrefix + storage_FIELDNAME)
@CheckPersist(allowEmpty = false, simplyString = true)
private String storage;
public static final String length_FIELDNAME = "length";
@FieldDescribe("文件大小.")
@Column(name = ColumnNamePrefix + length_FIELDNAME)
@CheckPersist(allowEmpty = true)
private Long length;
public static final String lastUpdateTime_FIELDNAME = "lastUpdateTime";
@FieldDescribe("最后更新时间")
@Column(name = ColumnNamePrefix + lastUpdateTime_FIELDNAME)
@Index(name = TABLE + IndexNameMiddle + lastUpdateTime_FIELDNAME)
@CheckPersist(allowEmpty = false)
private Date lastUpdateTime;
public static final String deepPath_FIELDNAME = "deepPath";
@FieldDescribe("是否使用更深的路径.")
@CheckPersist(allowEmpty = true)
@Column(name = ColumnNamePrefix + deepPath_FIELDNAME)
private Boolean deepPath;
public String getPerson() {
return person;
}
public void setPerson(String person) {
this.person = person;
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册