Statistics
| Revision:

root / sqs-reader / src / main / java / net / sqs2 / omr / result / path / MultiRowMultiQuestionPageSelection.java @ 1847

History | View | Annotate | Download (3.2 KB)

1
package net.sqs2.omr.result.path;
2
3
import java.util.ArrayList;
4
import java.util.List;
5
6
import net.sqs2.omr.master.FormArea;
7
import net.sqs2.omr.model.Answer;
8
import net.sqs2.omr.model.Row;
9
import net.sqs2.omr.model.RowAccessor;
10
import net.sqs2.omr.model.RowID;
11
12
public class MultiRowMultiQuestionPageSelection extends MultiRowMultiQuestionSelection {
13
14
        public static final int NUM_MAX_ITEMS_WITHIN_A_PAGE = 50;
15
        List<Answer> answerList;
16
        
17
        public MultiRowMultiQuestionPageSelection(MultiRowMultiQuestionPagePath multiRowMultiQuestionPagePath) {
18
                this(multiRowMultiQuestionPagePath, NUM_MAX_ITEMS_WITHIN_A_PAGE);
19
        }
20
        
21
        public MultiRowMultiQuestionPageSelection(MultiRowMultiQuestionPagePath multiRowMultiQuestionPagePath, int numMaxItems) {
22
                super(multiRowMultiQuestionPagePath);
23
                
24
                int startIndex = multiRowMultiQuestionPagePath.getStartIndex();
25
                int endIndex = multiRowMultiQuestionPagePath.getEndIndex();
26
                
27
                RowAccessor rowAccessor = sessionSource.getContentAccessor().getRowAccessor();
28
29
                if(0 <= startIndex && endIndex < 0){
30
                        answerList = forwardSearch(rowAccessor, numMaxItems);
31
                }else if(0 <= endIndex && startIndex < 0){
32
                        answerList = backwardSearch(rowAccessor, numMaxItems);
33
                }else{
34
                        throw new IllegalArgumentException();
35
                }
36
        }
37
        
38
        private ArrayList<Answer> forwardSearch(RowAccessor rowAccessor, int numMaxItems){
39
                // forward search
40
                return forwardSearch(rowAccessor, new ArrayList<Answer>(), numMaxItems);        
41
        }
42
        
43
        private ArrayList<Answer> forwardSearch(RowAccessor rowAccessor, ArrayList<Answer> answerList, int numMaxItems){
44
                // forward search
45
                int numFocused = 0;
46
                ITEM_LOOP: for(RowID rowID: rowIDList){
47
                        Row row = rowAccessor.get(formMaster.getRelativePath(), rowID.getSpreadSheet().getSourceDirectory().getRelativePath(), rowID.getRowIndex());
48
                        for(List<FormArea> formAreaList: getFormAreaListList()){
49
                                if(numFocused < numMaxItems){
50
                                        int questionIndex = formAreaList.get(0).getQuestionIndex();
51
                                        answerList.add(row.getAnswer(questionIndex));
52
                                        numFocused++;
53
                                }else{
54
                                        break ITEM_LOOP;
55
                                }
56
                        }
57
                }
58
                return answerList;
59
        }
60
        
61
        private ArrayList<Answer> backwardSearch(RowAccessor rowAccessor, int numMaxItems){
62
                // forward search
63
                return backwardSearch(rowAccessor, new ArrayList<Answer>(), numMaxItems);        
64
        }
65
        
66
        private ArrayList<Answer> backwardSearch(RowAccessor rowAccessor, ArrayList<Answer> answerList, int numMaxItems){
67
                // backward search
68
                int numFocused = 0;
69
                ITEM_LOOP: for(int rowIndex = rowIDList.size() - 1; 0 <= rowIndex; rowIndex--){
70
                        RowID rowID = rowIDList.get(rowIndex);
71
                        Row row = rowAccessor.get(formMaster.getRelativePath(), rowID.getSpreadSheet().getSourceDirectory().getRelativePath(), rowID.getRowIndex());
72
                        List<List<FormArea>> formAreaListList = getFormAreaListList();
73
                        for(int columnIndex = formAreaListList.size() - 1; 0 <= columnIndex; columnIndex--){
74
                                 List<FormArea> formAreaList = formAreaListList.get(columnIndex);
75
                                if(numFocused < numMaxItems){
76
                                        int questionIndex = formAreaList.get(0).getQuestionIndex();
77
                                        answerList.add(row.getAnswer(questionIndex));
78
                                        numFocused++;
79
                                }else{
80
                                        break ITEM_LOOP;
81
                                }
82
                        }
83
                }
84
                return answerList;
85
        }
86
}