hefeixia
2021-02-18 5b8c95c760840f09910730943b21391e47187315
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package frame.object.dao;
 
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
import chat.server.call.IJSONWriter;
import chat.server.call.IJsonProvider;
import frame.persist.NamedSQL;
import frame.variant.IVariantsProvider;
import frame.variant.expression.IVariantRequestListener;
import frame.variant.expression.VariantRequestParams;
 
public class Page implements IVariantRequestListener, IVariantsProvider, IJsonProvider {  
    
    private static Set<String> parameterNames;
    private int pageSize;  
    private int recordCount;  
    private int pageNo;
  
    static {
        parameterNames = new HashSet<String>();
        parameterNames.add("pagesize");
        parameterNames.add("recordcount");
        parameterNames.add("pageno");
        parameterNames.add("beginno");   
        parameterNames.add(NamedSQL.Param_Limit);
    }
    
    public Page(int recordCount) { 
        this.pageSize = 20;
        this.pageNo = 1;
        this.recordCount = recordCount;
    }  
    
    public int getBeginRecordNo() { 
        int recordNo = pageSize * (pageNo - 1) + 1; 
        return recordNo;  
    }  
    
    public int getBeginRecordNo_1() { 
        int recordNo = getBeginRecordNo();
        return recordNo - 1;  
    } 
    
    public int getEndRecordNo() {  
        int recordNo = pageSize * pageNo;
        return Math.min(recordNo, recordCount);  
    }  
    
    public int getPageSize() {  
        return pageSize;  
    }  
  
    public int getPageNo() {  
        return pageNo;
    }  
  
    public int getRecordCount() {  
        return recordCount;  
    }  
  
    public int getPageCount() {  
        return (int)Math.ceil(recordCount * 1.0d / pageSize);
    }  
  
    public void setRecordCount(int count) {
        this.recordCount = count;
    }
  
    public void setPageSize(int value) { 
        if (value <= 0) {
            return;
        }
        
           pageSize = value;  
    }  
    
    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }
 
    public void set(String name, String value) {
        name = name.toLowerCase();
        
        if ("pageno".equals(name)) {
            setPageNo(Integer.parseInt(value));
        }
        else if ("pagesize".equals(name)) {
            setPageSize(Integer.parseInt(value));            
        }
 
    }
 
     public String toString() {
        StringBuilder result = new StringBuilder();
        
        result.append("size=").append(pageSize).append(",");
        result.append("recordCount=").append(recordCount).append(",");
        result.append("pageNo=").append(pageNo);
        
        return result.toString();
    }
 
    @Override
    public String getStringValue(String name, VariantRequestParams params) {
        if ("pageSize".equalsIgnoreCase(name)) {
            return String.valueOf(pageSize);
        }
        else if ("recordCount".equalsIgnoreCase(name)) {
            return String.valueOf(recordCount);
        }
        else if ("pageNo".equalsIgnoreCase(name)) {
            return String.valueOf(pageNo);
        }
        else if ("beginNo".equalsIgnoreCase(name)) {
            int beginNo = getBeginRecordNo_1();
            return String.valueOf(beginNo);
        }
        else if ("endNo".equalsIgnoreCase(name)) {
            int endNo = getEndRecordNo();
            return String.valueOf(endNo);
        }
        else if (NamedSQL.Param_Limit.equalsIgnoreCase(name)) {
            return getLimitSQL();
        }
        
        return null;
    }
 
 
    @Override
    public List<String> getVariantNames() {
        return new ArrayList<String>(parameterNames);
    }
 
    public static Set<String> getVarinatNameSet() {
        return parameterNames;
    }
 
    @Override
    public List<String> getVariantNameList() {
        List<String> result = new ArrayList<String>();
        result.addAll(parameterNames);
        
        return result;
    }
 
    @Override
    public boolean containsVariant(String name) {
        if (name == null) {
            return false;
        }
        
        name = name.toLowerCase();
        return parameterNames.contains(name);
    }
 
    @Override
    public Object getVariantValue(String name) {
        return getStringValue(name, null);
    }
 
    public boolean next() {
        int pageCount = getPageCount();
        
        if (pageNo < pageCount) {
            pageNo++;
            return true;
        }
        
        return false;
    }
 
    public String getLimitSQL() {
        return " limit " + getBeginRecordNo_1() + ", " + pageSize;
    }
 
    @Override
    public void writeJSONObject(IJSONWriter writer) {
        writer.beginObject();
        writeJSONData(writer);
        writer.endObject();        
    }
 
    @Override
    public void writeJSONData(IJSONWriter writer) {
        writer.write("recordcount", getRecordCount());
        writer.write("pagecount", getPageCount());
        writer.write("pagesize", getPageSize());
        writer.write("pageno", getPageNo());
        writer.write("beginrecordno", getBeginRecordNo());
        writer.write("endrecordno", getEndRecordNo());
    }
    
}