001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.gl.report;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import org.kuali.kfs.sys.KFSConstants;
022    
023    /**
024     * This class represents a summary amount used in reporst
025     */
026    public class Summary implements Comparable {
027        /**
028         * This number is used by TransactionReport when sorting the list of Summary objects passed to
029         * TransactionReport.generateReport(). Lowest number prints first.
030         */
031        public static final int TOTAL_RECORD_COUNT_SUMMARY_SORT_ORDER = 1;
032        public static final int SELECTED_RECORD_COUNT_SUMMARY_SORT_ORDER = 2;
033        public static final int SEQUENCE_RECORDS_WRITTEN_SUMMARY_SORT_ORDER = 3;
034        private int sortOrder;
035    
036        /**
037         * This is the description that prints for the summary line.
038         */
039        private String description;
040    
041        /**
042         * This is the count that displays. FIXME: Make this documentation a bit more clear.
043         */
044        private long count;
045    
046        /**
047         * 
048         */
049        public Summary() {
050            super();
051        }
052    
053        
054        /**
055         * Constructs a Summary.java.
056         * @param sortOrder
057         * @param description
058         * @param count
059         */
060        public Summary(int sortOrder, String description, long count) {
061            this.sortOrder = sortOrder;
062            this.description = description;
063            this.count = count;
064        }
065    
066        /**
067         * Constructs a Summary.java.
068         * @param sortOrder
069         * @param description
070         * @param count
071         */
072        public Summary(int sortOrder, String description, Integer count) {
073            this.sortOrder = sortOrder;
074            this.description = description;
075            if (count == null) {
076                this.count = 0;
077            }
078            else {
079                this.count = count.longValue();
080            }
081        }
082    
083        /**
084         * Compare this Summary object with another summary object
085         * 
086         * (non-Javadoc)
087         * 
088         * @see java.lang.Comparable#compareTo(java.lang.Object)
089         */
090        public int compareTo(Object arg0) {
091            if (arg0 instanceof Summary) {
092                Summary otherObject = (Summary) arg0;
093                Integer otherSort = new Integer(otherObject.getSortOrder());
094                Integer thisSort = new Integer(sortOrder);
095                return thisSort.compareTo(otherSort);
096            }
097            else {
098                return 0;
099            }
100        }
101    
102        /**
103         * Returns true if the description of this summary object and the passed in summary object are the same
104         * 
105         * @see java.lang.Object#equals(java.lang.Object)
106         */
107        @Override
108        public boolean equals(Object object) {
109            if (this == object)
110                return true;
111            if (!(object instanceof Summary))
112                return false;
113    
114            Summary that = (Summary) object;
115            return this.description.equals(that.getDescription());
116        }
117    
118        /**
119         * Build a report summary list for labor general ledger posting
120         * 
121         * @param destination description of summary displayed
122         * @param startingOrder order how information is displayed
123         * @return a list of summary objects
124         */
125        public static List<Summary> buildDefualtReportSummary(String destination, int startingOrder) {
126            List<Summary> reportSummary = new ArrayList<Summary>();
127            updateReportSummary(reportSummary, destination, KFSConstants.OperationType.INSERT, 0, startingOrder++);
128            updateReportSummary(reportSummary, destination, KFSConstants.OperationType.UPDATE, 0, startingOrder++);
129            updateReportSummary(reportSummary, destination, KFSConstants.OperationType.DELETE, 0, startingOrder++);
130            return reportSummary;
131        }
132    
133        /**
134         * Update the report summary with the given information
135         * 
136         * @param reportSummary list of summaries
137         * @param destinationName description of summary displayed
138         * @param operationType description of what action is related to the summary (i.e. insert, updated, deleted)
139         * @param count count of how many "objects" are affected
140         * @param order order how information is displayed
141         */
142        public static void updateReportSummary(List<Summary> reportSummary, String destinationName, String operationType, int count, int order) {
143            StringBuilder summaryDescription = buildSummaryDescription(destinationName, operationType);
144            updateReportSummary(reportSummary, summaryDescription.toString(), count, order);
145        }
146    
147        /**
148         * Update the report summary with the given information
149         * 
150         * @param reportSummary list of summaries
151         * @param summaryDescription description of summary displayed
152         * @param count count of how many "objects" are affected
153         * @param order order how information is displayed
154         */
155        public static void updateReportSummary(List<Summary> reportSummary, String summaryDescription, int count, int order) {
156            Summary inputSummary = new Summary(order, summaryDescription, count);
157    
158            int index = reportSummary.indexOf(inputSummary);
159            if (index >= 0) {
160                Summary summary = reportSummary.get(index);
161                summary.setCount(summary.getCount() + count);
162            }
163            else {
164                reportSummary.add(inputSummary);
165            }
166        }
167    
168        /**
169         * Build the description of summary with the given information
170         * 
171         * @param destinationName description of summary displayed
172         * @param operationType description of what action is related to the summary (i.e. insert, updated, deleted)
173         * @return
174         */
175        public static StringBuilder buildSummaryDescription(String destinationName, String operationType) {
176            StringBuilder summaryDescription = new StringBuilder();
177            summaryDescription.append("Number of ").append(destinationName).append(" records ").append(operationType).append(":");
178            return summaryDescription;
179        }
180    
181    
182        public long getCount() {
183            return count;
184        }
185    
186        public void setCount(long count) {
187            this.count = count;
188        }
189    
190        public String getDescription() {
191            return description;
192        }
193    
194        public void setDescription(String description) {
195            this.description = description;
196        }
197    
198        public int getSortOrder() {
199            return sortOrder;
200        }
201    
202        public void setSortOrder(int sortOrder) {
203            this.sortOrder = sortOrder;
204        }
205    }