stationversionaction.java
来自「工厂版本管理系统,STRUTS2框架,用于管理商品的版本,便于有效的控制版本」· Java 代码 · 共 285 行
JAVA
285 行
package com.utstar.fcs.web.struts.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
import com.opensymphony.xwork2.Preparable;
import com.utstar.fcs.domain.workinstruction.Field;
import com.utstar.fcs.domain.workinstruction.FieldDefinition;
import com.utstar.fcs.domain.workinstruction.FieldFactory;
import com.utstar.fcs.domain.workinstruction.FieldType;
import com.utstar.fcs.domain.workinstruction.Station;
import com.utstar.fcs.domain.workinstruction.StationVersion;
import com.utstar.fcs.domain.workinstruction.field.FileField;
import com.utstar.fcs.domain.workinstruction.field.TableField;
import com.utstar.fcs.domain.workinstruction.field.TableRow;
public class StationVersionAction extends BaseAction implements Preparable {
/**
*
*/
private static final long serialVersionUID = 1L;
// form 1
private Long stationId;
private StationVersion stationVersion;
private String save;
private String saveAsNew;
// form bean
private Map<String, FileField> fileFields = new HashMap<String, FileField>();
private Map tableFields = new HashMap();
public StationVersionAction() {
}
public void prepare() throws Exception {
stationVersion = new StationVersion();
Station station = (Station) getReposity().get(Station.class, stationId);
stationVersion.setStation(station);
}
public String input() {
return INPUT;
}
public String edit() {
stationVersion = (StationVersion) getReposity().get(
StationVersion.class, stationVersion.getId());
return INPUT;
}
/*
* fdid1_row_col, start from 0 to csv
*/
private String getTableParameter(String parameterName,
HttpServletRequest request) {
String[][] table = new String[100][5];
Enumeration<String> names = request.getParameterNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
if (name.startsWith(parameterName)) {
String[] parts = name.split("_");
try {
int row = Integer.parseInt(parts[1]);
int col = Integer.parseInt(parts[2]);
table[row][col] = request.getParameter(name);
} catch (Exception e) {// ignor the parse exception,
}
}
}
TableField tf = new TableField();
for (int row = 0; row < 100; row++) {
TableRow tr = new TableRow();
for (int col = 0; col < 5; col++) {
tr.setCol(col, table[row][col]);
}
if (!tr.isEmpty())
tf.AddRow(tr);
}
return tf.toString();
}
private byte[] getByteArrayFromFile(java.io.File file) throws IOException {
byte[] buffer = new byte[1023 * 1024 * 10];
InputStream is = new FileInputStream(file);
int len = is.read(buffer);
is.close();
byte[] data = new byte[len];
for (int i = 0; i < len; i++)
data[i] = buffer[i];
return data;
}
private FileField getFileField(String parameterName,
HttpServletRequest request) {
// read file fields
MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) request;
Enumeration fileParameterNames = multiWrapper.getFileParameterNames();
while (fileParameterNames != null
&& fileParameterNames.hasMoreElements()) {
// get the value of this input tag
String inputName = (String) fileParameterNames.nextElement();
// get the content type
String[] contentType = multiWrapper.getContentTypes(inputName);
File[] file = multiWrapper.getFiles(inputName);
if (contentType.length > 0 && inputName.equals(parameterName)) {
// get the name of the file from the input tag
String[] fileName = multiWrapper.getFileNames(inputName);
FileField ff = new FileField();
ff.setFileName(fileName[0]);
ff.setContentType(contentType[0]);
byte[] ba = null;
try {
ba = getByteArrayFromFile(file[0]);
} catch (Exception e) {
e.printStackTrace();
}
ff.setData(ba);
// System.out.println("upload file:" + inputName + ","
// + contentType[0] + "," + fileName[0]);
return ff;
}
}
return null;
}
public String save() {
// TODO save the form
HttpServletRequest request = ServletActionContext.getRequest();
Iterator<FieldDefinition> fdIt = stationVersion.getStation()
.getStationType().getFieldDefinitions().iterator();
while (fdIt.hasNext()) {
FieldDefinition fd = fdIt.next();
String parameterName = fd.getStringId();
String parameterValue = request.getParameter(parameterName);
Field field = FieldFactory.createFieldInstance(fd);
field.setFieldDefinition(fd);
if (fd.getFieldType().equals(FieldType.TableField)) {
parameterValue = getTableParameter(parameterName, request);
field.setValue(parameterValue);
stationVersion.addField(field);
} else if (fd.getFieldType().equals(FieldType.FileField)) {
field = getFileField(parameterName, request);
if (field != null) {
field.setFieldDefinition(fd);
stationVersion.addField(field);
}
} else {
field.setValue(parameterValue);
stationVersion.addField(field);
}
if (field != null) {
System.out.println("get parameter: name:" + fd.getName()
+ ",value:" + field.getStringValue());
}
}
// save or update entity to DB
if (stationVersion.getId() == null) {
//save
getWorkInstructionService().addStationVersion(stationVersion.getStation(), stationVersion);
} else {
// we have to load the entity in DB, otherwise the FileFields will
// be lost
StationVersion entity = (StationVersion) getReposity().get(
StationVersion.class, stationVersion.getId());
// clear all fields except fileFields
Iterator<Field> it = entity.getFields().values().iterator();
while (it.hasNext()) {
Field f = it.next();
if (!f.getFieldDefinition().getFieldType().equals(
FieldType.FileField))
it.remove();
}
// copy the fields to entity
it = stationVersion.getFields().values().iterator();
while (it.hasNext()) {
Field f = it.next();
entity.addField(f);
}
if(save != null)
getReposity().update(entity);
else//saveAsNew, clone it
{
StationVersion sv2 = entity.cloneEntity();
getWorkInstructionService().addStationVersion(stationVersion.getStation(), sv2);
}
}
return "autoclose";
}
public String saveAsNewVersion() {
return "autoclose";
}
public Long getStationId() {
return stationId;
}
public void setStationId(Long stationId) {
this.stationId = stationId;
}
public StationVersion getStationVersion() {
return stationVersion;
}
public void setStationVersion(StationVersion stationVersion) {
this.stationVersion = stationVersion;
}
public Map getTableFields() {
return tableFields;
}
public void setTableFields(Map tableFields) {
this.tableFields = tableFields;
}
public String getSave() {
return save;
}
public void setSave(String save) {
this.save = save;
}
public String getSaveAsNew() {
return saveAsNew;
}
public void setSaveAsNew(String saveAsNew) {
this.saveAsNew = saveAsNew;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?