我正在开发一个Web应用程序,我必须完成的任务之一就是添加excel导入功能,该功能将读取xlsx文件并将其转换为抽象类列表。
将其转换为类很容易,并且有很多教程介绍了如何做到这一点,但是抽象类却没有。
抽象类称为AbstractQuestion,它代表一个问题,并且存在多种类型的问题,例如单选问题,多选问题和文本问题。
基本上,excel文件的每一行可以是不同类型的问题,我需要在最后返回一个AbstractQuestion列表。此外,单选题最多可以有10个选择。
AbstractQuestion:
@SuppressWarnings("serial")
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "question_type", discriminatorType =
DiscriminatorType.STRING)
public abstract class AbstractQuestion implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name="code", nullable=false)
private String code;
@Column(name="question", nullable=false)
private String question;
@Version
private long version;
private @OneToMany(mappedBy="question")
List<AbstractAnswer> answers = new ArrayList<AbstractAnswer>();
public AbstractAnswer currentUserAnswer;
public AbstractQuestion() {
super();
// TODO Auto-generated constructor stub
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public long getVersion() {
return version;
}
public void setVersion(long version) {
this.version = version;
}
public List<AbstractAnswer> getAnswers() {
return answers;
}
public void setAnswers(List<AbstractAnswer> answers) {
this.answers = answers;
}
public void putCurrentUserAnswer(AbstractAnswer answer) {
this.currentUserAnswer = answer;
}
public abstract void accept(QuestionVisitor q);
public abstract AbstractAnswer createAnswer(User user);
}
文字问题:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("Text")
public class TextQuestion extends AbstractQuestion {
public TextQuestion() {
super();
}
@Override
public void accept(QuestionVisitor q) {
q.process(this);
}
public String getType() {
return QuestionUtility.TEXT_QUESTION;
}
@Override
public AbstractAnswer createAnswer(User user) {
TextAnswer answer = new TextAnswer();
answer.setAnswerer(user);
answer.setQuestion(this);
answer.setId(0);
answer.setVersion(0);
answer.setAnswered(false);
return answer;
}
}
单选题:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("SingleChoice")
public class SingleChoiceQuestion extends AbstractQuestion {
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
@JoinTable(name = "singlechoice_options", joinColumns = {
@JoinColumn(name= "parent" )
},inverseJoinColumns = {
@JoinColumn(name = "child")
}
)
private List<Option> options = new ArrayList<Option>();
public SingleChoiceQuestion() {
super();
// TODO Auto-generated constructor stub
}
@Override
public void accept(QuestionVisitor q) {
q.process(this);
}
public String getType() {
return QuestionUtility.SINGLE_CHOICE_QUESTION;
}
public List<Option> getOptions() {
return options;
}
public void setOptions(List<Option> options) {
this.options = options;
}
@Override
public AbstractAnswer createAnswer(User user) {
SingleChoiceAnswer answer = new SingleChoiceAnswer();
answer.setAnswerer(user);
answer.setQuestion(this);
answer.setId(0);
answer.setVersion(0);
answer.setAnswered(false);
return answer;
}
}
多项选择题:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("MultipleChoice")
public class MultipleChoiceQuestion extends AbstractQuestion {
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
@JoinTable(name = "multiplechoice_options", joinColumns = {
@JoinColumn(name= "parent" )
},inverseJoinColumns = {
@JoinColumn(name = "child")
}
)
private List<Option> options = new ArrayList<Option>();
public MultipleChoiceQuestion() {
super();
// TODO Auto-generated constructor stub
}
@Override
public void accept(QuestionVisitor q) {
q.process(this);
}
public String getType() {
return QuestionUtility.MULTIPLE_CHOICE_QUESTION;
}
public List<Option> getOptions() {
return options;
}
public void setOptions(List<Option> options) {
this.options = options;
}
@Override
public AbstractAnswer createAnswer(User user) {
MultipleChoiceAnswer answer = new MultipleChoiceAnswer();
answer.setAnswerer(user);
answer.setQuestion(this);
answer.setId(0);
answer.setVersion(0);
answer.setAnswered(false);
return answer;
}
}
选项:
@SuppressWarnings("serial")
@Entity
@Table(name = "option")
public class Option implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "option", nullable=false)
private String option;
@Column(name = "weight", nullable=false)
private int weight;
@Version
private int version;
public Option() {
super();
// TODO Auto-generated constructor stub
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getOption() {
return option;
}
public void setOption(String option) {
this.option = option;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
}
然后让我们想象一下这个excel文件: xlsx file
目标是返回AbstractQuestion的列表。有可能做到吗?如果是这样,我该怎么办?任何可能的解决方案将不胜感激。
谢谢。