001package org.kuali.ole.sip2.sip2Response;
002
003import org.apache.commons.lang3.StringUtils;
004import org.kuali.ole.bo.OLERenewItem;
005import org.kuali.ole.common.OLESIP2Util;
006import org.kuali.ole.bo.OLERenewItemList;
007import org.kuali.ole.response.OLESIP2Response;
008import org.kuali.ole.sip2.constants.OLESIP2Constants;
009import org.kuali.ole.sip2.requestParser.OLESIP2RenewAllRequestParser;
010import org.kuali.ole.sip2.sip2Server.MessageUtil;
011
012import java.util.ArrayList;
013import java.util.List;
014
015/**
016 * Created by gayathria on 10/11/14.
017 */
018public class OLESIP2RenewAllResponse extends OLESIP2Response {
019
020    public OLESIP2RenewAllResponse() {
021        this.code = "";
022    }
023
024    public String getSIP2RenewAllResponse(OLERenewItemList oleRenewItemList, OLESIP2RenewAllRequestParser sip2RenewAllRequestParser) {
025        StringBuilder renewAllResponseBuilder = new StringBuilder();
026        renewAllResponseBuilder.append(code);
027        if (oleRenewItemList.getRenewItemList().size() > 0) {
028            renewAllResponseBuilder.append(OLESIP2Util.bool2Int(true));
029        } else {
030            renewAllResponseBuilder.append(OLESIP2Util.bool2Int(false));
031        }
032        int renewedCount = 0;
033        int unRenewedCount = 0;
034        List<String> renewedItems = new ArrayList<>();
035        /**
036         * List of unrenewd items.
037         */
038        List<String> unRenewedItems = new ArrayList<>();
039
040        for (int i = 0; i < oleRenewItemList.getRenewItemList().size(); i++) {
041            OLERenewItem oleRenewItem = oleRenewItemList.getRenewItemList().get(i);
042            if (oleRenewItem.getMessage().equalsIgnoreCase(OLESIP2Constants.CHECK_OUT_SUCCESS)) {
043                renewedCount = renewedCount + 1;
044                renewedItems.add(oleRenewItem.getItemBarcode());
045
046            } else {
047                unRenewedCount = unRenewedCount + 1;
048                if(StringUtils.isNotBlank(oleRenewItem.getItemBarcode()))
049                    unRenewedItems.add(oleRenewItem.getItemBarcode());
050            }
051        }
052        renewAllResponseBuilder.append(OLESIP2Util.intToFixedLengthString(renewedCount, 4));
053        renewAllResponseBuilder.append(OLESIP2Util.intToFixedLengthString(unRenewedCount, 4));
054        renewAllResponseBuilder.append(MessageUtil.getSipDateTime());
055        renewAllResponseBuilder.append(OLESIP2Constants.INSTITUTION_ID_CODE);
056        renewAllResponseBuilder.append(StringUtils.isNotBlank(sip2RenewAllRequestParser.getInstitutionId()) ? sip2RenewAllRequestParser.getInstitutionId() : "");
057        for (String item : renewedItems) {
058            renewAllResponseBuilder.append(OLESIP2Constants.SPLIT+
059                    OLESIP2Constants.RENEWED_ITEMS_CODE);
060            renewAllResponseBuilder.append(item);
061        }
062        for (String item : unRenewedItems) {
063            renewAllResponseBuilder.append(OLESIP2Constants.SPLIT+
064                    OLESIP2Constants.UN_RENEWED_ITEMS_CODE);
065            renewAllResponseBuilder.append(item);
066        }
067        renewAllResponseBuilder.append(OLESIP2Constants.SPLIT+
068                OLESIP2Constants.SCREEN_MSG_CODE);
069        renewAllResponseBuilder.append(OLESIP2Constants.REQUEST_PROCESSED);
070        renewAllResponseBuilder.append(OLESIP2Constants.SPLIT+
071                OLESIP2Constants.PRINT_LINE_CODE);
072        renewAllResponseBuilder.append(" " +renewedCount + " - Renewed succesfully. " + unRenewedCount + " - Not Renewed. ");
073        if (StringUtils.isNotBlank(sip2RenewAllRequestParser.getSequenceNum())) {
074            renewAllResponseBuilder.append(OLESIP2Constants.SPLIT+
075                    OLESIP2Constants.SEQUENCE_NUM_CODE);
076            renewAllResponseBuilder.append(sip2RenewAllRequestParser.getSequenceNum());
077            renewAllResponseBuilder.append(MessageUtil.computeChecksum(sip2RenewAllRequestParser.toString()));
078        }
079        return renewAllResponseBuilder.toString() + '\r';
080    }
081
082    public String getSIP2RenewAllResponse(String message, OLESIP2RenewAllRequestParser sip2RenewAllRequestParser) {
083
084        StringBuilder renewAllResponseBuilder = new StringBuilder();
085        renewAllResponseBuilder.append(code);
086        renewAllResponseBuilder.append(OLESIP2Util.bool2Int(false));
087        renewAllResponseBuilder.append(OLESIP2Util.intToFixedLengthString(0, 4));
088        renewAllResponseBuilder.append(OLESIP2Util.intToFixedLengthString(0, 4));
089        renewAllResponseBuilder.append(MessageUtil.getSipDateTime());
090        renewAllResponseBuilder.append(OLESIP2Constants.INSTITUTION_ID_CODE);
091        renewAllResponseBuilder.append(StringUtils.isNotBlank(sip2RenewAllRequestParser.getInstitutionId()) ? sip2RenewAllRequestParser.getInstitutionId() : "");
092        renewAllResponseBuilder.append(OLESIP2Constants.SPLIT+
093                OLESIP2Constants.SCREEN_MSG_CODE);
094        renewAllResponseBuilder.append(message);
095        if (StringUtils.isNotBlank(sip2RenewAllRequestParser.getSequenceNum())) {
096            renewAllResponseBuilder.append(OLESIP2Constants.SPLIT+
097                    OLESIP2Constants.SEQUENCE_NUM_CODE);
098            renewAllResponseBuilder.append(sip2RenewAllRequestParser.getSequenceNum());
099            renewAllResponseBuilder.append(MessageUtil.computeChecksum(sip2RenewAllRequestParser.toString()));
100        }
101        return renewAllResponseBuilder.toString() + '\r';
102
103    }
104}