Package org.infotechservice.smartcard.smartcard

Source Code of org.infotechservice.smartcard.smartcard.NewSocialCardService

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.infotechservice.smartcard.smartcard;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import javax.smartcardio.CardException;
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;
import org.infotechservice.smartcard.service.CardService;
import org.infotechservice.smartcard.service.SmartCard;
import org.infotechservice.smartcard.utils.BCD;
import org.infotechservice.smartcard.utils.Converter;
import org.infotechservice.smartcard.utils.Messages;

/**
* Сервисный объект
* Должен использоваться для работы со старой социальной картой
* @author Finder
*/
public class NewSocialCardService extends CardService {

    private static final CommandAPDU SELECT_SOCIAL_APPLICATION = new CommandAPDU(
            0x000xA40x040x00
            new byte[] {
                (byte)0xA0, 0x00, 0x00, 0x00, 0x04, (byte)0x99, (byte)0x99, (byte)0xD6, 0x43, 0x07
            }
    );
    private static final CommandAPDU READ_FILE_1_7 = new CommandAPDU(0x00, 0xB2, 0x07, getFile(1), 256);
    private static final CommandAPDU READ_FILE_2_1 = new CommandAPDU(0x00, 0xB2, 0x01, getFile(2), 256);

    private byte[] record1_7;
    private byte[] record2_1;


    public NewSocialCardService(SmartCard card) throws CardException {
        super(card);
        exec(SELECT_SOCIAL_APPLICATION, "Unable select social application");
        record1_7 = getRecord(READ_FILE_1_7);
    }

    private byte[] getRecord(CommandAPDU apdu) throws CardException {
        ResponseAPDU responce = exec(apdu, "Unable read user info");
        Messages msg = Messages.findMessage(responce);
        if (!msg.isOk()) {
            throw new CardException(msg.getMessage());
        }
        return responce.getData();
    }

    public String getFIO() {
        byte[] result = readTag(new byte[] {(byte)0xDF, 0x10}, record1_7, 0);
        try {
            return new String(result, "windows-1251");
        } catch (UnsupportedEncodingException ex) {
        }
        return "";
    }

    public String getSex() {
        byte[] result = readTag(new byte[] {(byte)0x5F, 0x35}, record1_7, 0);
        try {
            return new String(result, "windows-1251");
        } catch (UnsupportedEncodingException ex) {
        }
        return "";
    }

    public String getAddress() {
        byte[] result = readTag(new byte[] {(byte)0x5F, 0x42}, record1_7, 0);
        try {
            return new String(result, "windows-1251");
        } catch (UnsupportedEncodingException ex) {
        }
        return "";
    }

    public Date getBirthDay() {
        byte[] result = readTag(new byte[] {(byte)0x5F, 0x2B}, record1_7, 0);
        return bcdToDate(result, 0);
    }

    public String getSerial() throws CardException {
        if(record2_1 == null) {
            record2_1 = getRecord(READ_FILE_2_1);
        }
        byte[] result = readTag(new byte[] {(byte)0xDF, 0x11}, record2_1, 0);
        String serial = Converter.hexDump(result).replaceAll("F+$", "");
        if(serial.length() != 25) {
            return serial;
        }
        serial = format(serial, new SF[] {
            new SF(6, " "), new SF(2, " "), new SF(10, " "), new SF(1, "  "),new SF(2, "/"),new SF(2, " "),
        });
        return serial;
    }

    public String getSnils() throws CardException {
        if(record2_1 == null) {
            record2_1 = getRecord(READ_FILE_2_1);
        }
        byte[] result = readTag(new byte[] {(byte)0xDF, 0x50}, record2_1, 0);
        String snils = Converter.hexDump(result).replaceAll("F+$", "");
        if(snils.length() != 11) {
            return snils;
        }
        snils = format(snils, new SF[] {
            new SF(3, "-"), new SF(3, "-"), new SF(3, " "),
        });
        return snils;
    }

    public String getInn() throws CardException {
        if(record2_1 == null) {
            record2_1 = getRecord(READ_FILE_2_1);
        }
        byte[] result = readTag(new byte[] {(byte)0xDF, 0x51}, record2_1, 0);
        return Converter.hexDump(result).replaceAll("F+$", "");
    }

    public class Excemption {
        public Excemption(int type, Date begin, Date end) {
            this.type = type;
            this.begin = begin;
            this.end = end;
        }
        public final int type;
        public final Date begin;
        public final Date end;
    }

    /**
     * Возвращает информацию о льготе
     * @param index 0-11
     * @return NULL if not exists
     * @throws CardException
     */
    public Excemption getExcemption(int index) throws CardException {
        if(index < 0 || index > 12) {
            return null;
        }
        if(record2_1 == null) {
            record2_1 = getRecord(READ_FILE_2_1);
        }
        byte[] result = readTag(new byte[] {(byte)0xDF, (byte)(0x12 + index)}, record2_1, 0);
        if(result.length == 0) {
            return null;
        }
        Integer type = new BCD(result).toInteger(0, 4);
        Date begin = bcdToDate(result, 4);
        Date end = bcdToDate(result, 8);
        return new Excemption(type, begin, end);
    }
}
TOP

Related Classes of org.infotechservice.smartcard.smartcard.NewSocialCardService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.