From 0298d916830b7b1dc5ff59c8370311d7fcaa1de1 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Fri, 3 Jan 2020 10:49:00 +0100 Subject: utils and decoder / encoder --- .../uic/ticket/api/utils/Api2OpenAsnEncoder.java | 2256 ++++++++++++++++++++ .../uic/ticket/api/utils/OpenAsn2ApiDecoder.java | 2188 +++++++++++++++++++ src/org/uic/ticket/api/utils/UicEncoderUtils.java | 517 +++++ src/org/uic/ticket/api/utils/package.html | 7 + 4 files changed, 4968 insertions(+) create mode 100644 src/org/uic/ticket/api/utils/Api2OpenAsnEncoder.java create mode 100644 src/org/uic/ticket/api/utils/OpenAsn2ApiDecoder.java create mode 100644 src/org/uic/ticket/api/utils/UicEncoderUtils.java create mode 100644 src/org/uic/ticket/api/utils/package.html diff --git a/src/org/uic/ticket/api/utils/Api2OpenAsnEncoder.java b/src/org/uic/ticket/api/utils/Api2OpenAsnEncoder.java new file mode 100644 index 0000000..cb274c1 --- /dev/null +++ b/src/org/uic/ticket/api/utils/Api2OpenAsnEncoder.java @@ -0,0 +1,2256 @@ +/* + * + */ +package org.uic.ticket.api.utils; + +import java.util.Collection; +import java.util.Date; +import net.gcdc.asn1.datatypes.Asn1BigInteger; +import net.gcdc.asn1.datatypesimpl.SequenceOfStringIA5; +import net.gcdc.asn1.datatypesimpl.SequenceOfStringUTF8; +import net.gcdc.asn1.datatypesimpl.SequenceOfUnrestrictedLong; + +import org.uic.ticket.api.EncodingFormatException; +import org.uic.ticket.api.asn.omv1.BerthDetailData; +import org.uic.ticket.api.asn.omv1.CarCarriageReservationData; +import org.uic.ticket.api.asn.omv1.CardReferenceType; +import org.uic.ticket.api.asn.omv1.CodeTableType; +import org.uic.ticket.api.asn.omv1.CompartmentDetailsType; +import org.uic.ticket.api.asn.omv1.CompartmentGenderType; +import org.uic.ticket.api.asn.omv1.ConfirmationTypeType; +import org.uic.ticket.api.asn.omv1.ControlData; +import org.uic.ticket.api.asn.omv1.CountermarkData; +import org.uic.ticket.api.asn.omv1.CustomerCardData; +import org.uic.ticket.api.asn.omv1.CustomerStatusType; +import org.uic.ticket.api.asn.omv1.DelayConfirmation; +import org.uic.ticket.api.asn.omv1.DeltaCoordinates; +import org.uic.ticket.api.asn.omv1.DocumentData; +import org.uic.ticket.api.asn.omv1.ExtensionData; +import org.uic.ticket.api.asn.omv1.FIPTicketData; +import org.uic.ticket.api.asn.omv1.GenderType; +import org.uic.ticket.api.asn.omv1.GeoCoordinateSystemType; +import org.uic.ticket.api.asn.omv1.GeoCoordinateType; +import org.uic.ticket.api.asn.omv1.GeoUnitType; +import org.uic.ticket.api.asn.omv1.HemisphereLatitudeType; +import org.uic.ticket.api.asn.omv1.HemisphereLongitudeType; +import org.uic.ticket.api.asn.omv1.IncludedOpenTicketType; +import org.uic.ticket.api.asn.omv1.IssuingData; +import org.uic.ticket.api.asn.omv1.LineType; +import org.uic.ticket.api.asn.omv1.LinkMode; +import org.uic.ticket.api.asn.omv1.LoadingDeckType; +import org.uic.ticket.api.asn.omv1.LuggageRestrictionType; +import org.uic.ticket.api.asn.omv1.OpenTicketData; +import org.uic.ticket.api.asn.omv1.ParkingGroundData; +import org.uic.ticket.api.asn.omv1.PassData; +import org.uic.ticket.api.asn.omv1.PassengerType; +import org.uic.ticket.api.asn.omv1.PlacesType; +import org.uic.ticket.api.asn.omv1.PolygoneType; +import org.uic.ticket.api.asn.omv1.PriceTypeType; +import org.uic.ticket.api.asn.omv1.RegionalValidityType; +import org.uic.ticket.api.asn.omv1.RegisteredLuggageType; +import org.uic.ticket.api.asn.omv1.ReservationData; +import org.uic.ticket.api.asn.omv1.ReturnRouteDescriptionType; +import org.uic.ticket.api.asn.omv1.RoofRackType; +import org.uic.ticket.api.asn.omv1.RouteSectionType; +import org.uic.ticket.api.asn.omv1.SequenceOfBerthDetailData; +import org.uic.ticket.api.asn.omv1.SequenceOfCardReferenceType; +import org.uic.ticket.api.asn.omv1.SequenceOfCarrierNum; +import org.uic.ticket.api.asn.omv1.SequenceOfCountries; +import org.uic.ticket.api.asn.omv1.SequenceOfCustomerStatusType; +import org.uic.ticket.api.asn.omv1.SequenceOfDeltaCoordinates; +import org.uic.ticket.api.asn.omv1.SequenceOfDocumentData; +import org.uic.ticket.api.asn.omv1.SequenceOfExtensionData; +import org.uic.ticket.api.asn.omv1.SequenceOfIncludedOpenTicketType; +import org.uic.ticket.api.asn.omv1.SequenceOfPlaceNum; +import org.uic.ticket.api.asn.omv1.SequenceOfRegionalValidityType; +import org.uic.ticket.api.asn.omv1.SequenceOfRegisteredLuggageType; +import org.uic.ticket.api.asn.omv1.SequenceOfServiceBrands; +import org.uic.ticket.api.asn.omv1.SequenceOfTariffType; +import org.uic.ticket.api.asn.omv1.SequenceOfTicketLinkType; +import org.uic.ticket.api.asn.omv1.SequenceOfTravelerId; +import org.uic.ticket.api.asn.omv1.SequenceOfTravelerType; +import org.uic.ticket.api.asn.omv1.SequenceOfViaStationType; +import org.uic.ticket.api.asn.omv1.SeriesDetailType; +import org.uic.ticket.api.asn.omv1.ServiceType; +import org.uic.ticket.api.asn.omv1.StationPassageData; +import org.uic.ticket.api.asn.omv1.TariffType; +import org.uic.ticket.api.asn.omv1.TicketDetailData; +import org.uic.ticket.api.asn.omv1.TicketLinkType; +import org.uic.ticket.api.asn.omv1.TicketType; +import org.uic.ticket.api.asn.omv1.TimeRangeType; +import org.uic.ticket.api.asn.omv1.TokenType; +import org.uic.ticket.api.asn.omv1.TrainLinkType; +import org.uic.ticket.api.asn.omv1.TravelClassType; +import org.uic.ticket.api.asn.omv1.TravelerData; +import org.uic.ticket.api.asn.omv1.TravelerType; +import org.uic.ticket.api.asn.omv1.UicRailTicketData; +import org.uic.ticket.api.asn.omv1.ValidityPeriodDetailType; +import org.uic.ticket.api.asn.omv1.ValidityPeriodType; +import org.uic.ticket.api.asn.omv1.VatDetailType; +import org.uic.ticket.api.asn.omv1.ViaStationType; +import org.uic.ticket.api.asn.omv1.VoucherData; +import org.uic.ticket.api.asn.omv1.ZoneType; +import org.uic.ticket.api.spec.IBerth; +import org.uic.ticket.api.spec.ICarCarriageReservation; +import org.uic.ticket.api.spec.ICardReference; +import org.uic.ticket.api.spec.ICompartmentDetails; +import org.uic.ticket.api.spec.IControlDetail; +import org.uic.ticket.api.spec.ICounterMark; +import org.uic.ticket.api.spec.ICustomerCard; +import org.uic.ticket.api.spec.ICustomerStatusDescription; +import org.uic.ticket.api.spec.IDelayConfirmation; +import org.uic.ticket.api.spec.IDocumentData; +import org.uic.ticket.api.spec.IExtension; +import org.uic.ticket.api.spec.IFipTicket; +import org.uic.ticket.api.spec.IGeoCoordinate; +import org.uic.ticket.api.spec.IIncludedOpenTicket; +import org.uic.ticket.api.spec.IIssuingDetail; +import org.uic.ticket.api.spec.ILine; +import org.uic.ticket.api.spec.ILuggageRestriction; +import org.uic.ticket.api.spec.IOpenTicket; +import org.uic.ticket.api.spec.IParkingGround; +import org.uic.ticket.api.spec.IPass; +import org.uic.ticket.api.spec.IPlaces; +import org.uic.ticket.api.spec.IPolygone; +import org.uic.ticket.api.spec.IRegionalValidity; +import org.uic.ticket.api.spec.IRegisteredLuggage; +import org.uic.ticket.api.spec.IReservation; +import org.uic.ticket.api.spec.IReturnRouteDescription; +import org.uic.ticket.api.spec.IRouteSection; +import org.uic.ticket.api.spec.ISeriesDataDetails; +import org.uic.ticket.api.spec.IStationPassage; +import org.uic.ticket.api.spec.ITariff; +import org.uic.ticket.api.spec.ITicketLink; +import org.uic.ticket.api.spec.ITimeRange; +import org.uic.ticket.api.spec.IToken; +import org.uic.ticket.api.spec.ITrainLink; +import org.uic.ticket.api.spec.ITraveler; +import org.uic.ticket.api.spec.ITravelerDetail; +import org.uic.ticket.api.spec.IUicRailTicket; +import org.uic.ticket.api.spec.IValidityDetails; +import org.uic.ticket.api.spec.IValidityRange; +import org.uic.ticket.api.spec.IVatDetail; +import org.uic.ticket.api.spec.IViaStation; +import org.uic.ticket.api.spec.IVoucher; +import org.uic.ticket.api.spec.IZone; + +/** + * The Class Api2OpenAsnEncoder. + */ +public class Api2OpenAsnEncoder { + + + /** + * Populate asn1 model. + * + * @param uicTicket the uic ticket + * @return the org.uic.ticket.asn.om. uic rail ticket data + * @throws EncodingFormatException the encoding format exception + */ + public UicRailTicketData populateToAsn1Model(IUicRailTicket uicTicket) throws EncodingFormatException { + + UicRailTicketData asnTicket = new UicRailTicketData(); + + if (uicTicket.getIssuerDetails() != null) { + asnTicket.setIssuingDetail(encodeIssuingDetails(uicTicket.getIssuerDetails())); + } + + + if (uicTicket.getControlDetails() != null) { + asnTicket.setControlDetail(encodeControlDetails(uicTicket.getControlDetails(), uicTicket.getIssuerDetails().getIssuingDate())); + } + + + if (uicTicket.getExtensions() != null && !uicTicket.getExtensions().isEmpty()){ + + SequenceOfExtensionData asnList = new SequenceOfExtensionData(); + for (IExtension extension : uicTicket.getExtensions()){ + ExtensionData asnExtension = encodeExtension(extension); + if (asnExtension!= null) { + asnList.add(asnExtension); + } + } + if (!asnList.isEmpty()){ + asnTicket.setExtension(asnList); + } + + + } + + + if (uicTicket.getTravelerDetails() != null) { + asnTicket.setTravelerDetail(encodeTravelers(uicTicket.getTravelerDetails(),uicTicket.getIssuerDetails().getIssuingDate() )); + } + + if (uicTicket.getDocumentData() != null && !uicTicket.getDocumentData().isEmpty()) { + + SequenceOfDocumentData documents = new SequenceOfDocumentData(); + + for (IDocumentData document : uicTicket.getDocumentData()){ + + DocumentData asnDocument = null; + + if (document instanceof IReservation) { + asnDocument = convertReservation((IReservation) document , uicTicket.getIssuerDetails().getIssuingDate()); + } + + if (document instanceof IOpenTicket) { + asnDocument = convertOpenTicket((IOpenTicket) document , uicTicket.getIssuerDetails().getIssuingDate()); + } + + if (document instanceof ICarCarriageReservation) { + asnDocument = convertCarCarriageReservation((ICarCarriageReservation) document , uicTicket.getIssuerDetails().getIssuingDate()); + } + + if (document instanceof IFipTicket) { + asnDocument = convertFipTicket((IFipTicket) document , uicTicket.getIssuerDetails().getIssuingDate()); + } + + if (document instanceof IPass) { + asnDocument = convertPass((IPass) document , uicTicket.getIssuerDetails().getIssuingDate()); + } + + if (document instanceof IVoucher) { + asnDocument = convertVoucher((IVoucher) document , uicTicket.getIssuerDetails().getIssuingDate()); + } + + if (document instanceof ICounterMark) { + asnDocument = convertCounterMark((ICounterMark) document , uicTicket.getIssuerDetails().getIssuingDate()); + } + + if (document instanceof IParkingGround) { + asnDocument = convertParkingGround((IParkingGround) document , uicTicket.getIssuerDetails().getIssuingDate()); + } + + if (document instanceof IStationPassage) { + asnDocument = convertStationPassage((IStationPassage) document , uicTicket.getIssuerDetails().getIssuingDate()); + } + + if (document instanceof ICustomerCard) { + asnDocument = encodeCustomerCard((ICustomerCard) document , uicTicket.getIssuerDetails().getIssuingDate()); + } + + + if (document instanceof IDelayConfirmation) { + asnDocument = encodeDelayConfirmation((IDelayConfirmation) document , uicTicket.getIssuerDetails().getIssuingDate()); + } + + + if (asnDocument!= null && document.getToken()!= null) { + asnDocument.setToken(encodeToken(document.getToken())); + } + + if (asnDocument != null) { + documents.add(asnDocument); + } + } + asnTicket.setTransportDocument(documents); + } + + + return asnTicket; + } + + private DocumentData encodeDelayConfirmation(IDelayConfirmation document, Date issuingDate) throws EncodingFormatException { + + DocumentData asnDocument = new DocumentData(); + TicketDetailData asnTicket = new TicketDetailData(); + asnDocument.setTicket(asnTicket); + + DelayConfirmation asnData = new DelayConfirmation(); + asnDocument.getTicket().setDelayConfirmation(asnData); + + asnData.setTrainIA5(UicEncoderUtils.getIA5NonNum(document.getTrain())); + asnData.setTrainNum(Asn1BigInteger.toAsn1(UicEncoderUtils.getNum(document.getTrain()))); + + asnData.setReferenceNum(UicEncoderUtils.getNum(document.getReference())); + asnData.setReferenceIA5(UicEncoderUtils.getIA5NonNum(document.getReference())); + + if (document.getStationCodeTable() != CodeTableType.stationUICReservation){ + asnData.setStationCodeTable(document.getStationCodeTable()); + } + asnData.setStationIA5(UicEncoderUtils.getIA5NonNum(document.getStation())); + asnData.setStationNum(UicEncoderUtils.getNum(document.getStation())); + + + asnData.setPlannedArrivalDate(document.getArrivalDate()); + asnData.setDepartureUTCOffset(document.getArrivalUTCoffset()); + + asnData.setAffectedTickets(encodeTicketLickList(document.getLinkedTickets())); + + asnData.setConfirmationType(ConfirmationTypeType.values()[document.getConfirmationType()]); + + asnData.setDelay(new Long(document.getDelay())); + + asnData.setTrainCancelled(document.isTrainCancelled()); + + + asnData.setInfoText(document.getInfoText()); + + asnData.setStationNameUTF8(document.getStationName()); + + asnData.setExtension(encodeExtension(document.getExtension())); + + return asnDocument; + } + + + /** + * Convert car carriage reservation. + * + * @param document the document + * @param issuingDate the issuing date + * @return the document data + * @throws EncodingFormatException the encoding format exception + */ + private DocumentData convertCarCarriageReservation( ICarCarriageReservation document, Date issuingDate) throws EncodingFormatException { + + DocumentData asnDocument = new DocumentData(); + TicketDetailData asnTicket = new TicketDetailData(); + asnDocument.setTicket(asnTicket); + + CarCarriageReservationData asnData = new CarCarriageReservationData(); + asnDocument.getTicket().setCarCarriageReservation(asnData); + + asnData.setTrainIA5(UicEncoderUtils.getIA5NonNum(document.getTrain())); + asnData.setTrainNum(UicEncoderUtils.getNum(document.getTrain())); + + asnData.setProductOwnerNum(UicEncoderUtils.getNum(document.getProductOwner())); + asnData.setProductOwnerIA5(UicEncoderUtils.getIA5NonNum(document.getProductOwner())); + + asnData.setReferenceNum(UicEncoderUtils.getNum(document.getReference())); + asnData.setReferenceIA5(UicEncoderUtils.getIA5NonNum(document.getReference())); + + if (document.getStationCodeTable() != CodeTableType.stationUICReservation){ + asnData.setStationCodeTable(document.getStationCodeTable()); + } + + asnData.setCarrierNum(SequenceOfCarrierNum.getSequence(UicEncoderUtils.getNumList(document.getCarriers()))); + asnData.setCarrierIA5(UicEncoderUtils.getIA5NonNumList(document.getCarriers())); + + asnData.setExtension(encodeExtension(document.getExtension())); + + asnData.setCompartmentDetails(encodeCompartmentDetails(document.getCompartmentDetails())); + + + if (document.getPriceType()!= PriceTypeType.travelPrice){ + asnData.setPriceType(document.getPriceType()); + } + + + if (document.getServiceBrand()!= null) { + asnData.setServiceBrand(UicEncoderUtils.getRestrictedInt(document.getServiceBrand().getServiceBrand(),1,9999)); + asnData.setServiceBrandAbrUTF8(document.getServiceBrand().getServiceBrandAbbreviation()); + asnData.setServiceBrandNameUTF8(document.getServiceBrand().getServiceBrandDescription()); + + } + if (document.getStationCodeTable() != CodeTableType.stationUICReservation){ + asnData.setStationCodeTable(document.getStationCodeTable()); + } + + asnData.setFromStationIA5(UicEncoderUtils.getIA5NonNum(document.getFromStation())); + asnData.setFromStationNum(UicEncoderUtils.getNum(document.getFromStation())); + + asnData.setToStationIA5(UicEncoderUtils.getIA5NonNum(document.getToStation())); + asnData.setToStationNum(UicEncoderUtils.getNum(document.getToStation())); + + asnData.setFromStationNameUTF8(document.getFromStationName()); + asnData.setToStationNameUTF8(document.getToStationName()); + + + asnData.setAttachedBicycles(UicEncoderUtils.getRestrictedInt(document.getAttachedBicycles(),1,5)); + asnData.setAttachedBoats(UicEncoderUtils.getRestrictedInt(document.getAttachedBoats(),1,2)); + asnData.setAttachedSurfboards(UicEncoderUtils.getRestrictedInt(document.getAttachedSurfboards(),1,5)); + + if (document.getLoadingDeck() != LoadingDeckType.upper){ + asnData.setLoadingDeck(document.getLoadingDeck()); + } + + asnData.setLoadingListEntry(UicEncoderUtils.getRestrictedInt(document.getAttachedSurfboards(),1,200)); + + asnData.setBoatCategory(UicEncoderUtils.getRestrictedInt(document.getBoatCategory(),1,6)); + asnData.setCarCategory(UicEncoderUtils.getRestrictedInt(document.getCarCategory(),1,9)); + asnData.setCoach(UicEncoderUtils.getIA5(document.getCoach())); + + asnData.setRoofRackHeight(UicEncoderUtils.getRestrictedInt(document.getRoofRackHeight(),1,99)); + + if (document.getRoofRackType()!= RoofRackType.norack) { + asnData.setRoofRackType(document.getRoofRackType()); + } + + if (document.getTariff() != null) { + asnData.setTariff(encodeTariff(document.getTariff())); + } + asnData.setNumberPlate(UicEncoderUtils.getIA5(document.getNumberPlate())); + asnData.setPlace(UicEncoderUtils.getIA5(document.getPlace())); + + if (document.isTextileRoof() ) { + asnData.setTextileRoof(true); + } else { + asnData.setTextileRoof(false); + } + + asnData.setTrailerPlate(UicEncoderUtils.getIA5(document.getTrailerPlate())); + + asnData.setLoadingDates(document.getBeginLoading(),document.getEndLoading(), issuingDate); + + asnData.setLoadingUTCOffset(document.getLoadingTimeUTCoffset()); + + asnData.setPrice(document.getPrice()); + + if (document.getVatDetails() != null && !document.getVatDetails().isEmpty()){ + for (IVatDetail vat : document.getVatDetails()) { + asnData.addVatDetail(encodeVatDetail(vat)); + } + } + + + return asnDocument; + } + + + /** + * Encode places. + * + * @param places the places + * @return the places type + * @throws EncodingFormatException the encoding format exception + */ + private PlacesType encodePlaces(IPlaces places) throws EncodingFormatException { + + if (places == null) { + return null; + } + + PlacesType asnData = new PlacesType(); + + asnData.setCoach(UicEncoderUtils.getIA5(places.getCoach())); + asnData.setPlaceDescription(places.getPlaceDescription()); + asnData.setPlaceString(UicEncoderUtils.getIA5(places.getPlaceString())); + asnData.setPlaceNum(SequenceOfPlaceNum.getSequence(UicEncoderUtils.getNumList(places.getPlaces()))); + asnData.setPlaceIA5(UicEncoderUtils.getIA5NonNumList(places.getPlaces())); + + return asnData; + } + + + /** + * Encode compartment details. + * + * @param compartmentDetails the compartment details + * @return the compartment details type + * @throws EncodingFormatException the encoding format exception + */ + private CompartmentDetailsType encodeCompartmentDetails(ICompartmentDetails compartmentDetails) throws EncodingFormatException { + + if (compartmentDetails == null) { + return null; + } + CompartmentDetailsType asnData = new CompartmentDetailsType(); + + asnData.setCoachType(UicEncoderUtils.getRestrictedInt(compartmentDetails.getCoachType(), 1,99)); + asnData.setCoachTypeDescr(compartmentDetails.getCoachTypeDescr()); + asnData.setCompartmentType(UicEncoderUtils.getRestrictedInt(compartmentDetails.getCompartmentType(), 1,99)); + asnData.setCompartmentTypeDescr(compartmentDetails.getCompartmentTypeDescr()); + asnData.setSpecialAllocation(UicEncoderUtils.getRestrictedInt(compartmentDetails.getSpecialAllocation(), 1,99)); + asnData.setSpecialAllocationDescr(compartmentDetails.getSpecialAllocationDescr()); + + return asnData; + } + + /** + * Encode berths. + * + * @param berths the berths + * @return the list + * @throws EncodingFormatException the encoding format exception + */ + private SequenceOfBerthDetailData encodeBerths(Collection berths) throws EncodingFormatException { + + if (berths == null || berths.isEmpty()) { + return null; + } + SequenceOfBerthDetailData asnList = new SequenceOfBerthDetailData(); + + for ( IBerth berth : berths){ + BerthDetailData asnBerth = new BerthDetailData(); + + asnBerth.setBerthType(berth.getType()); + + if (berth.getGender()!= CompartmentGenderType.family) { + asnBerth.setGender(berth.getGender()); + } + + asnBerth.setNumberOfBerths(UicEncoderUtils.getRestrictedInt(berth.getNumberOfBerths(), 1 , 999 )); + + asnList.add(asnBerth); + } + + if (asnList.isEmpty()) { + return null; + } + return asnList; + } + + /** + * Convert reservation. + * + * @param document the document + * @param issuingDate the issuing date + * @return the document data + * @throws EncodingFormatException the encoding format exception + */ + private DocumentData convertReservation(IReservation document, Date issuingDate) throws EncodingFormatException { + + DocumentData asnDocument = new DocumentData(); + TicketDetailData asnTicket = new TicketDetailData(); + asnDocument.setTicket(asnTicket); + + ReservationData asnData = new ReservationData(); + asnDocument.getTicket().setReservation(asnData); + + asnData.setProductOwnerNum(UicEncoderUtils.getNum(document.getProductOwner())); + asnData.setProductOwnerIA5(UicEncoderUtils.getIA5NonNum(document.getProductOwner())); + + asnData.setReferenceNum(UicEncoderUtils.getNum(document.getReference())); + asnData.setReferenceIA5(UicEncoderUtils.getIA5NonNum(document.getReference())); + + asnData.setInfoText(document.getInfoText()); + asnData.setExtension(encodeExtension(document.getExtension())); + + asnData.setCarrierNum(SequenceOfCarrierNum.getSequence(UicEncoderUtils.getNumList(document.getCarriers()))); + asnData.setCarrierIA5(UicEncoderUtils.getIA5NonNumList(document.getCarriers())); + + if (document.getStationCodeTable() != CodeTableType.stationUICReservation){ + asnData.setStationCodeTable(document.getStationCodeTable()); + } + + asnData.setFromStationIA5(UicEncoderUtils.getIA5NonNum(document.getFromStation())); + asnData.setFromStationNum(UicEncoderUtils.getNum(document.getFromStation())); + + asnData.setToStationIA5(UicEncoderUtils.getIA5NonNum(document.getToStation())); + asnData.setToStationNum(UicEncoderUtils.getNum(document.getToStation())); + + asnData.setFromStationNameUTF8(document.getFromStationName()); + asnData.setToStationNameUTF8(document.getToStationName()); + + asnData.setTrainIA5(UicEncoderUtils.getIA5NonNum(document.getTrain())); + asnData.setTrainNum(UicEncoderUtils.getNum(document.getTrain())); + + asnData.setTariff(encodeTariffCollection(document.getTariffs())); + + asnData.setDepartureArrivalDates(document.getDepartureDate(),document.getArrivalDate(), issuingDate); + + if (document.getDepartureUTCoffset() != null) { + asnData.setDepartureUTCOffset(document.getDepartureUTCoffset()); + if (document.getArrivalUTCoffset() != null && document.getArrivalUTCoffset() != document.getDepartureUTCoffset()){ + asnData.setArrivalUTCOffset(document.getArrivalUTCoffset()); + } + } + + + + if (document.getClassCode() != TravelClassType.second){ + asnData.setClassCode(document.getClassCode()); + } + + asnData.setBerth(encodeBerths(document.getBerths())); + + asnData.setCompartmentDetails(encodeCompartmentDetails(document.getCompartmentDetails())); + + asnData.setLuggage(encodeLuggage(document.getLuggageRestriction())); + + asnData.setNumberOfOverbooked(UicEncoderUtils.getRestrictedInt(document.getNumberOfOverbooked(), 1, 200)); + asnData.setNumberOfSupplements(UicEncoderUtils.getRestrictedInt(document.getNumberOfSupplements(), 1, 200)); + + if (document.getPriceType()!=PriceTypeType.travelPrice){ + asnData.setPriceType(document.getPriceType()); + } + asnData.setTypeOfSupplement(UicEncoderUtils.getRestrictedInt(document.getTypeOfSupplement(), 1, 9)); + + asnData.setPlaces(encodePlaces(document.getPlaces())); + asnData.setBicyclePlaces(encodePlaces(document.getBicyclePlaces())); + + + if (document.getService() != ServiceType.seat) { + asnData.setService(document.getService()); + } + + if (document.getServiceBrand()!= null) { + asnData.setServiceBrand(UicEncoderUtils.getRestrictedInt(document.getServiceBrand().getServiceBrand(),1,9999)); + asnData.setServiceBrandAbrUTF8(document.getServiceBrand().getServiceBrandAbbreviation()); + asnData.setServiceBrandNameUTF8(document.getServiceBrand().getServiceBrandDescription()); + + } + + if (document.getServiceLevel() != null && document.getServiceLevel().length() > 2) { + throw new EncodingFormatException("Service level too long"); + } + asnData.setServiceLevel(UicEncoderUtils.getIA5(document.getServiceLevel())); + + asnData.setPrice(document.getPrice()); + + if (document.getVatDetails() != null && !document.getVatDetails().isEmpty()){ + for (IVatDetail vat : document.getVatDetails()) { + asnData.addVatDetail(encodeVatDetail(vat)); + } + } + + return asnDocument; + } + + /** + * Encode included addons. + * + * @param tickets the tickets + * @param issuingDate the issuing date + * @param classCode the class code + * @return the list + * @throws EncodingFormatException the encoding format exception + */ + private SequenceOfIncludedOpenTicketType encodeIncludedAddons(Collection tickets, Date issuingDate, TravelClassType classCode) throws EncodingFormatException { + + if (tickets == null || tickets.isEmpty()) { + return null; + } + SequenceOfIncludedOpenTicketType asnList = new SequenceOfIncludedOpenTicketType(); + + for ( IIncludedOpenTicket ticket : tickets){ + + IncludedOpenTicketType asnTicket = encodeIncludedOpenTicket(ticket,issuingDate,classCode); + if (asnTicket!=null) { + asnList.add(asnTicket); + } + } + + if (asnList.isEmpty()) { + return null; + } + return asnList; + } + + + /** + * Encode included open ticket. + * + * @param document the document + * @param issuingDate the issuing date + * @param classCode the class code + * @return the included open ticket type + * @throws EncodingFormatException the encoding format exception + */ + private IncludedOpenTicketType encodeIncludedOpenTicket(IIncludedOpenTicket document, Date issuingDate, TravelClassType classCode) throws EncodingFormatException { + + if (document == null) return null; + + IncludedOpenTicketType asnData = new IncludedOpenTicketType(); + + asnData.setProductOwnerNum(UicEncoderUtils.getNum(document.getProductOwner())); + asnData.setProductOwnerIA5(UicEncoderUtils.getIA5NonNum(document.getProductOwner())); + + asnData.setProductIdNum(UicEncoderUtils.getNum(document.getProductId())); + asnData.setProductIdIA5(UicEncoderUtils.getIA5NonNum(document.getProductId())); + + + asnData.setInfoText(document.getInfoText()); + asnData.setExtension(encodeExtension(document.getExtension())); + + if (document.getStationCodeTable() != CodeTableType.stationUIC){ + asnData.setStationCodeTable(document.getStationCodeTable()); + } + + asnData.setIncludedCarriersNum(SequenceOfCarrierNum.getSequence(UicEncoderUtils.getNumList(document.getIncludedCarriers()))); + asnData.setIncludedCarriersIA5(UicEncoderUtils.getIA5NonNumList(document.getIncludedCarriers())); + + asnData.setExcludedServiceBrands(SequenceOfServiceBrands.getSequence(UicEncoderUtils.encodeRestrictedIntegerCollection(document.getExcludedServiceBrands(),1,9999))); + asnData.setIncludedServiceBrands(SequenceOfServiceBrands.getSequence(UicEncoderUtils.encodeRestrictedIntegerCollection(document.getIncludedServiceBrands(),1,9999))); + + asnData.setValidityDates(document.getValidFrom(), document.getValidUntil(), issuingDate); + + if (document.getValidFromUTCoffset() != null) { + asnData.setValidFromUTCOffset(document.getValidFromUTCoffset()); + if (document.getValidUntilUTCoffset() != null && document.getValidUntilUTCoffset() != document.getValidFromUTCoffset()){ + asnData.setValidUntilUTCOffset(document.getValidUntilUTCoffset()); + } + } + + + if (document.getTariffs() != null && document.getTariffs().size() > 0){ + asnData.setTariffs(encodeTariffCollection(document.getTariffs())); + } + + if (document.getClassCode() != classCode){ + asnData.setClassCode(document.getClassCode()); + } + if (document.getExternalIssuer() > 0) { + asnData.setExternalIssuerId(new Long(document.getExternalIssuer())); + } + + if (document.getAuthorizationCode() > 0) { + asnData.setIssuerAutorizationId(new Long(document.getAuthorizationCode())); + } + + if (document.getValidRegionList()!= null && document.getValidRegionList().size() > 0) { + asnData.setValidRegion(encodeRegionCollection(document.getValidRegionList(), issuingDate)); + } + + return asnData; + } + + /** + * Encode return description. + * + * @param route the route + * @param issuingDate the issuing date + * @return the return route description type + * @throws EncodingFormatException the encoding format exception + */ + private ReturnRouteDescriptionType encodeReturnDescription(IReturnRouteDescription route, Date issuingDate) throws EncodingFormatException { + + if (route == null) return null; + + ReturnRouteDescriptionType asnData = new ReturnRouteDescriptionType(); + + asnData.setFromStationIA5(UicEncoderUtils.getIA5NonNum(route.getFromStation())); + asnData.setFromStationNum(UicEncoderUtils.getNum(route.getFromStation())); + + asnData.setToStationIA5(UicEncoderUtils.getIA5NonNum(route.getToStation())); + asnData.setToStationNum(UicEncoderUtils.getNum(route.getToStation())); + + asnData.setFromStationNameUTF8(route.getFromStationName()); + asnData.setToStationNameUTF8(route.getToStationName()); + + if (route.getValidRegionList()!= null && route.getValidRegionList().size() > 0) { + asnData.setValidReturnRegion(encodeRegionCollection(route.getValidRegionList(), issuingDate)); + } + + if (route.getValidRegionDesc() != null && route.getValidRegionDesc().length() > 0){ + asnData.setValidReturnRegionDesc(route.getValidRegionDesc()); + } + + return asnData; + } + + /** + * Encode luggage. + * + * @param luggageRestriction the luggage restriction + * @return the luggage restriction type + * @throws EncodingFormatException the encoding format exception + */ + private LuggageRestrictionType encodeLuggage(ILuggageRestriction luggageRestriction) throws EncodingFormatException { + + if (luggageRestriction== null) return null; + + LuggageRestrictionType asnData = new LuggageRestrictionType(); + + asnData.setMaxHandLuggagePieces(UicEncoderUtils.getRestrictedIntWithDefault(luggageRestriction.getMaxHandLuggagePieces(),1,99,3)); + + asnData.setMaxNonHandLuggagePieces(UicEncoderUtils.getRestrictedIntWithDefault(luggageRestriction.getMaxNonHandLuggagePieces(),1,99,1)); + + if (luggageRestriction.getRegisteredLuggage() != null && luggageRestriction.getRegisteredLuggage().size() > 0) { + asnData.setRegisteredLuggage(encodeRegisteredLuggage(luggageRestriction.getRegisteredLuggage())); + } + + return asnData; + } + + /** + * Encode registered luggage. + * + * @param registeredLuggage the registered luggage + * @return the list + * @throws EncodingFormatException the encoding format exception + */ + private SequenceOfRegisteredLuggageType encodeRegisteredLuggage(Collection registeredLuggage) throws EncodingFormatException { + if (registeredLuggage== null || registeredLuggage.isEmpty()) { + return null; + } + SequenceOfRegisteredLuggageType asnList = new SequenceOfRegisteredLuggageType(); + + for ( IRegisteredLuggage luggage : registeredLuggage){ + RegisteredLuggageType asnLuggage = new RegisteredLuggageType(); + asnLuggage.setMaxSize(UicEncoderUtils.getRestrictedInt(luggage.getMaxSize(),1,300)); + asnLuggage.setMaxWeight(UicEncoderUtils.getRestrictedInt(luggage.getMaxWeight(),1,99)); + asnLuggage.setRegistrationId(luggage.getRegistrationId()); + + asnList.add(asnLuggage); + } + + if (asnList.isEmpty()){ + return null; + } + return asnList; + } + + /** + * Convert open ticket. + * + * @param document the document + * @param issuingDate the issuing date + * @return the document data + * @throws EncodingFormatException the encoding format exception + */ + private DocumentData convertOpenTicket(IOpenTicket document,Date issuingDate) throws EncodingFormatException { + + DocumentData asnDocument = new DocumentData(); + TicketDetailData asnTicket = new TicketDetailData(); + asnDocument.setTicket(asnTicket); + + OpenTicketData asnData = new OpenTicketData(); + asnTicket.setOpenTicket(asnData); + + asnDocument.setTicket(asnTicket); + asnTicket.setOpenTicket(asnData); + + asnData.setProductOwnerNum(UicEncoderUtils.getNum(document.getProductOwner())); + asnData.setProductOwnerIA5(UicEncoderUtils.getIA5NonNum(document.getProductOwner())); + + asnData.setProductIdNum(UicEncoderUtils.getNum(document.getProductId())); + asnData.setProductIdIA5(UicEncoderUtils.getIA5NonNum(document.getProductId())); + + asnData.setReferenceNum(UicEncoderUtils.getNum(document.getReference())); + asnData.setReferenceIA5(UicEncoderUtils.getIA5NonNum(document.getReference())); + + asnData.setInfoText(document.getInfoText()); + asnData.setExtension(encodeExtension(document.getExtension())); + + if (document.getStationCodeTable() != CodeTableType.stationUIC){ + asnData.setStationCodeTable(document.getStationCodeTable()); + } + + asnData.setFromStationIA5(UicEncoderUtils.getIA5NonNum(document.getFromStation())); + asnData.setFromStationNum(UicEncoderUtils.getNum(document.getFromStation())); + + asnData.setToStationIA5(UicEncoderUtils.getIA5NonNum(document.getToStation())); + asnData.setToStationNum(UicEncoderUtils.getNum(document.getToStation())); + + asnData.setFromStationNameUTF8(document.getFromStationName()); + asnData.setToStationNameUTF8(document.getToStationName()); + + asnData.setCarriersNum(SequenceOfCarrierNum.getSequence(UicEncoderUtils.getNumList(document.getIncludedCarriers()))); + asnData.setCarriersIA5(UicEncoderUtils.getIA5NonNumList(document.getIncludedCarriers())); + + asnData.setExcludedServiceBrands(SequenceOfServiceBrands.getSequence(UicEncoderUtils.encodeRestrictedIntegerCollection(document.getExcludedServiceBrands(),1,32000))); + asnData.setIncludedServiceBrands(SequenceOfServiceBrands.getSequence(UicEncoderUtils.encodeRestrictedIntegerCollection(document.getIncludedServiceBrands(),1,32000))); + + asnData.setValidityDates(document.getValidFrom(), document.getValidUntil(), issuingDate); + + if (document.getValidFromUTCoffset() != null) { + asnData.setValidFromUTCOffset(document.getValidFromUTCoffset()); + if (document.getValidUntilUTCoffset() != null && document.getValidUntilUTCoffset() != document.getValidFromUTCoffset()){ + asnData.setValidUntilUTCOffset(document.getValidUntilUTCoffset()); + } + } + + + asnData.setTariffs(encodeTariffCollection(document.getTariffs())); + + asnData.setActivatedDays(document.getActivatedDays(),document.getValidFrom()); + + if (document.getClassCode() != TravelClassType.second){ + asnData.setClassCode(document.getClassCode()); + } + if (document.getExternalIssuer()>0) { + asnData.setExtIssuerId(new Long(document.getExternalIssuer())); + } + + if (document.getAuthorizationCode()>0) { + asnData.setIssuerAutorizationId(new Long (document.getAuthorizationCode())); + } + + if (document.getValidRegionList() != null && document.getValidRegionList().size() > 0) { + asnData.setValidRegion(encodeRegionCollection(document.getValidRegionList(), issuingDate)); + } + asnData.setValidRegionDesc(document.getValidRegionDesc()); + + if (document.getIncludedAddOns() != null && document.getIncludedAddOns().size() > 0) { + asnData.setIncludedAddOns(encodeIncludedAddons(document.getIncludedAddOns(),issuingDate, document.getClassCode())); + } + + if (document.getLuggageRestriction() != null) { + asnData.setLuggage(encodeLuggage(document.getLuggageRestriction())); + } + + if (document.isReturnIncluded()) { + asnData.setReturnIncluded(true); + asnData.setReturnDescription(encodeReturnDescription(document.getReturnDescription(),issuingDate)); + } else { + asnData.setReturnIncluded(false); + } + + asnData.setPrice(document.getPrice()); + + if (document.getVatDetails() != null && !document.getVatDetails().isEmpty()){ + for (IVatDetail vat : document.getVatDetails()) { + asnData.addVatDetail(encodeVatDetail(vat)); + } + } + + + return asnDocument; + } + + /** + * Convert counter mark. + * + * @param document the document + * @param issuingDate the issuing date + * @return the document data + * @throws EncodingFormatException the encoding format exception + */ + private DocumentData convertCounterMark(ICounterMark document, Date issuingDate) throws EncodingFormatException { + + DocumentData asnDocument = new DocumentData(); + TicketDetailData asnTicket = new TicketDetailData(); + asnDocument.setTicket(asnTicket); + + CountermarkData asnData = new CountermarkData(); + asnDocument.getTicket().setCounterMark(asnData); + + asnData.setProductOwnerNum(UicEncoderUtils.getNum(document.getProductOwner())); + asnData.setProductOwnerIA5(UicEncoderUtils.getIA5NonNum(document.getProductOwner())); + + + asnData.setTicketReferenceNum(UicEncoderUtils.getNum(document.getTicketReference())); + asnData.setTicketReferenceIA5(UicEncoderUtils.getIA5NonNum(document.getTicketReference())); + + asnData.setInfoText(document.getInfoText()); + asnData.setExtension(encodeExtension(document.getExtension())); + + if (document.getStationCodeTable() != CodeTableType.stationUIC){ + asnData.setStationCodeTable(document.getStationCodeTable()); + } + + asnData.setFromStationIA5(UicEncoderUtils.getIA5NonNum(document.getFromStation())); + asnData.setFromStationNum(UicEncoderUtils.getNum(document.getFromStation())); + + asnData.setToStationIA5(UicEncoderUtils.getIA5NonNum(document.getToStation())); + asnData.setToStationNum(UicEncoderUtils.getNum(document.getToStation())); + + asnData.setFromStationNameUTF8(document.getFromStationName()); + asnData.setToStationNameUTF8(document.getToStationName()); + + asnData.setValidityDates(document.getValidFrom(), document.getValidUntil(), issuingDate); + + if (document.getValidFromUTCoffset() != null) { + asnData.setValidFromUTCOffset(document.getValidFromUTCoffset()); + if (document.getValidUntilUTCoffset() != null && document.getValidUntilUTCoffset() != document.getValidFromUTCoffset()){ + asnData.setValidUntilUTCOffset(document.getValidUntilUTCoffset()); + } + } + + + + asnData.setReferenceNum(UicEncoderUtils.getNum(document.getReference())); + asnData.setReferenceIA5(UicEncoderUtils.getIA5NonNum(document.getReference())); + + asnData.setInfoText(document.getInfoText()); + asnData.setExtension(encodeExtension(document.getExtension())); + + if (document.getStationCodeTable() != CodeTableType.stationUIC){ + asnData.setStationCodeTable(document.getStationCodeTable()); + } + + asnData.setFromStationIA5(UicEncoderUtils.getIA5NonNum(document.getFromStation())); + asnData.setFromStationNum(UicEncoderUtils.getNum(document.getFromStation())); + + asnData.setToStationIA5(UicEncoderUtils.getIA5NonNum(document.getToStation())); + asnData.setToStationNum(UicEncoderUtils.getNum(document.getToStation())); + + asnData.setFromStationNameUTF8(document.getFromStationName()); + asnData.setToStationNameUTF8(document.getToStationName()); + + asnData.setCarriersNum(SequenceOfCarrierNum.getSequence(UicEncoderUtils.getNumList(document.getIncludedCarriers()))); + asnData.setCarriersIA5(UicEncoderUtils.getIA5NonNumList(document.getIncludedCarriers())); + + asnData.setExcludedServiceBrands(SequenceOfServiceBrands.getSequence(UicEncoderUtils.encodeRestrictedIntegerCollection(document.getExcludedServiceBrands(),1,32000))); + asnData.setIncludedServiceBrands(SequenceOfServiceBrands.getSequence(UicEncoderUtils.encodeRestrictedIntegerCollection(document.getIncludedServiceBrands(),1,32000))); + + if (document.getValidRegionList() != null && document.getValidRegionList().size() > 0) { + asnData.setValidRegion(encodeRegionCollection(document.getValidRegionList(), issuingDate)); + } + asnData.setValidRegionDesc(document.getValidRegionDesc()); + + if (document.getClassCode() != TravelClassType.second){ + asnData.setClassCode(document.getClassCode()); + } + + + if (document.isReturnIncluded()) { + asnData.setReturnIncluded(true); + if (document.getReturnDescription()!= null) { + asnData.setReturnDescription(encodeReturnDescription(document.getReturnDescription(),issuingDate)); + } + } else { + asnData.setReturnIncluded(false); + } + + return asnDocument; + } + + + + /** + * Encode via station collection. + * + * @param stations the stations + * @return the list + * @throws EncodingFormatException the encoding format exception + */ + private SequenceOfViaStationType encodeViaStationCollection(Collection stations) throws EncodingFormatException { + if (stations== null || stations.isEmpty()) { + return null; + } + SequenceOfViaStationType asnList = new SequenceOfViaStationType(); + + for ( IViaStation station : stations) { + ViaStationType asnData = encodeViaStation(station); + if (asnData != null) { + asnList.add(asnData); + } + } + + if (asnList.isEmpty()) { + return null; + } + + return asnList; + } + + + /** + * Encode via station. + * + * @param data the data + * @return the via station type + * @throws EncodingFormatException the encoding format exception + */ + private ViaStationType encodeViaStation(IViaStation data) throws EncodingFormatException { + if (data == null) return null; + + ViaStationType asnData =new ViaStationType(); + + if (data.getStationCodeTable() != CodeTableType.stationUIC){ + asnData.setStationCodeTable(data.getStationCodeTable()); + } + + asnData.setStationIA5(UicEncoderUtils.getIA5NonNum(data.getStation())); + asnData.setStationNum(UicEncoderUtils.getNum(data.getStation())); + + asnData.setAlternativeRoutes(encodeViaStationCollection(data.getAlternativeRoutes())); + + if (data.isBorder()) { + asnData.setBorder(true); + } else { + asnData.setBorder(false); + } + + asnData.setCarriersNum(SequenceOfCarrierNum.getSequence(UicEncoderUtils.getNumList(data.getCarriers()))); + asnData.setCarriersIA5(UicEncoderUtils.getIA5NonNumList(data.getCarriers())); + + asnData.setRoute(encodeViaStationCollection(data.getRoute())); + + if (data.getRouteId() > 0){ + asnData.setSeriesId(new Long(data.getRouteId())); + } + return asnData; + } + + /** + * Encode zone. + * + * @param data the data + * @return the zone type + * @throws EncodingFormatException the encoding format exception + */ + private ZoneType encodeZone(IZone data) throws EncodingFormatException { + if (data == null) return null; + + ZoneType asnData =new ZoneType(); + + if (data.getBinaryZoneId() != null && data.getBinaryZoneId().length > 0) { + asnData.setBinaryZoneId(data.getBinaryZoneId()); + } + asnData.setCarrierNum(UicEncoderUtils.getNum(data.getCarrier())); + asnData.setCarrierIA5(UicEncoderUtils.getIA5NonNum(data.getCarrier())); + + if (data.getStationCodeTable() != CodeTableType.stationUIC){ + asnData.setStationCodeTable(data.getStationCodeTable()); + } + asnData.setEntryStationIA5(UicEncoderUtils.getIA5NonNum(data.getEntryStation())); + asnData.setEntryStationNum(UicEncoderUtils.getNum(data.getEntryStation())); + + asnData.setTerminatingStationIA5(UicEncoderUtils.getIA5NonNum(data.getTerminatingStation())); + asnData.setTerminatingStationNum(UicEncoderUtils.getNum(data.getTerminatingStation())); + + asnData.setZoneId(SequenceOfUnrestrictedLong.getSequence(UicEncoderUtils.encodeIntegerCollection(data.getZoneIds()))); + + if (data.getCity() > 0) { + asnData.setCity(Asn1BigInteger.toAsn1(data.getCity())); + } + + if (data.getNUTScode() != null && data.getNUTScode().length() > 0) { + asnData.setNutsCode(data.getNUTScode()); + } + + return asnData; + } + + + + + + /** + * Encode train link. + * + * @param data the data + * @param issuingDate the issuing date + * @return the train link type + * @throws EncodingFormatException the encoding format exception + */ + private TrainLinkType encodeTrainLink(ITrainLink data, Date issuingDate) throws EncodingFormatException { + if (data == null) return null; + + TrainLinkType asnData =new TrainLinkType(); + + asnData.setFromStationIA5(UicEncoderUtils.getIA5NonNum(data.getFromStation())); + asnData.setFromStationNum(UicEncoderUtils.getNum(data.getFromStation())); + + asnData.setToStationIA5(UicEncoderUtils.getIA5NonNum(data.getToStation())); + asnData.setToStationNum(UicEncoderUtils.getNum(data.getToStation())); + + asnData.setFromStationName(data.getFromStationName()); + asnData.setToStationName(data.getToStationName()); + + asnData.setTrainIA5(UicEncoderUtils.getIA5NonNum(data.getTrain())); + asnData.setTrainNum(UicEncoderUtils.getNum(data.getTrain())); + + asnData.setDepartureDate(data.getDepartureDateTime(), issuingDate); + + return asnData; + } + + /** + * Encode polygone. + * + * @param data the data + * @return the polygone type + * @throws EncodingFormatException the encoding format exception + */ + private PolygoneType encodePolygone(IPolygone data) throws EncodingFormatException { + if (data == null) return null; + if (data.getEdges() == null || data.getEdges().isEmpty()) { + return null; + } + + PolygoneType asnData = new PolygoneType(); + SequenceOfDeltaCoordinates asnList = new SequenceOfDeltaCoordinates(); + + boolean firstEdge = true; + + for ( IGeoCoordinate edge : data.getEdges()) { + + if (firstEdge) { + asnData.setFirstEdge(encodeGeoCoordinate(edge)); + firstEdge = false; + } else { + DeltaCoordinates delta = new DeltaCoordinates(); + delta.setLatitude(Asn1BigInteger.toAsn1(asnData.getFirstEdge().getLatitude() - edge.getLatitude())); + delta.setLatitude(Asn1BigInteger.toAsn1(asnData.getFirstEdge().getLongitude() - edge.getLongitude())); + } + + } + if (!asnList.isEmpty()) { + asnData.setEdges(asnList); + } + return asnData; + } + + /** + * Encode line. + * + * @param data the data + * @return the line type + * @throws EncodingFormatException the encoding format exception + */ + private LineType encodeLine(ILine data) throws EncodingFormatException { + if (data == null) return null; + LineType asnData =new LineType(); + + asnData.setBinaryZoneId(data.getBinaryZoneId()); + asnData.setCarrierNum(UicEncoderUtils.getNum(data.getCarrier())); + asnData.setCarrierIA5(UicEncoderUtils.getIA5NonNum(data.getCarrier())); + if (data.getStationCodeTable() != CodeTableType.stationUIC){ + asnData.setStationCodeTable(data.getStationCodeTable()); + } + asnData.setEntryStationIA5(UicEncoderUtils.getIA5NonNum(data.getEntryStation())); + asnData.setEntryStationNum(UicEncoderUtils.getNum(data.getEntryStation())); + + asnData.setTerminatingStationIA5(UicEncoderUtils.getIA5NonNum(data.getTerminatingStation())); + asnData.setTerminatingStationNum(UicEncoderUtils.getNum(data.getTerminatingStation())); + + if (data.getCity() > 0) { + asnData.setCity(new Long(data.getCity())); + } + + asnData.setLineId(SequenceOfUnrestrictedLong.getSequence(UicEncoderUtils.encodeIntegerCollection(data.getLineIds()))); + + return asnData; + } + + + + /** + * Encode region collection. + * + * @param regionList the region list + * @param issuingDate the issuing date + * @return the list + * @throws EncodingFormatException the encoding format exception + */ + private SequenceOfRegionalValidityType encodeRegionCollection(Collection regionList, Date issuingDate) throws EncodingFormatException { + + if (regionList == null || regionList.isEmpty()) { + return null; + } + + SequenceOfRegionalValidityType asnList = new SequenceOfRegionalValidityType(); + + for ( IRegionalValidity region :regionList) { + + RegionalValidityType asnRegion = new RegionalValidityType(); + + if (region instanceof ILine ) { + LineType line = encodeLine((ILine)region); + if (line == null){ + break; + } + asnRegion.setLines(line); + asnList.add(asnRegion); + } else if (region instanceof IPolygone ) { + PolygoneType line = encodePolygone((IPolygone)region); + if (line == null){ + break; + } + asnRegion.setPolygone(line); + asnList.add(asnRegion); + } else if (region instanceof ITrainLink ) { + TrainLinkType trainLink = encodeTrainLink((ITrainLink)region, issuingDate); + if (trainLink == null){ + break; + } + asnRegion.setTrainLink(trainLink); + asnList.add(asnRegion); + } else if (region instanceof IViaStation ) { + ViaStationType viaStation = encodeViaStation((IViaStation)region); + if (viaStation == null){ + break; + } + asnRegion.setViaStations(viaStation); + asnList.add(asnRegion); + } else if (region instanceof IZone ) { + ZoneType zone = encodeZone((IZone)region); + if (zone == null){ + break; + } + asnRegion.setZones(zone); + asnList.add(asnRegion); + } + + } + + if (asnList.isEmpty()) { + return null; + } + + return asnList; + } + + /** + * Convert parking ground. + * + * @param document the document + * @param issuingDate the issuing date + * @return the document data + * @throws EncodingFormatException the encoding format exception + */ + private DocumentData convertParkingGround(IParkingGround document, Date issuingDate) throws EncodingFormatException { + + DocumentData asnDocument = new DocumentData(); + TicketDetailData asnTicket = new TicketDetailData(); + asnDocument.setTicket(asnTicket); + + ParkingGroundData asnData = new ParkingGroundData(); + asnDocument.getTicket().setParkingGround(asnData); + + asnData.setProductOwnerNum(UicEncoderUtils.getNum(document.getProductOwner())); + asnData.setProductOwnerIA5(UicEncoderUtils.getIA5NonNum(document.getProductOwner())); + + asnData.setReferenceNum(Asn1BigInteger.toAsn1(UicEncoderUtils.getNum(document.getReference()))); + asnData.setReferenceIA5(UicEncoderUtils.getIA5NonNum(document.getReference())); + + asnData.setExtension(encodeExtension(document.getExtension())); + + if (document.getStationCodeTable() != CodeTableType.stationUIC){ + asnData.setStationCodeTable(document.getStationCodeTable()); + } + + asnData.setStationIA5(UicEncoderUtils.getIA5NonNum(document.getStation())); + asnData.setStationNum(UicEncoderUtils.getNum(document.getStation())); + + asnData.setAccessCode(UicEncoderUtils.getIA5(document.getAccessCode())); + asnData.setEntryTrack(document.getEntryTrack()); + + if (document.getFromParkingDate() != null){ + asnData.setParkingDates(document.getFromParkingDate(),document.getToParkingDate(),issuingDate); + } + + asnData.setLocation(document.getLocation()); + asnData.setNumberPlate(UicEncoderUtils.getIA5(document.getNumberPlate())); + asnData.setParkingGroundId(UicEncoderUtils.getIA5(document.getParkingGroundId())); + asnData.setSpecialInformation(document.getSpecialInformation()); + + asnData.setPrice(document.getPrice()); + + if (document.getVatDetails() != null && !document.getVatDetails().isEmpty()){ + for (IVatDetail vat : document.getVatDetails()) { + asnData.addVatDetail(encodeVatDetail(vat)); + } + } + + return asnDocument; + } + + + /** + * Encode route section. + * + * @param route the route + * @return the route section type + * @throws EncodingFormatException the encoding format exception + */ + private RouteSectionType encodeRouteSection(IRouteSection route) throws EncodingFormatException { + + if (route == null) return null; + + RouteSectionType asnRoute = new RouteSectionType(); + + + if (route.getStationCodeTable() != CodeTableType.stationUIC){ + asnRoute.setStationCodeTable(route.getStationCodeTable()); + } + + asnRoute.setFromStationIA5(UicEncoderUtils.getIA5NonNum(route.getFromStation())); + asnRoute.setFromStationNum(UicEncoderUtils.getNum(route.getFromStation())); + + asnRoute.setToStationIA5(UicEncoderUtils.getIA5NonNum(route.getToStation())); + asnRoute.setToStationNum(UicEncoderUtils.getNum(route.getToStation())); + + asnRoute.setFromStationNameUTF8(route.getFromStationName()); + asnRoute.setToStationNameUTF8(route.getToStationName()); + + + return asnRoute; + } + + /** + * Encode tariff collection. + * + * @param tariffs the tariffs + * @return the list + * @throws EncodingFormatException the encoding format exception + */ + private SequenceOfTariffType encodeTariffCollection(Collection tariffs) throws EncodingFormatException { + + if (tariffs== null || tariffs.isEmpty()) { + return null; + } + + SequenceOfTariffType tariffList = new SequenceOfTariffType(); + + for (ITariff tariff :tariffs){ + + TariffType asnTariff = encodeTariff(tariff); + + if (asnTariff != null) { + tariffList.add(asnTariff); + } + } + + if (tariffList.isEmpty()) { + return null; + } + + return tariffList; + } + + /** + * Encode tariff. + * + * @param tariff the tariff + * @return the tariff type + * @throws EncodingFormatException the encoding format exception + */ + private TariffType encodeTariff(ITariff tariff) throws EncodingFormatException { + + if (tariff == null ) { + return null; + } + + TariffType asnTariff = new TariffType(); + + asnTariff.setAgeAbove(UicEncoderUtils.getRestrictedInt(tariff.getAgeAbove(),2,120)); + asnTariff.setAgeBelow(UicEncoderUtils.getRestrictedInt(tariff.getAgeBelow(),1,40)); + + asnTariff.setNumberOfPassengers(UicEncoderUtils.getRestrictedIntWithDefault(tariff.getNumberOfPassengers(),1,200,1)); + + + try { + PassengerType passengerType = PassengerType.valueOf(tariff.getPassengerType().name()); + asnTariff.setPassengerType(passengerType); + } catch (Exception e){ + //no value found + } + + + asnTariff.setReductionCard(encodeCardReferences(tariff.getReductionCards())); + asnTariff.setRestrictedToCountryOfResidence(tariff.isRestrictedToCountryOfResidence()); + asnTariff.setRestrictedToRouteSection(encodeRouteSection(tariff.getRestrictedToRouteSection())); + + if (tariff.getSeriesDataDetails()!= null) { + asnTariff.setSeriesDataDetails(encodeSeriesDataDetails(tariff.getSeriesDataDetails())); + } + + + asnTariff.setTariffDesc(tariff.getTariffDescription()); + asnTariff.setTariffIdIA5(UicEncoderUtils.getIA5NonNum(tariff.getTariffId())); + asnTariff.setTariffIdNum(UicEncoderUtils.getNum(tariff.getTariffId())); + + asnTariff.setTraverlerid(SequenceOfTravelerId.getSequence(UicEncoderUtils.encodeRestrictedIntegerCollection(tariff.getTravelerIds(), 1, 254))); + + return asnTariff; + } + + private SeriesDetailType encodeSeriesDataDetails( ISeriesDataDetails seriesDataDetails) throws EncodingFormatException { + SeriesDetailType details = new SeriesDetailType(); + details.setSeries(UicEncoderUtils.getRestrictedInt(seriesDataDetails.getSeries(), 1, 99999)); + details.setSupplyingCarrier(UicEncoderUtils.getRestrictedInt(seriesDataDetails.getSupplyingCarrier(), 1, 9999)); + details.setOfferIdentification(UicEncoderUtils.getRestrictedInt(seriesDataDetails.getOfferIdentification(), 1, 99)); + return null; + } + + /** + * Convert voucher. + * + * @param document the document + * @param issuingDate the issuing date + * @return the document data + * @throws EncodingFormatException the encoding format exception + */ + private DocumentData convertVoucher(IVoucher document, Date issuingDate) throws EncodingFormatException { + DocumentData asnDocument = new DocumentData(); + TicketDetailData asnTicket = new TicketDetailData(); + asnDocument.setTicket(asnTicket); + + + VoucherData asnData = new VoucherData(); + asnDocument.getTicket().setVoucher(asnData); + + asnData.setProductOwnerNum(UicEncoderUtils.getNum(document.getProductOwner())); + asnData.setProductOwnerIA5(UicEncoderUtils.getIA5NonNum(document.getProductOwner())); + + asnData.setProductIdNum(UicEncoderUtils.getNum(document.getProductId())); + asnData.setProductIdIA5(UicEncoderUtils.getIA5NonNum(document.getProductId())); + + asnData.setReferenceNum(Asn1BigInteger.toAsn1(UicEncoderUtils.getNum(document.getReference()))); + asnData.setReferenceIA5(UicEncoderUtils.getIA5NonNum(document.getReference())); + + asnData.setInfoText(document.getInfoText()); + + asnData.setValidity(document.getValidFrom(), document.getValidUntil()); + + asnData.setType(UicEncoderUtils.getRestrictedInt(document.getType(), 1,9999)); + + asnData.setValue(UicEncoderUtils.getRestrictedInt(document.getAmount(),1,9999999)); + + return asnDocument; + } + + /** + * Convert pass. + * + * @param document the document + * @param issuingDate the issuing date + * @return the document data + * @throws EncodingFormatException the encoding format exception + */ + private DocumentData convertPass(IPass document, Date issuingDate) throws EncodingFormatException { + + DocumentData asnDocument = new DocumentData(); + TicketDetailData asnTicket = new TicketDetailData(); + asnDocument.setTicket(asnTicket); + + PassData asnData = new PassData(); + asnDocument.getTicket().setPass(asnData); + + asnData.setProductOwnerNum(UicEncoderUtils.getNum(document.getProductOwner())); + asnData.setProductOwnerIA5(UicEncoderUtils.getIA5NonNum(document.getProductOwner())); + + asnData.setProductIdNum(UicEncoderUtils.getNum(document.getProductId())); + asnData.setProductIdIA5(UicEncoderUtils.getIA5NonNum(document.getProductId())); + + asnData.setReferenceNum(Asn1BigInteger.toAsn1(UicEncoderUtils.getNum(document.getReference()))); + asnData.setReferenceIA5(UicEncoderUtils.getIA5NonNum(document.getReference())); + + asnData.setInfoText(document.getInfoText()); + asnData.setExtension(encodeExtension(document.getExtension())); + + asnData.setIncludedCarriersNum(SequenceOfCarrierNum.getSequence(UicEncoderUtils.getNumList(document.getIncludedCarriers()))); + asnData.setIncludedCarriersIA5(UicEncoderUtils.getIA5NonNumList(document.getIncludedCarriers())); + + asnData.setExcludedCarriersNum(SequenceOfCarrierNum.getSequence(UicEncoderUtils.getNumList(document.getExcludedCarriers()))); + asnData.setExcludedCarriersIA5(UicEncoderUtils.getIA5NonNumList(document.getExcludedCarriers())); + + asnData.setValidityDates(document.getValidFrom(), document.getValidUntil(), issuingDate); + + if (document.getValidFromUTCoffset() != null) { + asnData.setValidFromUTCOffset(document.getValidFromUTCoffset()); + if (document.getValidUntilUTCoffset() != null && document.getValidUntilUTCoffset() != document.getValidFromUTCoffset()){ + asnData.setValidUntilUTCOffset(document.getValidUntilUTCoffset()); + } + } + + + if(document.getActivatedDays()!= null && document.getActivatedDays().size() > 0) { + if (document.getValidFrom() != null) { + asnData.setActivatedDays(document.getActivatedDays(),document.getValidFrom()); + } else { + asnData.setActivatedDays(document.getActivatedDays(),issuingDate); + } + } + + asnData.setClassCode(document.getClassCode()); + + if (document.getCountries() != null && document.getCountries().size() > 0){ + asnData.setCountries(SequenceOfCountries.getSequence(UicEncoderUtils.encodeRestrictedIntegerCollection(document.getCountries(),1,250))); + } + + asnData.setExcludedServiceBrands(SequenceOfServiceBrands.getSequence(UicEncoderUtils.encodeRestrictedIntegerCollection(document.getExcludedServiceBrands(),1,32000))); + asnData.setIncludedServiceBrands(SequenceOfServiceBrands.getSequence(UicEncoderUtils.encodeRestrictedIntegerCollection(document.getIncludedServiceBrands(),1,32000))); + + asnData.setNumberOfDaysOfTravel(UicEncoderUtils.getRestrictedInt(document.getNumberOfDaysOfTravel(), 1, 93)); + asnData.setNumberOfPossibleTrips(UicEncoderUtils.getRestrictedInt(document.getNumberOfPossibleTrips(), 1, 30)); + asnData.setNumberOfValidityDays(UicEncoderUtils.getRestrictedInt(document.getNumberOfValidityDays(), 1, 370)); + asnData.setPassDescription(document.getPassDescription()); + if (document.getPassType() > 0 ){ + asnData.setPassType(new Long(document.getPassType())); + } + + asnData.setTariffs(encodeTariffCollection(document.getTariffs())); + + + + if(document.getValidRegionList()!= null && document.getValidRegionList().size() > 0) { + asnData.setValidRegion(encodeRegionCollection(document.getValidRegionList(), issuingDate)); + } + + if (document.getValidityDetails() != null) { + asnData.setValidityPeriodDetails(encodeValidityDetails(document.getValidityDetails(), issuingDate)); + } + + + + asnData.setPrice(document.getPrice()); + + if (document.getVatDetails() != null && !document.getVatDetails().isEmpty()){ + for (IVatDetail vat : document.getVatDetails()) { + asnData.addVatDetail(encodeVatDetail(vat)); + } + } + + + return asnDocument; + } + + + private ValidityPeriodDetailType encodeValidityDetails( IValidityDetails validityDetails, Date referenceDate) throws EncodingFormatException { + + if ( (validityDetails.getTimeRanges() == null || validityDetails.getTimeRanges().isEmpty()) + || + (validityDetails.getValidityRanges() == null || validityDetails.getValidityRanges().isEmpty()) ) { + return null; + } + + + ValidityPeriodDetailType asnData = new ValidityPeriodDetailType(); + + if (validityDetails.getTimeRanges() != null) { + for (ITimeRange range : validityDetails.getTimeRanges()) { + + if (range.getFromTime() == range.getUntilTime()){ + break; + } + + TimeRangeType asnRange = new TimeRangeType(); + asnRange.setFromTime(new Long(range.getFromTime())); + asnRange.setUntilTime(new Long(range.getUntilTime())); + + asnData.getExcludedTimeRange().add(asnRange); + } + } + + if (validityDetails.getValidityRanges() != null) { + for (IValidityRange range : validityDetails.getValidityRanges()) { + + ValidityPeriodType asnRange = new ValidityPeriodType(); + + asnRange.setValidityDates(range.getFromDate(), range.getUntilDate(), referenceDate); + + if (range.getValidFromUTCoffset() != null) { + asnRange.setValidFromUTCOffset(range.getValidFromUTCoffset()); + if (range.getValidUntilUTCoffset() != null && range.getValidUntilUTCoffset() != range.getValidFromUTCoffset()){ + asnRange.setValidUntilUTCOffset(range.getValidUntilUTCoffset()); + } + } + + + asnData.getValidityPeriod().add(asnRange); + } + } + + return asnData; + } + + /** + * Convert fip ticket. + * + * @param document the document + * @param issuingDate the issuing date + * @return the document data + * @throws EncodingFormatException the encoding format exception + */ + private DocumentData convertFipTicket(IFipTicket document, Date issuingDate) throws EncodingFormatException { + + DocumentData asnDocument = new DocumentData(); + TicketDetailData asnTicket = new TicketDetailData(); + asnDocument.setTicket(asnTicket); + + + FIPTicketData asnData = new FIPTicketData(); + asnDocument.getTicket().setFipTicket(asnData); + + asnData.setProductOwnerNum(UicEncoderUtils.getNum(document.getProductOwner())); + asnData.setProductOwnerIA5(UicEncoderUtils.getIA5NonNum(document.getProductOwner())); + + asnData.setProductIdNum(UicEncoderUtils.getNum(document.getProductId())); + asnData.setProductIdIA5(UicEncoderUtils.getIA5NonNum(document.getProductId())); + + asnData.setReferenceNum(Asn1BigInteger.toAsn1(UicEncoderUtils.getNum(document.getReference()))); + asnData.setReferenceIA5(UicEncoderUtils.getIA5NonNum(document.getReference())); + + asnData.setCarrierNum(SequenceOfCarrierNum.getSequence(UicEncoderUtils.getNumList(document.getCarriers()))); + asnData.setCarrierIA5(UicEncoderUtils.getIA5NonNumList(document.getCarriers())); + + asnData.setClassCode(document.getClassCode()); + + if (!document.isIncludesSupplements()){ + asnData.setIncludesSupplements(false); + } + asnData.setNumberOfTravelDays(UicEncoderUtils.getRestrictedInt(document.getNumberOfTravelDates(), 1, 8)); + + asnData.setValidityDates(document.getValidFrom(), document.getValidUntil(), issuingDate); + + if(document.getActivatedDays()!= null && document.getActivatedDays().size() > 0) { + asnData.setActivatedDays(document.getActivatedDays(), document.getValidFrom()); + } + + asnData.setExtension(encodeExtension(document.getExtension())); + + return asnDocument; + } + + /** + * Convert station passage. + * + * @param document the document + * @param issuingDate the issuing date + * @return the document data + * @throws EncodingFormatException the encoding format exception + */ + private DocumentData convertStationPassage(IStationPassage document,Date issuingDate) throws EncodingFormatException { + + DocumentData asnDocument = new DocumentData(); + TicketDetailData asnTicket = new TicketDetailData(); + asnDocument.setTicket(asnTicket); + + StationPassageData asnData = new StationPassageData(); + asnDocument.getTicket().setStationPassage(asnData); + + asnData.setProductOwnerNum(UicEncoderUtils.getNum(document.getProductOwner())); + asnData.setProductOwnerIA5(UicEncoderUtils.getIA5NonNum(document.getProductOwner())); + + asnData.setProductIdNum(UicEncoderUtils.getNum(document.getProductId())); + asnData.setProductIdIA5(UicEncoderUtils.getIA5NonNum(document.getProductId())); + + asnData.setReferenceNum(Asn1BigInteger.toAsn1(UicEncoderUtils.getNum(document.getReference()))); + asnData.setReferenceIA5(UicEncoderUtils.getIA5NonNum(document.getReference())); + + if (document.getNumberOfdaysAllowed() > 0) { + asnData.setNumberOfDaysValid(new Long(document.getNumberOfdaysAllowed())); + } + + asnData.setProductName(document.getProductName()); + + if (document.getStationCodeTable() != CodeTableType.stationUIC){ + asnData.setStationCodeTable(document.getStationCodeTable()); + } + + + if (document.getStations()!= null && !document.getStations().isEmpty()){ + SequenceOfStringIA5 stationsIA5 = new SequenceOfStringIA5(); + SequenceOfUnrestrictedLong stationsNum = new SequenceOfUnrestrictedLong(); + + for (String station : document.getStations()) { + String ia5 = UicEncoderUtils.getIA5NonNum(station); + Long num = UicEncoderUtils.getNum(station); + if (ia5 != null && ia5.length() > 0) { + stationsIA5.add(ia5); + } + if (num != null && num > 0){ + stationsNum.add(num); + } + } + if (!stationsIA5.isEmpty()){ + asnData.setStationIA5(stationsIA5); + } + if (!stationsNum.isEmpty()){ + asnData.setStationNum(stationsNum); + } + } + + if (document.getStationNames()!= null && !document.getStationNames().isEmpty()) { + + SequenceOfStringUTF8 asnList = new SequenceOfStringUTF8(); + for ( String text :document.getStationNames()){ + if (text.length() > 0) { + asnList.add(text); + } + } + + if (!asnList.isEmpty()) { + asnData.setStationNameUTF8(asnList); + } + + } + + asnData.setValidityDates(document.getValidFrom(), document.getValidUntil(), issuingDate); + + if (document.getValidFromUTCoffset() != null) { + asnData.setValidFromUTCOffset(document.getValidFromUTCoffset()); + if (document.getValidUntilUTCoffset() != null && document.getValidUntilUTCoffset() != document.getValidFromUTCoffset()){ + asnData.setValidUntilUTCOffset(document.getValidUntilUTCoffset()); + } + } + + + asnData.setExtension(encodeExtension(document.getExtension())); + + asnData.setAreaCodeNum(SequenceOfUnrestrictedLong.getSequence(UicEncoderUtils.getNumList(document.getAreaCodes()))); + asnData.setAreaCodeIA5(UicEncoderUtils.getIA5NonNumList(document.getAreaCodes())); + + asnData.setAreaNameUTF8(UicEncoderUtils.encodeStringCollection(document.getAreaNames())); + + return asnDocument; + } + + + /** + * Encode control details. + * + * @param data the data + * @param issuingDate the issuing date + * @return the control data + * @throws EncodingFormatException the encoding format exception + */ + private ControlData encodeControlDetails(IControlDetail data, Date issuingDate) throws EncodingFormatException { + + if (data == null) return null; + + ControlData asnData = new ControlData(); + + asnData.setExtension(encodeExtension(data.getExtension())); + + if (data.isAgeCheckRequired()) { + asnData.setAgeCheckRequired(true); + } else { + asnData.setAgeCheckRequired(false); + } + + if (data.isIdentificationByIdCard()){ + asnData.setIdentificationByIdCard(true); + } else { + asnData.setIdentificationByIdCard(false); + } + + if (data.isIdentificationByPassportId()){ + asnData.setIdentificationByPassportId(true); + } else { + asnData.setIdentificationByPassportId(false); + } + + if (data.isOnlineValidationRequired()){ + asnData.setOnlineValidationRequired(true); + } else { + asnData.setOnlineValidationRequired(false); + } + + if (data.isPassportValidationRequired()){ + asnData.setPassportValidationRequired(true); + } else { + asnData.setPassportValidationRequired(false); + } + + if (data.isReductionCardCheckRequired()){ + asnData.setReductionCardCheckRequired(true); + } else { + asnData.setReductionCardCheckRequired(false); + } + + asnData.setInfoText(data.getInfoText()); + + asnData.setRandomDetailedValidationRequired(UicEncoderUtils.getRestrictedInt(data.getRandomDetailedValidationRequired(), 0,99)); + + asnData.setIdentificationItem(UicEncoderUtils.getUnRestrictedInt(data.getIdentificationItem())); + + asnData.setIncludedTickets(encodeTicketLickList(data.getLinkedTickets())); + + asnData.setIdentificationByCardReference(encodeCardReferences(data.getIdentificationByCardReference())); + + return asnData; + } + + + + /** + * Encode card references. + * + * @param cardReferences the card references + * @return the list + * @throws EncodingFormatException the encoding format exception + */ + private SequenceOfCardReferenceType encodeCardReferences(Collection cardReferences) throws EncodingFormatException { + + if (cardReferences == null || cardReferences.isEmpty()) return null; + + SequenceOfCardReferenceType asnList = new SequenceOfCardReferenceType(); + + for (ICardReference card : cardReferences){ + + CardReferenceType asnCard = new CardReferenceType(); + + if (card.getCardId() != null && card.getCardId().length() > 0){ + //only longs allowed + try { + long num = Long.parseLong(card.getCardId()); + asnCard.setCardIdNum(num); + } catch (Exception e ){ + asnCard.setCardIdIA5(card.getCardId()); + } + } + + + asnCard.setCardIssuerNum(UicEncoderUtils.getNum(card.getCardIssuer())); + asnCard.setCardIssuerIA5(UicEncoderUtils.getIA5NonNum(card.getCardIssuer())); + asnCard.setCardName(card.getCardName()); + asnCard.setCardType(UicEncoderUtils.getUnRestrictedInt(card.getCardType())); + + asnCard.setLeadingCardIdNum(UicEncoderUtils.getNum(card.getLeadingCardId())); + asnCard.setLeadingCardIdIA5(UicEncoderUtils.getIA5NonNum(card.getLeadingCardId())); + + asnCard.setTrailingCardIdNum(UicEncoderUtils.getNum(card.getTrailingCardId())); + asnCard.setTrailingCardIdIA5(UicEncoderUtils.getIA5NonNum(card.getTrailingCardId())); + + asnList.add(asnCard); + + } + + + if (asnList.isEmpty()) return null; + + return asnList; + } + + /** + * Encode ticket lick list. + * + * @param linkedTickets the linked tickets + * @return the list + * @throws EncodingFormatException the encoding format exception + */ + private SequenceOfTicketLinkType encodeTicketLickList(Collection linkedTickets) throws EncodingFormatException { + + if (linkedTickets == null || linkedTickets.isEmpty()) return null; + + SequenceOfTicketLinkType asnList = new SequenceOfTicketLinkType(); + + for (ITicketLink ticket : linkedTickets){ + + TicketLinkType asnTicket = new TicketLinkType(); + + asnTicket.setIssuerName(ticket.getIssuer()); + + asnTicket.setIssuerPNR(ticket.getIssuerPNR()); + + asnTicket.setProductOwnerNum(UicEncoderUtils.getNum(ticket.getProductOwner())); + asnTicket.setProductOwnerIA5(UicEncoderUtils.getIA5NonNum(ticket.getProductOwner())); + + asnTicket.setReferenceNum(UicEncoderUtils.getNum(ticket.getReference())); + asnTicket.setReferenceIA5(UicEncoderUtils.getIA5NonNum(ticket.getReference())); + + if (ticket.getTicketType() != TicketType.openTicket){ + asnTicket.setTicketType(ticket.getTicketType()); + } + + if (ticket.getLinkMode() != LinkMode.issuedTogether){ + asnTicket.setLinkMode(ticket.getLinkMode()); + } + + asnList.add(asnTicket); + } + + if (asnList.isEmpty()) return null; + + return asnList; + } + + /** + * Encode travelers. + * + * @param data the data + * @param issuingDate the issuing date + * @return the traveler data + * @throws EncodingFormatException the encoding format exception + */ + private TravelerData encodeTravelers(ITravelerDetail data,Date issuingDate) throws EncodingFormatException { + + if (data == null) return null; + + TravelerData asnData = new TravelerData(); + + asnData.setGroupName(data.getGroupName()); + + asnData.setPreferedLanguage(data.getPreferredLanguage()); + + if (data.getTravelers()!=null && !data.getTravelers().isEmpty()) { + + SequenceOfTravelerType asnTravelers = new SequenceOfTravelerType(); + + for (ITraveler traveler : data.getTravelers()){ + + TravelerType asnTraveler = encodeTraveler(traveler); + + if (asnTraveler !=null){ + asnTravelers.add(asnTraveler); + } + } + + if (!asnTravelers.isEmpty()) { + asnData.setTraveler(asnTravelers); + } + + } + + return asnData; + } + + /** + * Encode issuing details. + * + * @param data the data + * @return the issuing data + * @throws EncodingFormatException the encoding format exception + */ + private IssuingData encodeIssuingDetails(IIssuingDetail data) throws EncodingFormatException { + + if (data == null){ + throw new EncodingFormatException("Issuind data missing"); + } + + IssuingData asnData = new IssuingData(); + + if (!data.isActivated()){ + asnData.setActivated(false); + } else { + asnData.setActivated(true); + } + + if (!data.isSecurePaperTicket()) { + asnData.setSecurePaperTicket(false); + } else { + asnData.setSecurePaperTicket(true); + } + + asnData.setExtension(encodeExtension(data.getExtension())); + + + asnData.setIssuedOnLine(UicEncoderUtils.getRestrictedInt(data.getIssuedOnLine(), 1, 99999)); + + asnData.setIssuedOnTrainNum(UicEncoderUtils.getNum(data.getIssuedOnTrain())); + asnData.setIssuedOnTrainIA5(UicEncoderUtils.getIA5NonNum(data.getIssuedOnTrain())); + + + asnData.setSecurityProviderNum(UicEncoderUtils.getNum(data.getSecurityProvider())); + + + if (data.getIssuer()!= null && !data.getIssuer().equals(data.getSecurityProvider()) ){ + asnData.setIssuerNum(UicEncoderUtils.getNum(data.getIssuer())); + asnData.setIssuerIA5(UicEncoderUtils.getIA5NonNum(data.getIssuer())); + } + + asnData.setIssuerName(data.getIssuerName()); + asnData.setIssuerPNR(UicEncoderUtils.getIA5(data.getIssuerPNR())); + + + asnData.setIssuingDate(data.getIssuingDate()); + + if (data.isSpecimen()){ + asnData.setSpecimen(true); + } else { + asnData.setSpecimen(false); + } + + if (data.getPointOfSale()!= null){ + asnData.setPointOfSale(encodeGeoCoordinate(data.getPointOfSale())); + } + + return asnData; + } + + /** + * Encode geo coordinate. + * + * @param point the point + * @return the geo coordinate type + */ + private GeoCoordinateType encodeGeoCoordinate(IGeoCoordinate point) { + + if (point == null) return null; + + GeoCoordinateType asnPoint = new GeoCoordinateType(); + + asnPoint.setLatitude(point.getLatitude()); + asnPoint.setLongitude(point.getLongitude()); + + if (point.getUnit() != GeoUnitType.milliDegree){ + asnPoint.setGeoUnit(point.getUnit()); + } + + asnPoint.setAccuracy(point.getAccuracy()); + + if (point.getHemisphereLatitude() != HemisphereLatitudeType.east) { + asnPoint.setHemisphereLatitude(point.getHemisphereLatitude()); + } + + if (point.getHemisphereLongitude() != HemisphereLongitudeType.north) { + asnPoint.setHemisphereLongitude(point.getHemisphereLongitude()); + } + + if (point.getSystem() != GeoCoordinateSystemType.wgs84){ + asnPoint.setCoordinateSystem(point.getSystem()); + } + + return asnPoint; + } + + /** + * Encode customer card. + * + * @param customerCard the customer card + * @param issuingDate the issuing date + * @return the document data + * @throws EncodingFormatException the encoding format exception + */ + private DocumentData encodeCustomerCard(ICustomerCard customerCard,Date issuingDate) throws EncodingFormatException { + + DocumentData asnDocument = new DocumentData(); + TicketDetailData asnTicket = new TicketDetailData(); + asnDocument.setTicket(asnTicket); + + CustomerCardData asnCustomerCard = new CustomerCardData(); + asnDocument.getTicket().setCustomerCard(asnCustomerCard); + + + if (customerCard.getCardId() != null && customerCard.getCardId().length() > 0){ + //only longs allowed + try { + long num = Long.parseLong(customerCard.getCardId()); + asnCustomerCard.setCardIdNum(num); + } catch (Exception e ){ + asnCustomerCard.setCardIdIA5(customerCard.getCardId()); + } + } + + + asnCustomerCard.setCardType(UicEncoderUtils.getRestrictedInt(customerCard.getCardType(),1,1000)); + + asnCustomerCard.setCardTypeDescr(customerCard.getCardTypeDescr()); + + asnCustomerCard.setClassCode(customerCard.getClassCode()); + + if (customerCard.getCustomer()!=null){ + asnCustomerCard.setCustomer(encodeTraveler(customerCard.getCustomer())); + } + + asnCustomerCard.setCustomerStatus(UicEncoderUtils.getUnRestrictedInt(customerCard.getCustomerStatus())); + + asnCustomerCard.setCustomerStatusDescr(customerCard.getCustomerStatusDescr()); + + asnCustomerCard.setExtension(encodeExtension(customerCard.getExtension())); + + asnCustomerCard.setIncludedServices(SequenceOfUnrestrictedLong.getSequence(UicEncoderUtils.getUnRestrictedIntList(customerCard.getIncludedServices()))); + + asnCustomerCard.setValidity(customerCard.getValidFrom(), customerCard.getValidUntil()); + + return asnDocument; + } + + private TokenType encodeToken(IToken token) throws EncodingFormatException { + TokenType asnToken = new TokenType(); + asnToken.setToken(token.getToken()); + asnToken.setTokenProviderNum(UicEncoderUtils.getNum(token.getTokenProvider())); + asnToken.setTokenProviderIA5(UicEncoderUtils.getIA5NonNum(token.getTokenProvider())); + asnToken.setTokenSpecification(token.getTokenSpecification()); + return asnToken; + } + + + + + private VatDetailType encodeVatDetail(IVatDetail vatDetail) { + + if (vatDetail == null) return null; + + VatDetailType asnVatDetail = new VatDetailType(); + + asnVatDetail.setAmount(vatDetail.getAmount()); + asnVatDetail.setCountry(new Long(vatDetail.getCountry())); + asnVatDetail.setPercentage(new Long(vatDetail.getPercentage())); + asnVatDetail.setVatId(vatDetail.getVatId()); + + return asnVatDetail; + + } + + /** + * Encode traveler. + * + * @param traveler the traveler + * @return the traveler type + * @throws EncodingFormatException the encoding format exception + */ + private TravelerType encodeTraveler(ITraveler traveler) throws EncodingFormatException { + + if (traveler == null) return null; + + TravelerType asnTraveler = new TravelerType(); + + asnTraveler.setCountryOfResidence(UicEncoderUtils.getRestrictedInt(traveler.getCountryOfResidence(), 1, 999)); + asnTraveler.setCountryOfPassport(UicEncoderUtils.getRestrictedInt(traveler.getPassportCountry(), 1, 999)); + asnTraveler.setCountryOfIdCard(UicEncoderUtils.getRestrictedInt(traveler.getIDCardCountry(), 1, 999)); + + + asnTraveler.setCustomerIdNum(UicEncoderUtils.getNum(traveler.getCustomerId())); + asnTraveler.setCustomerIdIA5(UicEncoderUtils.getIA5NonNum(traveler.getCustomerId())); + + asnTraveler.setDateOfBirth(traveler.getDateOfBirth()); + + asnTraveler.setFirstName(traveler.getFirstName()); + asnTraveler.setSecondName(traveler.getSecondName()); + asnTraveler.setLastName(traveler.getLastName()); + asnTraveler.setGender(GenderType.valueOf(traveler.getGender().toString())); + asnTraveler.setIdCard(UicEncoderUtils.getIA5(traveler.getIdCard())); + asnTraveler.setPassportId(UicEncoderUtils.getIA5(traveler.getPassportId())); + asnTraveler.setTitle(UicEncoderUtils.getIA5(traveler.getTitle())); + + try { + PassengerType passengerType = PassengerType.valueOf(traveler.getPassengerType().name()); + asnTraveler.setPassengerType(passengerType); + } catch (Exception e){ + //no value found + } + + + asnTraveler.setPassengerWithReducedMobility(traveler.isPassengerWithReducedMobility()); + if (traveler.isTicketHolder()){ + asnTraveler.setTicketHolder(true); + } else { + asnTraveler.setTicketHolder(false); + } + + if (traveler.getStatusCollection()!= null && !traveler.getStatusCollection().isEmpty()){ + + SequenceOfCustomerStatusType asnList = new SequenceOfCustomerStatusType(); + + for (ICustomerStatusDescription status : traveler.getStatusCollection() ) { + asnList.add(mapCustomerStatusType(status)); + } + if (!asnList.isEmpty()) { + asnTraveler.setStatus(asnList); + } + } + + + return asnTraveler; + } + + /** + * Map customer status type. + * + * @param status the status + * @return the customer status type + * @throws EncodingFormatException the encoding format exception + */ + private CustomerStatusType mapCustomerStatusType(ICustomerStatusDescription status) throws EncodingFormatException { + + CustomerStatusType asnStatus = new CustomerStatusType(); + + if (status.getStatus() > 0) { + asnStatus.setCustomerStatus(new Long(status.getStatus())); + } + asnStatus.setCustomerStatusDescr(status.getDescription()); + asnStatus.setStatusProviderIA5(UicEncoderUtils.getIA5NonNum(status.getStatusProvider())); + asnStatus.setStatusProviderNum(UicEncoderUtils.getNum(status.getStatusProvider())); + + return asnStatus; + } + + /** + * Encode extension. + * + * @param extension the extension + * @return the extension data + * @throws EncodingFormatException the encoding format exception + */ + private ExtensionData encodeExtension(IExtension extension) throws EncodingFormatException { + + if (extension==null) return null; + + if (extension.getBinarydata() == null || extension.getBinarydata().length == 0) { + throw new EncodingFormatException("Extension does not include data"); + } + + if (extension.getId() == null || extension.getId().length() == 0) { + throw new EncodingFormatException("Extension does not include id"); + } + + ExtensionData asnExtension = new ExtensionData(); + + asnExtension.setExtensionData(extension.getBinarydata()); + asnExtension.setExtensionId(UicEncoderUtils.getIA5(extension.getId())); + + return asnExtension; + } + + +} diff --git a/src/org/uic/ticket/api/utils/OpenAsn2ApiDecoder.java b/src/org/uic/ticket/api/utils/OpenAsn2ApiDecoder.java new file mode 100644 index 0000000..4a5848d --- /dev/null +++ b/src/org/uic/ticket/api/utils/OpenAsn2ApiDecoder.java @@ -0,0 +1,2188 @@ +/* + * + */ +package org.uic.ticket.api.utils; + +import java.io.IOException; +import java.util.Date; +import java.util.List; + +import net.gcdc.asn1.datatypes.Asn1BigInteger; + +import org.uic.ticket.api.asn.omv1.BerthDetailData; +import org.uic.ticket.api.asn.omv1.CarCarriageReservationData; +import org.uic.ticket.api.asn.omv1.CardReferenceType; +import org.uic.ticket.api.asn.omv1.CompartmentDetailsType; +import org.uic.ticket.api.asn.omv1.ControlData; +import org.uic.ticket.api.asn.omv1.CountermarkData; +import org.uic.ticket.api.asn.omv1.CustomerCardData; +import org.uic.ticket.api.asn.omv1.CustomerStatusType; +import org.uic.ticket.api.asn.omv1.DelayConfirmation; +import org.uic.ticket.api.asn.omv1.DeltaCoordinates; +import org.uic.ticket.api.asn.omv1.DocumentData; +import org.uic.ticket.api.asn.omv1.ExtensionData; +import org.uic.ticket.api.asn.omv1.FIPTicketData; +import org.uic.ticket.api.asn.omv1.GeoCoordinateType; +import org.uic.ticket.api.asn.omv1.IncludedOpenTicketType; +import org.uic.ticket.api.asn.omv1.IssuingData; +import org.uic.ticket.api.asn.omv1.LineType; +import org.uic.ticket.api.asn.omv1.LuggageRestrictionType; +import org.uic.ticket.api.asn.omv1.OpenTicketData; +import org.uic.ticket.api.asn.omv1.ParkingGroundData; +import org.uic.ticket.api.asn.omv1.PassData; +import org.uic.ticket.api.asn.omv1.PlacesType; +import org.uic.ticket.api.asn.omv1.PolygoneType; +import org.uic.ticket.api.asn.omv1.RegionalValidityType; +import org.uic.ticket.api.asn.omv1.RegisteredLuggageType; +import org.uic.ticket.api.asn.omv1.ReservationData; +import org.uic.ticket.api.asn.omv1.ReturnRouteDescriptionType; +import org.uic.ticket.api.asn.omv1.RouteSectionType; +import org.uic.ticket.api.asn.omv1.SeriesDetailType; +import org.uic.ticket.api.asn.omv1.StationPassageData; +import org.uic.ticket.api.asn.omv1.TariffType; +import org.uic.ticket.api.asn.omv1.TicketLinkType; +import org.uic.ticket.api.asn.omv1.TimeRangeType; +import org.uic.ticket.api.asn.omv1.TokenType; +import org.uic.ticket.api.asn.omv1.TrainLinkType; +import org.uic.ticket.api.asn.omv1.TravelClassType; +import org.uic.ticket.api.asn.omv1.TravelerData; +import org.uic.ticket.api.asn.omv1.TravelerType; +import org.uic.ticket.api.asn.omv1.UicRailTicketData; +import org.uic.ticket.api.asn.omv1.ValidityPeriodDetailType; +import org.uic.ticket.api.asn.omv1.ValidityPeriodType; +import org.uic.ticket.api.asn.omv1.VatDetailType; +import org.uic.ticket.api.asn.omv1.ViaStationType; +import org.uic.ticket.api.asn.omv1.VoucherData; +import org.uic.ticket.api.asn.omv1.ZoneType; +import org.uic.ticket.api.impl.SimpleUicTicketObjectFactory; +import org.uic.ticket.api.spec.IBerth; +import org.uic.ticket.api.spec.ICarCarriageReservation; +import org.uic.ticket.api.spec.ICardReference; +import org.uic.ticket.api.spec.ICompartmentDetails; +import org.uic.ticket.api.spec.IControlDetail; +import org.uic.ticket.api.spec.ICounterMark; +import org.uic.ticket.api.spec.ICustomerCard; +import org.uic.ticket.api.spec.ICustomerStatusDescription; +import org.uic.ticket.api.spec.IDelayConfirmation; +import org.uic.ticket.api.spec.IDocumentExtension; +import org.uic.ticket.api.spec.IExtension; +import org.uic.ticket.api.spec.IFipTicket; +import org.uic.ticket.api.spec.IGeoCoordinate; +import org.uic.ticket.api.spec.IIncludedOpenTicket; +import org.uic.ticket.api.spec.IIssuingDetail; +import org.uic.ticket.api.spec.ILine; +import org.uic.ticket.api.spec.ILuggageRestriction; +import org.uic.ticket.api.spec.IOpenTicket; +import org.uic.ticket.api.spec.IParkingGround; +import org.uic.ticket.api.spec.IPass; +import org.uic.ticket.api.spec.IPlaces; +import org.uic.ticket.api.spec.IPolygone; +import org.uic.ticket.api.spec.IRegionalValidity; +import org.uic.ticket.api.spec.IRegisteredLuggage; +import org.uic.ticket.api.spec.IReservation; +import org.uic.ticket.api.spec.IReturnRouteDescription; +import org.uic.ticket.api.spec.IRouteSection; +import org.uic.ticket.api.spec.ISeriesDataDetails; +import org.uic.ticket.api.spec.IServiceBrand; +import org.uic.ticket.api.spec.IStationPassage; +import org.uic.ticket.api.spec.ITariff; +import org.uic.ticket.api.spec.ITicketLink; +import org.uic.ticket.api.spec.ITimeRange; +import org.uic.ticket.api.spec.IToken; +import org.uic.ticket.api.spec.ITrainLink; +import org.uic.ticket.api.spec.ITraveler; +import org.uic.ticket.api.spec.ITravelerDetail; +import org.uic.ticket.api.spec.IUicRailTicket; +import org.uic.ticket.api.spec.IUicTicketObjectFactory; +import org.uic.ticket.api.spec.IValidityDetails; +import org.uic.ticket.api.spec.IValidityRange; +import org.uic.ticket.api.spec.IVatDetail; +import org.uic.ticket.api.spec.IViaStation; +import org.uic.ticket.api.spec.IVoucher; +import org.uic.ticket.api.spec.IZone; + + +/** + * The Class OpenAsn2ApiDecoder. + */ +public class OpenAsn2ApiDecoder { + + IUicTicketObjectFactory factory = SimpleUicTicketObjectFactory.getInstance(); + + /** + * Decode from asn.1 unaligned PER encoded data. + * + * @param asnUicRailTicketData the asn uic rail ticket data + * @return the decoded uic rail ticket + * @throws IOException Signals that an I/O exception has occurred. + */ + public IUicRailTicket decodeFromAsn (UicRailTicketData asnUicRailTicketData) throws IOException{ + + IUicRailTicket uicRailTicket = factory.createUicRailTicket(); + + populateFromAsn1Model(uicRailTicket, asnUicRailTicketData); + + return uicRailTicket; + + } + + + + + + + + /** + * Populate from asn1 model. + * + * @param uicRailTicket the uic rail ticket + * @param asnUicRailTicketData the asn uic rail ticket data + */ + protected void populateFromAsn1Model(IUicRailTicket uicRailTicket, UicRailTicketData asnUicRailTicketData) { + + if (asnUicRailTicketData.getExtension()!= null && !asnUicRailTicketData.getExtension().isEmpty()) { + for (ExtensionData asnExtension : asnUicRailTicketData.getExtension()){ + uicRailTicket.addExtension(convertExtension(asnExtension)); + } + } + + if (asnUicRailTicketData.getIssuingDetail() != null) { + populateIssuingDetail(asnUicRailTicketData.getIssuingDetail(), uicRailTicket.getIssuerDetails()); + } + + if (asnUicRailTicketData.getControlDetail() != null) { + populateControlDetails(asnUicRailTicketData.getControlDetail(),uicRailTicket.getControlDetails()); + } + + if (asnUicRailTicketData.getTravelerDetail() != null) { + populateTravelerDetails(asnUicRailTicketData.getTravelerDetail(),uicRailTicket.getTravelerDetails()); + } + + if (asnUicRailTicketData.getTransportDocument() != null && !asnUicRailTicketData.getTransportDocument().isEmpty()) { + populateTravelDocuments(asnUicRailTicketData.getTransportDocument(),uicRailTicket,uicRailTicket.getIssuerDetails().getIssuingDate() ); + } + + + } + + + + + + + /** + * Populate travel documents. + * + * @param asnTransportDocuments the asn transport documents + * @param uicRailTicket the uic rail ticket + * @param issuingDate the issuing date + */ + protected void populateTravelDocuments(List asnTransportDocuments,IUicRailTicket uicRailTicket, Date issuingDate) { + + for ( DocumentData asnDocument : asnTransportDocuments){ + + if (asnDocument.getTicket() == null) { + break; + } + + if (asnDocument.getTicket().getExtension() != null) { + IDocumentExtension ticket = convertDocumentExtension(asnDocument.getTicket().getExtension()); + if (asnDocument.getToken() != null) { + IToken token = convertToken(asnDocument.getToken()); + ticket.setToken(token); + } + uicRailTicket.addDocumentExtension(ticket); + } + + if (asnDocument.getTicket().getCarCarriageReservation() != null) { + ICarCarriageReservation ticket = convertCarCarriage(asnDocument.getTicket().getCarCarriageReservation(),issuingDate); + if (asnDocument.getToken() != null) { + IToken token = convertToken(asnDocument.getToken()); + ticket.setToken(token); + } + uicRailTicket.addCarCarriageReservation(ticket); + } + + if (asnDocument.getTicket().getCounterMark() != null) { + ICounterMark ticket = convertCountermark(asnDocument.getTicket().getCounterMark(),issuingDate); + if (asnDocument.getToken() != null) { + IToken token = convertToken(asnDocument.getToken()); + ticket.setToken(token); + } + uicRailTicket.addCounterMark(ticket); + } + + if (asnDocument.getTicket().getCustomerCard()!= null) { + ICustomerCard ticket = convertCustomerCard(asnDocument.getTicket().getCustomerCard(),issuingDate); + if (asnDocument.getToken() != null) { + IToken token = convertToken(asnDocument.getToken()); + ticket.setToken(token); + } + uicRailTicket.addCustomerCard(ticket); + } + + if (asnDocument.getTicket().getFipTicket()!= null) { + IFipTicket ticket = convertFipTicket(asnDocument.getTicket().getFipTicket(),issuingDate); + if (asnDocument.getToken() != null) { + IToken token = convertToken(asnDocument.getToken()); + ticket.setToken(token); + } + uicRailTicket.addFipTicket(ticket); + } + + if (asnDocument.getTicket().getOpenTicket()!= null) { + IOpenTicket ticket = convertOpenTicket(asnDocument.getTicket().getOpenTicket(),issuingDate); + if (asnDocument.getToken() != null) { + IToken token = convertToken(asnDocument.getToken()); + ticket.setToken(token); + } + uicRailTicket.addOpenTicket(ticket); + } + + + if (asnDocument.getTicket().getParkingGround()!= null) { + IParkingGround ticket = convertParkingGround(asnDocument.getTicket().getParkingGround(),issuingDate); + if (asnDocument.getToken() != null) { + IToken token = convertToken(asnDocument.getToken()); + ticket.setToken(token); + } + uicRailTicket.addParkingGround(ticket); + } + + if (asnDocument.getTicket().getPass() != null) { + IPass ticket = convertPass(asnDocument.getTicket().getPass(),issuingDate); + if (asnDocument.getToken() != null) { + IToken token = convertToken(asnDocument.getToken()); + ticket.setToken(token); + } + uicRailTicket.addPass(ticket); + } + + if (asnDocument.getTicket().getStationPassage() != null) { + IStationPassage ticket = convertStationPassage(asnDocument.getTicket().getStationPassage(),issuingDate); + if (asnDocument.getToken() != null) { + IToken token = convertToken(asnDocument.getToken()); + ticket.setToken(token); + } + uicRailTicket.addStationPassage(ticket); + } + + if (asnDocument.getTicket().getReservation() != null) { + IReservation ticket = convertReservation(asnDocument.getTicket().getReservation(),issuingDate); + if (asnDocument.getToken() != null) { + IToken token = convertToken(asnDocument.getToken()); + ticket.setToken(token); + } + uicRailTicket.addReservation(ticket); + } + + if (asnDocument.getTicket().getVoucher()!= null) { + IVoucher ticket = convertVoucher(asnDocument.getTicket().getVoucher(),issuingDate); + if (asnDocument.getToken() != null) { + IToken token = convertToken(asnDocument.getToken()); + ticket.setToken(token); + } + uicRailTicket.addVoucher(ticket); + } + + if (asnDocument.getTicket().getDelayConfirmation()!= null) { + IDelayConfirmation ticket = convertDelayConfirmation(asnDocument.getTicket().getDelayConfirmation(),issuingDate); + if (asnDocument.getToken() != null) { + IToken token = convertToken(asnDocument.getToken()); + ticket.setToken(token); + } + uicRailTicket.addDelayConfirmation(ticket); + } + } + } + + + + + + + + + + + private IToken convertToken(TokenType asnToken) { + + if (asnToken == null) return null; + + IToken token = factory.createToken(); + token.setToken(asnToken.getToken()); + token.setTokenProvider(UicEncoderUtils.mapToString(asnToken.getTokenProviderNum(), asnToken.getTokenProviderIA5())); + token.setTokenSpecification(asnToken.getTokenSpecification()); + return token; + } + + + + private IDelayConfirmation convertDelayConfirmation(DelayConfirmation asnDocument, Date issuingDate) { + + IDelayConfirmation document = factory.createDelayConfirmation(); + document.setReference(UicEncoderUtils.mapToString(asnDocument.getReferenceNum().longValue(),asnDocument.getReferenceIA5())); + + + document.setExtension(convertExtension(asnDocument.getExtension())); + document.setInfoText(asnDocument.getInfoText()); + + if (asnDocument.getStationCodeTable()!=null){ + document.setStationCodeTable(asnDocument.getStationCodeTable()); + } + document.setStation(UicEncoderUtils.mapToString(asnDocument.getStationNum(),asnDocument.getStationIA5())); + + document.setStationName(asnDocument.getStationNameUTF8()); + + document.setArrivalDate(asnDocument.getPlannedArrivalDate()); + document.setArrivalUTCoffset(asnDocument.getDepartureUTCOffset()); + + if (asnDocument.getConfirmationType() != null) { + document.setConfirmationType(asnDocument.getConfirmationType().ordinal()); + } + + if (asnDocument.getDelay() != null) { + document.setDelay(asnDocument.getDelay().intValue()); + } + + if (asnDocument.getTrainCancelled() != null) { + document.setTrainCancelled(asnDocument.getTrainCancelled()); + } else { + document.setTrainCancelled(false); + } + + if (asnDocument.getAffectedTickets() !=null && !asnDocument.getAffectedTickets().isEmpty()) { + for (TicketLinkType asnTicketLink : asnDocument.getAffectedTickets()) { + document.addLinkedTicket(convertTicketLink(asnTicketLink)); + } + } + + return document; + } + + + + + + + /** + * Convert reservation. + * + * @param asnDocument the asn document + * @param issuingDate the issuing date + * @return the i reservation + */ + protected IReservation convertReservation(ReservationData asnDocument, Date issuingDate) { + + IReservation document = factory.createReservation(); + document.setReference(UicEncoderUtils.mapToString(asnDocument.getReferenceNum(),asnDocument.getReferenceIA5())); + + document.setProductId(UicEncoderUtils.mapToString(asnDocument.getProductIdNum(),asnDocument.getProductIdIA5())); + document.setProductOwner(UicEncoderUtils.mapToString(asnDocument.getProductOwnerNum(),asnDocument.getProductOwnerIA5())); + + document.setExtension(convertExtension(asnDocument.getExtension())); + document.setInfoText(asnDocument.getInfoText()); + + if (asnDocument.getStationCodeTable()!=null){ + document.setStationCodeTable(asnDocument.getStationCodeTable()); + } + document.setFromStation(UicEncoderUtils.mapToString(asnDocument.getFromStationNum(),asnDocument.getFromStationIA5())); + document.setToStation(UicEncoderUtils.mapToString(asnDocument.getToStationNum(),asnDocument.getToStationIA5())); + document.setFromStationName(asnDocument.getFromStationNameUTF8()); + document.setToStationName(asnDocument.getToStationNameUTF8()); + + document.setDepartureDate(asnDocument.getDepartureDate(issuingDate)); + document.setArrivalDate(asnDocument.getArrivalDate(issuingDate)); + + document.setDepartureUTCoffset(asnDocument.getDepartureUTCOffset()); + if (asnDocument.getArrivalUTCOffset() != null) { + document.setArrivalUTCoffset(asnDocument.getArrivalUTCOffset()); + } else { + document.setArrivalUTCoffset(asnDocument.getDepartureUTCOffset()); + } + + if(asnDocument.getClassCode()!=null){ + document.setClassCode(asnDocument.getClassCode()); + } + + document.setCompartmentDetails(convertCompartmentDetails(asnDocument.getCompartmentDetails())); + + document.setLuggageRestriction(convertLuggageRestriction(asnDocument.getLuggage())); + + if (asnDocument.getNumberOfOverbooked() != null) { + document.setNumberOfOverbooked(asnDocument.getNumberOfOverbooked().intValue()); + } + + if (asnDocument.getNumberOfSupplements() != null) { + document.setNumberOfSupplements(asnDocument.getNumberOfSupplements().intValue()); + } + + if (asnDocument.getPlaces()!=null) { + document.setPlaces(convertPlaces(asnDocument.getPlaces())); + } + + if (asnDocument.getBicyclePlaces()!=null) { + document.setBicyclePlaces(convertPlaces(asnDocument.getBicyclePlaces())); + } + + document.setPriceType(asnDocument.getPriceType()); + + IServiceBrand serviceBrand = factory.createServiceBrand(); + + if (asnDocument.getServiceBrand()!=null){ + serviceBrand.setServiceBrand(asnDocument.getServiceBrand().intValue()); + } + serviceBrand.setServiceBrandAbbreviation(asnDocument.getServiceBrandAbrUTF8()); + serviceBrand.setServiceBrandDescription(asnDocument.getServiceBrandNameUTF8()); + + document.setServiceBrand(serviceBrand); + + if (asnDocument.getServiceLevel()!=null && asnDocument.getServiceLevel().length() != 0) { + document.setServiceLevel(asnDocument.getServiceLevel()); + } + + document.setTrain(UicEncoderUtils.mapToString(asnDocument.getTrainNum(), asnDocument.getTrainIA5())); + + if (asnDocument.getTypeOfSupplement()!=null) { + document.setTypeOfSupplement(asnDocument.getTypeOfSupplement().intValue()); + } + + if (asnDocument.getBerth()!=null && !asnDocument.getBerth().isEmpty()){ + for (BerthDetailData asnBerth : asnDocument.getBerth()){ + document.addBerth(convertBerth(asnBerth)); + } + } + + if (asnDocument.getCarrierNum()!=null && !asnDocument.getCarrierNum().isEmpty()){ + for(Long carrier :asnDocument.getCarrierNum()){ + document.addCarrier(carrier.toString()); + } + } + if (asnDocument.getCarrierIA5()!=null && !asnDocument.getCarrierIA5().isEmpty()){ + for(String carrier :asnDocument.getCarrierIA5()){ + document.addCarrier(carrier); + } + } + + if (asnDocument.getTariff()!=null && !asnDocument.getTariff().isEmpty()){ + for (TariffType asnTariff : asnDocument.getTariff()) { + document.addTariff(convertTariff(asnTariff)); + } + } + + document.setPrice(asnDocument.getPrice()); + + if (asnDocument.getVatDetails() != null && !asnDocument.getVatDetails().isEmpty()){ + for (VatDetailType vat : asnDocument.getVatDetails()) { + document.addVatDetail(decodeVatDetail(vat)); + } + } + + return document; + } + + + + + + + + + + /** + * Convert car carriage. + * + * @param asnDocument the asn document + * @param issuingDate the issuing date + * @return the i car carriage reservation + */ + protected ICarCarriageReservation convertCarCarriage(CarCarriageReservationData asnDocument , Date issuingDate) { + + ICarCarriageReservation document = factory.createCarCarriageReservation(); + document.setReference(UicEncoderUtils.mapToString(asnDocument.getReferenceNum(),asnDocument.getReferenceIA5())); + + document.setProductId(UicEncoderUtils.mapToString(asnDocument.getProductIdNum(),asnDocument.getProductIdIA5())); + document.setProductOwner(UicEncoderUtils.mapToString(asnDocument.getProductOwnerNum(),asnDocument.getProductOwnerIA5())); + + document.setExtension(convertExtension(asnDocument.getExtension())); + document.setInfoText(asnDocument.getInfoText()); + + if (asnDocument.getStationCodeTable()!=null){ + document.setStationCodeTable(asnDocument.getStationCodeTable()); + } + document.setFromStation(UicEncoderUtils.mapToString(asnDocument.getFromStationNum(),asnDocument.getFromStationIA5())); + document.setToStation(UicEncoderUtils.mapToString(asnDocument.getToStationNum(),asnDocument.getToStationIA5())); + document.setFromStationName(asnDocument.getFromStationNameUTF8()); + document.setToStationName(asnDocument.getToStationNameUTF8()); + + document.setBeginLoading(asnDocument.getBeginLoadingDate(issuingDate)); + document.setEndLoading(asnDocument.getEndLoadingDate(issuingDate)); + + document.setLoadingTimeUTCoffset(asnDocument.getLoadingUTCOffset()); + + + if(asnDocument.getAttachedBicycles()!=null){ + document.setAttachedBicycles(asnDocument.getAttachedBicycles().intValue()); + } + + if (asnDocument.getAttachedBoats()!=null) { + document.setAttachedBoats(asnDocument.getAttachedBoats().intValue()); + } + + if(asnDocument.getAttachedSurfboards()!=null) { + document.setAttachedSurfboards(asnDocument.getAttachedSurfboards().intValue()); + } + + if (asnDocument.getBoatCategory()!=null){ + document.setBoatCategory(asnDocument.getBoatCategory().intValue()); + } + + if(asnDocument.getCarCategory()!=null){ + document.setCarCategory(asnDocument.getCarCategory().intValue()); + } + + if (asnDocument.getLoadingDeck()!=null){ + document.setLoadingDeck(asnDocument.getLoadingDeck()); + } + + if(asnDocument.getLoadingListEntry()!=null){ + document.setLoadingListEntry(asnDocument.getLoadingListEntry().intValue()); + } + + if (asnDocument.getRoofRackHeight()!=null){ + document.setRoofRackHeight(asnDocument.getRoofRackHeight().intValue()); + } + + document.setNumberPlate(asnDocument.getNumberPlate()); + document.setTrailerPlate(asnDocument.getTrailerPlate()); + + if(asnDocument.getRoofRackType()!=null){ + document.setRoofRackType(asnDocument.getRoofRackType()); + } + + if(asnDocument.getTextileRoof()) { + document.setTextileRoof(asnDocument.getTextileRoof()); + } + + + document.setCompartmentDetails(convertCompartmentDetails(asnDocument.getCompartmentDetails())); + + if (asnDocument.getCarrierNum()!=null && !asnDocument.getCarrierNum().isEmpty()){ + for(Long carrier :asnDocument.getCarrierNum()){ + document.addCarrier(carrier.toString()); + } + } + if (asnDocument.getCarrierIA5()!=null && !asnDocument.getCarrierIA5().isEmpty()){ + for(String carrier :asnDocument.getCarrierIA5()){ + document.addCarrier(carrier); + } + } + + if (asnDocument.getCoach()!=null) { + document.setCoach(asnDocument.getCoach()); + } + if (asnDocument.getPlace()!=null) { + document.setPlace(asnDocument.getPlace()); + } + + + document.setPriceType(asnDocument.getPriceType()); + + IServiceBrand serviceBrand = factory.createServiceBrand(); + + if (asnDocument.getServiceBrand()!=null){ + serviceBrand.setServiceBrand(asnDocument.getServiceBrand().intValue()); + } + serviceBrand.setServiceBrandAbbreviation(asnDocument.getServiceBrandAbrUTF8()); + serviceBrand.setServiceBrandDescription(asnDocument.getServiceBrandNameUTF8()); + + document.setServiceBrand(serviceBrand); + + document.setTrain(UicEncoderUtils.mapToString(asnDocument.getTrainNum(), asnDocument.getTrainIA5())); + + + if (asnDocument.getTariff()!=null){ + document.setTariff(convertTariff(asnDocument.getTariff())); + } + + document.setPrice(asnDocument.getPrice()); + + if (asnDocument.getVatDetails() != null && !asnDocument.getVatDetails().isEmpty()){ + for (VatDetailType vat : asnDocument.getVatDetails()) { + document.addVatDetail(decodeVatDetail(vat)); + } + } + + return null; + } + + + private IVatDetail decodeVatDetail(VatDetailType asnVat) { + + IVatDetail vat = factory.createVatDetail(); + + vat.setAmount(asnVat.getAmount()); + if (asnVat.getPercentage() != null){ + vat.setPercentage(asnVat.getPercentage().intValue()); + } + if (asnVat.getCountry() != null) { + vat.setCountry(asnVat.getCountry().intValue()); + } + vat.setVatId(asnVat.getVatId()); + return vat; + } + + + + + + + + /** + * Convert compartment details. + * + * @param asnDetails the asn details + * @return the i compartment details + */ + protected ICompartmentDetails convertCompartmentDetails(CompartmentDetailsType asnDetails) { + + if (asnDetails == null) return null; + + ICompartmentDetails details = factory.createCompartmentDetails(); + + if (asnDetails.getCompartmentType()!=null) { + details.setCompartmentType(asnDetails.getCompartmentType().intValue()); + } + + if (asnDetails.getCoachType()!=null){ + details.setCoachType(asnDetails.getCoachType().intValue()); + } + + if (asnDetails.getSpecialAllocation()!=null) { + details.setSpecialAllocation(asnDetails.getSpecialAllocation().intValue()); + } + + details.setCoachTypeDescr(asnDetails.getCoachTypeDescr()); + details.setCompartmentTypeDescr(asnDetails.getCompartmentTypeDescr()); + details.setSpecialAllocationDescr(asnDetails.getSpecialAllocationDescr()); + + details.setPosition(asnDetails.getPosition()); + + return details; + } + + /** + * Convert places. + * + * @param asnPlaces the asn places + * @return the i places + */ + protected IPlaces convertPlaces(PlacesType asnPlaces) { + + if (asnPlaces == null) return null; + + IPlaces places = factory.createPlaces(); + + places.setCoach(asnPlaces.getCoach()); + places.setPlaceDescription(asnPlaces.getPlaceDescription()); + + if (asnPlaces.getPlaceNum()!=null && !asnPlaces.getPlaceNum().isEmpty()){ + for (Long place: asnPlaces.getPlaceNum()){ + places.addPlace(place.toString()); + } + } + + if (asnPlaces.getPlaceIA5()!=null && !asnPlaces.getPlaceIA5().isEmpty()){ + for (String place: asnPlaces.getPlaceIA5()){ + places.addPlace(place); + } + } + + places.setPlaceString(asnPlaces.getPlaceString()); + + return places; + } + + /** + * Convert berth. + * + * @param asnBerth the asn berth + * @return the i berth + */ + protected IBerth convertBerth(BerthDetailData asnBerth) { + + if (asnBerth == null) return null; + + IBerth berth = factory.createBerth(); + if (asnBerth.getGender()!= null) { + berth.setGender(asnBerth.getGender()); + } + if (asnBerth.getNumberOfBerths()!=null) { + berth.setNumberOfBerths(asnBerth.getNumberOfBerths().intValue()); + } + if (asnBerth.getBerthType()!=null){ + berth.setType(asnBerth.getBerthType()); + } + + return berth; + } + + /** + * Convert countermark. + * + * @param asnDocument the asn document + * @param issuingDate the issuing date + * @return the i counter mark + */ + protected ICounterMark convertCountermark(CountermarkData asnDocument , Date issuingDate) { + + ICounterMark document = factory.createCounterMark(); + + document.setProductId(UicEncoderUtils.mapToString(asnDocument.getProductIdNum(),asnDocument.getProductIdIA5())); + document.setProductOwner(UicEncoderUtils.mapToString(asnDocument.getProductOwnerNum(),asnDocument.getProductOwnerIA5())); + + if(asnDocument.getClassCode()!=null){ + document.setClassCode(asnDocument.getClassCode()); + } + + document.setValidFrom(asnDocument.getValidFromDate(issuingDate)); + document.setValidUntil(asnDocument.getValidUntilDate(issuingDate)); + + document.setValidFromUTCoffset(asnDocument.getValidFromUTCOffset()); + if (asnDocument.getValidUntilUTCOffset() != null) { + document.setValidUntilUTCoffset(asnDocument.getValidUntilUTCOffset()); + } else { + document.setValidUntilUTCoffset(asnDocument.getValidFromUTCOffset()); + } + + + document.setExtension(convertExtension(asnDocument.getExtension())); + document.setInfoText(asnDocument.getInfoText()); + + document.setReference(UicEncoderUtils.mapToString(asnDocument.getReferenceNum(),asnDocument.getReferenceIA5())); + document.setTicketReference(UicEncoderUtils.mapToString(asnDocument.getTicketReferenceNum(),asnDocument.getTicketReferenceIA5())); + + document.setFromStation(UicEncoderUtils.mapToString(asnDocument.getFromStationNum(),asnDocument.getFromStationIA5())); + document.setToStation(UicEncoderUtils.mapToString(asnDocument.getToStationNum(),asnDocument.getToStationIA5())); + + document.setFromStationName(asnDocument.getFromStationNameUTF8()); + document.setToStationName(asnDocument.getToStationNameUTF8()); + document.setValidRegionDesc(asnDocument.getValidRegionDesc()); + + if (asnDocument.getValidRegion()!= null && !asnDocument.getValidRegion().isEmpty()) { + for (RegionalValidityType validRegion :asnDocument.getValidRegion()){ + document.addValidRegionList(convertValidRegion(validRegion, issuingDate)); + } + } + + document.setReturnDescription(convertReturnDescription(asnDocument.getReturnDescription(), issuingDate)); + + + if(asnDocument.getReturnIncluded()!=null){ + document.setReturnIncluded(asnDocument.getReturnIncluded()); + } else { + document.setReturnIncluded(false); + } + + + + document.setGroupName(asnDocument.getGroupName()); + + if (asnDocument.getNumberOfCountermark() != null) { + document.setNumberOfCountermark(asnDocument.getNumberOfCountermark().intValue()); + } + + if (asnDocument.getTotalOfCountermarks() != null) { + document.setTotalOfCountermarks(asnDocument.getTotalOfCountermarks().intValue()); + } + + return document; + + } + + /** + * Convert return description. + * + * @param asnReturnRoute the asn return route + * @param issuingDate the issuing date + * @return the i return route description + */ + protected IReturnRouteDescription convertReturnDescription(ReturnRouteDescriptionType asnReturnRoute, Date issuingDate) { + + if (asnReturnRoute == null) return null; + + IReturnRouteDescription route = factory.createReturnRouteDescription(); + + route.setFromStation(UicEncoderUtils.mapToString(asnReturnRoute.getFromStationNum(),asnReturnRoute.getFromStationIA5())); + route.setToStation(UicEncoderUtils.mapToString(asnReturnRoute.getToStationNum(),asnReturnRoute.getToStationIA5())); + + route.setFromStationName(asnReturnRoute.getFromStationNameUTF8()); + route.setToStationName(asnReturnRoute.getToStationNameUTF8()); + route.setValidRegionDesc(asnReturnRoute.getValidReturnRegionDesc()); + + if (asnReturnRoute.getValidReturnRegion()!= null && !asnReturnRoute.getValidReturnRegion().isEmpty()) { + for (RegionalValidityType validRegion :asnReturnRoute.getValidReturnRegion()){ + route.addValidRegionList(convertValidRegion(validRegion, issuingDate)); + } + } + + return route; + } + + /** + * Convert luggage restriction. + * + * @param asnLuggage the asn luggage + * @return the i luggage restriction + */ + protected ILuggageRestriction convertLuggageRestriction(LuggageRestrictionType asnLuggage) { + + if (asnLuggage == null) return null; + + ILuggageRestriction luggage = factory.createLuggageRestriction(); + if (asnLuggage.getMaxHandLuggagePieces()!=null){ + luggage.setMaxHandLuggagePieces(asnLuggage.getMaxHandLuggagePieces().intValue()); + } + if (asnLuggage.getMaxNonHandLuggagePieces()!=null){ + luggage.setMaxNonHandLuggagePieces(asnLuggage.getMaxNonHandLuggagePieces().intValue()); + } + + + if (asnLuggage.getRegisteredLuggage()!= null &&!asnLuggage.getRegisteredLuggage().isEmpty()){ + for (RegisteredLuggageType rl: asnLuggage.getRegisteredLuggage()){ + luggage.addRegisteredLuggage(convertRegisteredLuggage(rl)); + } + } + return luggage; + } + + /** + * Convert registered luggage. + * + * @param rl the rl + * @return the i registered luggage + */ + protected IRegisteredLuggage convertRegisteredLuggage(RegisteredLuggageType rl) { + + if (rl == null) return null; + + IRegisteredLuggage luggage= factory.createRegisteredLuggage(); + if (rl.getMaxSize()!=null){ + if (rl.getMaxSize()!=null) { + luggage.setMaxSize(rl.getMaxSize().intValue()); + } + } + if (rl.getMaxWeight()!=null){ + if(rl.getMaxWeight()!=null){ + luggage.setMaxWeight(rl.getMaxWeight().intValue()); + } + } + luggage.setRegistrationId(rl.getRegistrationId()); + return luggage; + } + + + /** + * Convert open ticket. + * + * @param asnDocument the asn document + * @param issuingDate the issuing date + * @return the i open ticket + */ + protected IOpenTicket convertOpenTicket(OpenTicketData asnDocument , Date issuingDate) { + + IOpenTicket document = factory.createOpenTicket(); + + if(asnDocument.getClassCode()!=null){ + document.setClassCode(asnDocument.getClassCode()); + } + + document.setValidFrom(asnDocument.getValidFromDate(issuingDate)); + document.setValidUntil(asnDocument.getValidUntilDate(issuingDate)); + + document.setValidFromUTCoffset(asnDocument.getValidFromUTCOffset()); + if (asnDocument.getValidUntilUTCOffset() != null) { + document.setValidUntilUTCoffset(asnDocument.getValidUntilUTCOffset()); + } else { + document.setValidUntilUTCoffset(asnDocument.getValidFromUTCOffset()); + } + + + document.setExtension(convertExtension(asnDocument.getExtension())); + document.setInfoText(asnDocument.getInfoText()); + document.setProductId(UicEncoderUtils.mapToString(asnDocument.getProductIdNum(),asnDocument.getProductIdIA5())); + document.setProductOwner(UicEncoderUtils.mapToString(asnDocument.getProductOwnerNum(),asnDocument.getProductOwnerIA5())); + document.setReference(UicEncoderUtils.mapToString(asnDocument.getReferenceNum(),asnDocument.getReferenceIA5())); + + if (asnDocument.getExcludedServiceBrands()!=null && !asnDocument.getExcludedServiceBrands().isEmpty()){ + for(Long number :asnDocument.getExcludedServiceBrands()){ + document.addExcludedServiceBrand(number.intValue()); + } + } + + if (asnDocument.getCarriersNum()!=null && !asnDocument.getCarriersNum().isEmpty()){ + for(Long carrier :asnDocument.getCarriersNum()){ + document.addIncludedCarrier(carrier.toString()); + } + } + if (asnDocument.getCarriersIA5()!=null && !asnDocument.getCarriersIA5().isEmpty()){ + for(String carrier :asnDocument.getCarriersIA5()){ + document.addIncludedCarrier(carrier); + } + } + + + if (asnDocument.getIncludedServiceBrands()!=null && !asnDocument.getIncludedServiceBrands().isEmpty()){ + for(Long number :asnDocument.getIncludedServiceBrands()){ + document.addIncludedServiceBrand(number.intValue()); + } + } + + if (asnDocument.getTariffs()!=null && !asnDocument.getTariffs().isEmpty()){ + for(TariffType asnTariff :asnDocument.getTariffs()){ + document.addTariff(convertTariff(asnTariff)); + } + } + + if (asnDocument.getStationCodeTable()!=null){ + document.setStationCodeTable(asnDocument.getStationCodeTable()); + } + document.setFromStation(UicEncoderUtils.mapToString(asnDocument.getFromStationNum(),asnDocument.getFromStationIA5())); + document.setToStation(UicEncoderUtils.mapToString(asnDocument.getToStationNum(),asnDocument.getToStationIA5())); + document.setFromStationName(asnDocument.getFromStationNameUTF8()); + document.setToStationName(asnDocument.getToStationNameUTF8()); + document.setValidRegionDesc(asnDocument.getValidRegionDesc()); + if (asnDocument.getValidRegion()!= null && !asnDocument.getValidRegion().isEmpty()) { + for (RegionalValidityType validRegion :asnDocument.getValidRegion()){ + document.addValidRegionList(convertValidRegion(validRegion, issuingDate)); + } + } + + if (asnDocument.getIssuerAutorizationId() != null) { + document.setAuthorizationCode(asnDocument.getIssuerAutorizationId().intValue()); + } + + if (asnDocument.getExtIssuerId() != null) { + document.setExternalIssuer(asnDocument.getExtIssuerId().intValue()); + } + + document.setLuggageRestriction(convertLuggageRestriction(asnDocument.getLuggage())); + + document.setReturnDescription(convertReturnDescription(asnDocument.getReturnDescription(), issuingDate)); + if(asnDocument.getReturnIncluded()!=null){ + document.setReturnIncluded(asnDocument.getReturnIncluded()); + } + + if (asnDocument.getActivatedDay()!=null && !asnDocument.getActivatedDay().isEmpty()) { + document.getActivatedDays().addAll(asnDocument.getActivatedDays(issuingDate)); + } + + if (asnDocument.getIncludedAddOns()!= null && !asnDocument.getIncludedAddOns().isEmpty()) { + + for (IncludedOpenTicketType asnAddOn :asnDocument.getIncludedAddOns()) { + + IIncludedOpenTicket addOn = convertIncludedOpenTicket(asnAddOn, issuingDate, document.getClassCode()); + if (addOn != null) { + document.addIncludedAddOn(addOn); + } + } + + + + } + + document.setPrice(asnDocument.getPrice()); + + if (asnDocument.getVatDetails() != null && !asnDocument.getVatDetails().isEmpty()){ + for (VatDetailType vat : asnDocument.getVatDetails()) { + document.addVatDetail(decodeVatDetail(vat)); + } + } + + return document; + + } + + + + /** + * Convert included open ticket. + * + * @param asnDocument the asn document + * @param issuingDate the issuing date + * @param classCode the class code + * @return the simple included open ticket + */ + private IIncludedOpenTicket convertIncludedOpenTicket( IncludedOpenTicketType asnDocument, Date issuingDate, TravelClassType classCode) { + + IIncludedOpenTicket document = factory.createIncludedOpenTicket(); + + if(asnDocument.getClassCode()!=null){ + document.setClassCode(asnDocument.getClassCode()); + } else { + document.setClassCode(classCode); + } + + document.setValidFrom(asnDocument.getValidFromDate(issuingDate)); + document.setValidUntil(asnDocument.getValidUntilDate(issuingDate)); + + document.setValidFromUTCoffset(asnDocument.getValidFromUTCOffset()); + if (asnDocument.getValidUntilUTCOffset() != null) { + document.setValidUntilUTCoffset(asnDocument.getValidUntilUTCOffset()); + } else { + document.setValidUntilUTCoffset(asnDocument.getValidFromUTCOffset()); + } + + + document.setExtension(convertExtension(asnDocument.getExtension())); + document.setInfoText(asnDocument.getInfoText()); + document.setProductId(UicEncoderUtils.mapToString(asnDocument.getProductIdNum(),asnDocument.getProductIdIA5())); + document.setProductOwner(UicEncoderUtils.mapToString(asnDocument.getProductOwnerNum(),asnDocument.getProductOwnerIA5())); + + if (asnDocument.getExcludedServiceBrands()!=null && !asnDocument.getExcludedServiceBrands().isEmpty()){ + for(Long number :asnDocument.getExcludedServiceBrands()){ + document.addExcludedServiceBrand(number.intValue()); + } + } + if (asnDocument.getIncludedCarriersNum()!=null && !asnDocument.getIncludedCarriersNum().isEmpty()){ + for(Long carrier :asnDocument.getIncludedCarriersNum()){ + document.addIncludedCarrier(carrier.toString()); + } + } + if (asnDocument.getIncludedCarriersIA5()!=null && !asnDocument.getIncludedCarriersIA5().isEmpty()){ + for(String carrier :asnDocument.getIncludedCarriersIA5()){ + document.addIncludedCarrier(carrier); + } + } + + if (asnDocument.getIncludedServiceBrands()!=null && !asnDocument.getIncludedServiceBrands().isEmpty()){ + for(Long number :asnDocument.getIncludedServiceBrands()){ + document.addIncludedServiceBrand(number.intValue()); + } + } + + if (asnDocument.getTariffs()!=null && !asnDocument.getTariffs().isEmpty()){ + for(TariffType asnTariff :asnDocument.getTariffs()){ + document.addTariff(convertTariff(asnTariff)); + } + } + + if (asnDocument.getStationCodeTable()!=null){ + document.setStationCodeTable(asnDocument.getStationCodeTable()); + } + + if (asnDocument.getValidRegion()!= null && !asnDocument.getValidRegion().isEmpty()) { + for (RegionalValidityType validRegion :asnDocument.getValidRegion()){ + document.addValidRegionList(convertValidRegion(validRegion, issuingDate)); + } + } + + if (asnDocument.getIssuerAutorizationId()!=null){ + document.setAuthorizationCode(asnDocument.getIssuerAutorizationId().intValue()); + } + if (asnDocument.getExternalIssuerId()!=null){ + document.setExternalIssuer(asnDocument.getExternalIssuerId().intValue()); + } + return document; + } + + + + + + + + /** + * Convert station passage. + * + * @param asnDocument the asn document + * @param issuingDate the issuing date + * @return the i station passage + */ + protected IStationPassage convertStationPassage(StationPassageData asnDocument , Date issuingDate) { + + IStationPassage document = factory.createStationPassage(); + + document.setValidFrom(asnDocument.getValidFromDate(issuingDate)); + document.setValidUntil(asnDocument.getValidUntilDate(issuingDate)); + + document.setValidFromUTCoffset(asnDocument.getValidFromUTCOffset()); + if (asnDocument.getValidUntilUTCOffset() != null) { + document.setValidUntilUTCoffset(asnDocument.getValidUntilUTCOffset()); + } else { + document.setValidUntilUTCoffset(asnDocument.getValidFromUTCOffset()); + } + + document.setReference(UicEncoderUtils.mapToString(asnDocument.getReferenceNum(),asnDocument.getReferenceIA5())); + + if (asnDocument.getNumberOfDaysValid()!=null) { + document.setNumberOfdaysAllowed(asnDocument.getNumberOfDaysValid().intValue()); + } + + if (asnDocument.getStationNum()!=null && !asnDocument.getStationNum().isEmpty()){ + for (Asn1BigInteger station : asnDocument.getStationNum()) { + if (station.value() != null) { + document.addStation(station.value().toString()); + } + } + } + if (asnDocument.getStationIA5()!=null && !asnDocument.getStationIA5().isEmpty()){ + for (String station : asnDocument.getStationIA5()) { + document.addStation(station); + } + } + + document.setProductId(UicEncoderUtils.mapToString(asnDocument.getProductIdNum(),asnDocument.getProductIdIA5())); + document.setProductOwner(UicEncoderUtils.mapToString(asnDocument.getProductOwnerNum(),asnDocument.getProductOwnerIA5())); + + document.setProductName(asnDocument.getProductName()); + document.setProductOwner(UicEncoderUtils.mapToString(asnDocument.getProductOwnerNum(),asnDocument.getProductOwnerIA5())); + + if (asnDocument.getStationNameUTF8()!= null && asnDocument.getStationNameUTF8().isEmpty() ) { + for (String name : asnDocument.getStationNameUTF8()) { + document.addStation(name); + } + } + + if (asnDocument.getStationCodeTable()!=null){ + document.setStationCodeTable(asnDocument.getStationCodeTable()); + } + + document.setExtension(convertExtension(asnDocument.getExtension())); + + if (asnDocument.getAreaCodeIA5() != null && !asnDocument.getAreaCodeIA5().isEmpty()) { + for (String code : asnDocument.getAreaCodeIA5()) { + document.addAreaCode(code); + } + } + if (asnDocument.getAreaCodeNum() != null && !asnDocument.getAreaCodeNum().isEmpty()) { + for (Asn1BigInteger code : asnDocument.getAreaCodeNum()) { + if (code.value() != null) { + document.addAreaCode(code.value().toString()); + } + } + } + + if (asnDocument.getAreaNameUTF8() != null && !asnDocument.getAreaNameUTF8().isEmpty()) { + for (String code : asnDocument.getAreaNameUTF8()) { + document.addAreaName(code); + } + } + + + + return document; + } + + + /** + * Convert valid region. + * + * @param asnRegion the asn region + * @param issuingDate the issuing date + * @return the i regional validity + */ + protected IRegionalValidity convertValidRegion(RegionalValidityType asnRegion, Date issuingDate) { + + if (asnRegion == null) return null; + + if (asnRegion.getLines()!=null){ + return convertLine(asnRegion.getLines()); + } + if (asnRegion.getPolygone()!=null){ + return convertPolygone(asnRegion.getPolygone()); + } + if (asnRegion.getTrainLink()!=null){ + return convertTrainLink(asnRegion.getTrainLink(),issuingDate); + } + if (asnRegion.getViaStations()!=null){ + return convertViaStation(asnRegion.getViaStations()); + } + if (asnRegion.getZones()!=null){ + return convertZone(asnRegion.getZones()); + } + return null; + } + + + /** + * Convert parking ground. + * + * @param asnDocument the asn document + * @param issuingDate the issuing date + * @return the i parking ground + */ + protected IParkingGround convertParkingGround(ParkingGroundData asnDocument , Date issuingDate) { + + IParkingGround document = factory.createParkingGround(); + + document.setReference(UicEncoderUtils.mapToString(asnDocument.getReferenceNum(),asnDocument.getReferenceIA5())); + + document.setProductId(UicEncoderUtils.mapToString(asnDocument.getProductIdNum(),asnDocument.getProductIdIA5())); + document.setProductOwner(UicEncoderUtils.mapToString(asnDocument.getProductOwnerNum(),asnDocument.getProductOwnerIA5())); + + + document.setAccessCode(asnDocument.getAccessCode()); + document.setLocation(asnDocument.getLocation()); + + document.setExtension(convertExtension(asnDocument.getExtension())); + + document.setNumberPlate(asnDocument.getNumberPlate()); + document.setEntryTrack(asnDocument.getEntryTrack()); + + + document.setFromParkingDate(asnDocument.getFromParkingDate(issuingDate)); + document.setToParkingDate(asnDocument.getToParkingDate(issuingDate)); + + document.setParkingGroundId(asnDocument.getParkingGroundId()); + + document.setSpecialInformation(asnDocument.getSpecialInformation()); + + if (asnDocument.getStationCodeTable()!=null){ + document.setStationCodeTable(asnDocument.getStationCodeTable()); + } + + document.setStation(UicEncoderUtils.mapToString(asnDocument.getStationNum(),asnDocument.getStationIA5())); + + document.setPrice(asnDocument.getPrice()); + + if (asnDocument.getVatDetails() != null && !asnDocument.getVatDetails().isEmpty()){ + for (VatDetailType vat : asnDocument.getVatDetails()) { + document.addVatDetail(decodeVatDetail(vat)); + } + } + + return document; + } + + /** + * Convert via station. + * + * @param asnVia the asn via + * @return the i via station + */ + protected IViaStation convertViaStation(ViaStationType asnVia) { + + if (asnVia == null) return null; + + IViaStation via = factory.createViaStation(); + + if (asnVia.getBorder()!=null) { + via.setBorder(asnVia.getBorder()); + } + + if (asnVia.getRouteId() != null) { + via.setRouteId(asnVia.getRouteId().intValue()); + } + + if (asnVia.getStationCodeTable()!=null){ + via.setStationCodeTable(asnVia.getStationCodeTable()); + } + + if (asnVia.getStationNum() != null) { + via.setStation(asnVia.getStationNum().toString()); + } else if (asnVia.getStationIA5() != null) { + via.setStation(asnVia.getStationIA5()); + } + + if (asnVia.getCarriersNum()!=null && !asnVia.getCarriersNum().isEmpty()){ + for(Long carrier :asnVia.getCarriersNum()){ + via.addCarrier(carrier.toString()); + } + } + if (asnVia.getCarriersIA5()!=null && !asnVia.getCarriersIA5().isEmpty()){ + for(String carrier :asnVia.getCarriersIA5()){ + via.addCarrier(carrier); + } + } + + if (asnVia.getRoute()!= null && !asnVia.getRoute().isEmpty()) { + for ( ViaStationType routeVia: asnVia.getRoute()) { + via.addRouteStation(convertViaStation(routeVia)); + } + } + if (asnVia.getAlternativeRoutes()!= null && !asnVia.getAlternativeRoutes().isEmpty()) { + for ( ViaStationType routeVia: asnVia.getAlternativeRoutes()) { + via.addRouteStation(convertViaStation(routeVia)); + } + } + + + return via; + } + + /** + * Convert train link. + * + * @param asnTrainLink the asn train link + * @param issuingDate the issuing date + * @return the i regional validity + */ + protected IRegionalValidity convertTrainLink(TrainLinkType asnTrainLink, Date issuingDate) { + + ITrainLink trainLink = factory.createTrainLink(); + + trainLink.setTrain(UicEncoderUtils.mapToString(asnTrainLink.getTrainNum(), asnTrainLink.getTrainIA5())); + + trainLink.setFromStation(UicEncoderUtils.mapToString(asnTrainLink.getFromStationNum(),asnTrainLink.getFromStationIA5())); + trainLink.setToStation(UicEncoderUtils.mapToString(asnTrainLink.getToStationNum(),asnTrainLink.getToStationIA5())); + trainLink.setFromStationName(asnTrainLink.getFromStationName()); + trainLink.setToStationName(asnTrainLink.getToStationName()); + + trainLink.setDepartureDateTime(asnTrainLink.getDepartureDate(issuingDate)); + + return trainLink; + } + + /** + * Convert zone. + * + * @param asnZone the asn zone + * @return the i regional validity + */ + protected IRegionalValidity convertZone(ZoneType asnZone) { + + if (asnZone == null) return null; + + IZone zone = factory.createZone(); + + zone.setBinaryZoneId(asnZone.getBinaryZoneId()); + zone.setCarrier(UicEncoderUtils.mapToString(asnZone.getCarrierNum(),asnZone.getCarrierIA5())); + + if (asnZone.getCity() != null) { + zone.setCity(asnZone.getCity().intValue()); + } + if (asnZone.getStationCodeTable()!=null){ + zone.setStationCodeTable(asnZone.getStationCodeTable()); + } + zone.setEntryStation(UicEncoderUtils.mapToString(asnZone.getEntryStationNum(),asnZone.getEntryStationIA5())); + zone.setTerminatingStation(UicEncoderUtils.mapToString(asnZone.getTerminatingStationNum(),asnZone.getTerminatingStationIA5())); + + zone.setNUTScode(asnZone.getNutsCode()); + + return zone; + } + + /** + * Convert polygone. + * + * @param asnPolygone the asn polygone + * @return the i regional validity + */ + protected IRegionalValidity convertPolygone(PolygoneType asnPolygone) { + + if (asnPolygone == null) return null; + + IPolygone polygone = factory.createPolygone(); + + IGeoCoordinate firstEdge = convertGeoCoordinate(asnPolygone.getFirstEdge()); + + if (firstEdge == null) return null; + + if (asnPolygone.getEdges()!=null && !asnPolygone.getEdges().isEmpty()) { + for (DeltaCoordinates asnEdge :asnPolygone.getEdges()){ + + IGeoCoordinate edge = firstEdge.clone(); + try { + edge.addLongitude(asnEdge.getLongitude().longValue()); + edge.addLatitude(asnEdge.getLatitude().longValue()); + } catch (Exception e) { + + } + polygone.addEdge(edge); + } + } + return polygone; + } + + /** + * Convert line. + * + * @param asnLine the asn line + * @return the i regional validity + */ + protected IRegionalValidity convertLine(LineType asnLine) { + + if (asnLine == null) return null; + + ILine line = factory.createLine(); + + line.setBinaryZoneId(asnLine.getBinaryZoneId()); + line.setCarrier(UicEncoderUtils.mapToString(asnLine.getCarrierNum(),asnLine.getCarrierIA5())); + if (asnLine.getCity()!=null) { + line.setCity(asnLine.getCity().intValue()); + } + if (asnLine.getStationCodeTable()!=null){ + line.setStationCodeTable(asnLine.getStationCodeTable()); + } + line.setEntryStation(UicEncoderUtils.mapToString(asnLine.getEntryStationNum(),asnLine.getEntryStationIA5())); + line.setTerminatingStation(UicEncoderUtils.mapToString(asnLine.getTerminatingStationNum(),asnLine.getTerminatingStationIA5())); + if (asnLine.getLineId()!=null && !asnLine.getLineId().isEmpty()){ + for (Asn1BigInteger lineId : asnLine.getLineId()) { + line.addLineId(lineId.intValue()); + } + } + return line; + } + + /** + * Convert tariff. + * + * @param asnTariff the asn tariff + * @return the i tariff + */ + protected ITariff convertTariff(TariffType asnTariff) { + + if (asnTariff== null) return null; + + ITariff tariff = factory.createTariff(); + + if (asnTariff.getAgeAbove() != null) { + tariff.setAgeAbove(asnTariff.getAgeAbove().intValue()); + } + + if (asnTariff.getAgeBelow() != null) { + tariff.setAgeBelow(asnTariff.getAgeBelow().intValue()); + } + + if(asnTariff.getNumberOfPassengers()!=null){ + tariff.setNumberOfPassengers(asnTariff.getNumberOfPassengers().intValue()); + } + + if (asnTariff.getSeriesDataDetails() != null) { + tariff.setSeriesDataDetails(convertSeriesDataDetails(asnTariff.getSeriesDataDetails())); + } + + + if (asnTariff.getPassengerType()!=null) { + tariff.setPassengerType(asnTariff.getPassengerType()); + } + + + if(asnTariff.getRestrictedToCountryOfResidence()!=null){ + tariff.setRestrictedToCountryOfResidence(asnTariff.getRestrictedToCountryOfResidence()); + } + if (asnTariff.getRestrictedToRouteSection()!=null){ + tariff.setRestrictedToRouteSection(convertRouteSection(asnTariff.getRestrictedToRouteSection())); + } + + + tariff.setTariffDescription(asnTariff.getTariffDesc()); + tariff.setTariffId(UicEncoderUtils.mapToString(asnTariff.getTariffIdNum(),asnTariff.getTariffIdIA5())); + + if (asnTariff.getTraverlerid()!= null && !asnTariff.getTraverlerid().isEmpty() ) { + for (Long number : asnTariff.getTraverlerid()){ + tariff.addTravelerId(number.intValue()); + } + } + + if (asnTariff.getReductionCard()!=null && !asnTariff.getReductionCard().isEmpty()){ + for(CardReferenceType card : asnTariff.getReductionCard()){ + tariff.addReductionCard(convertCardReference(card)); + } + } + + return tariff; + } + + private ISeriesDataDetails convertSeriesDataDetails(SeriesDetailType asnDetails) { + + if (asnDetails == null) return null; + + ISeriesDataDetails details = factory.createSeriesDataDetails(); + + if (asnDetails.getSeries() != null) { + details.setSeries(asnDetails.getSeries().intValue()); + } + + if (asnDetails.getSupplyingCarrier() != null) { + details.setSupplyingCarrier(asnDetails.getSupplyingCarrier().intValue()); + } + + if (asnDetails.getOfferIdentification() != null) { + details.setOfferIdentification(asnDetails.getOfferIdentification().intValue()); + } + + return details; + } + + + + + + + + /** + * Convert route section. + * + * @param asnDocument the asn document + * @return the i route section + */ + protected IRouteSection convertRouteSection(RouteSectionType asnDocument) { + + if (asnDocument == null) return null; + + IRouteSection document = factory.createRouteSection(); + if (asnDocument.getStationCodeTable()!=null){ + document.setStationCodeTable(asnDocument.getStationCodeTable()); + } + document.setFromStation(UicEncoderUtils.mapToString(asnDocument.getFromStationNum(),asnDocument.getFromStationIA5())); + document.setToStation(UicEncoderUtils.mapToString(asnDocument.getToStationNum(),asnDocument.getToStationIA5())); + document.setFromStationName(asnDocument.getFromStationNameUTF8()); + document.setToStationName(asnDocument.getToStationNameUTF8()); + return document; + } + + /** + * Convert fip ticket. + * + * @param asnDocument the asn document + * @param issuingDate the issuing date + * @return the i fip ticket + */ + protected IFipTicket convertFipTicket(FIPTicketData asnDocument , Date issuingDate) { + + if (asnDocument == null) return null; + + IFipTicket document = factory.createFipTicket(); + if(asnDocument.getClassCode()!=null){ + document.setClassCode(asnDocument.getClassCode()); + } + + document.setValidFrom(asnDocument.getValidFromDate(issuingDate)); + document.setValidUntil(asnDocument.getValidUntilDate(issuingDate)); + + + + document.setProductId(UicEncoderUtils.mapToString(asnDocument.getProductIdNum(),asnDocument.getProductIdIA5())); + document.setProductOwner(UicEncoderUtils.mapToString(asnDocument.getProductOwnerNum(),asnDocument.getProductOwnerIA5())); + document.setReference(UicEncoderUtils.mapToString(asnDocument.getReferenceNum(),asnDocument.getReferenceIA5())); + + if (asnDocument.getActivatedDay()!=null && !asnDocument.getActivatedDay().isEmpty()) { + document.getActivatedDays().addAll(asnDocument.getActivatedDays(issuingDate)); + } + + if(asnDocument.getIncludesSupplements()!=null) { + document.setIncludesSupplements(asnDocument.getIncludesSupplements()); + } + if(asnDocument.getNumberOfTravelDays()!=null){ + document.setNumberOfTravelDates(asnDocument.getNumberOfTravelDays().intValue()); + } + + document.setExtension(convertExtension(asnDocument.getExtension())); + + return document; + } + + /** + * Convert pass. + * + * @param asnDocument the asn document + * @param issuingDate the issuing date + * @return the i pass + */ + protected IPass convertPass(PassData asnDocument, Date issuingDate) { + + if (asnDocument == null) return null; + + IPass document = factory.createPass(); + + if(asnDocument.getClassCode()!=null){ + document.setClassCode(asnDocument.getClassCode()); + } + + document.setValidFrom(asnDocument.getValidFromDate(issuingDate)); + document.setValidUntil(asnDocument.getValidUntilDate(issuingDate)); + + if (asnDocument.getActivatedDay()!=null && !asnDocument.getActivatedDay().isEmpty()) { + if (document.getValidFrom() != null) { + document.getActivatedDays().addAll(asnDocument.getActivatedDays(document.getValidFrom())); + } else { + document.getActivatedDays().addAll(asnDocument.getActivatedDays(issuingDate)); + } + } + + document.setExtension(convertExtension(asnDocument.getExtension())); + document.setInfoText(asnDocument.getInfoText()); + document.setProductId(UicEncoderUtils.mapToString(asnDocument.getProductIdNum(),asnDocument.getProductIdIA5())); + document.setProductOwner(UicEncoderUtils.mapToString(asnDocument.getProductOwnerNum(),asnDocument.getProductOwnerIA5())); + document.setReference(UicEncoderUtils.mapToString(asnDocument.getReferenceNum(),asnDocument.getReferenceIA5())); + + if (asnDocument.getNumberOfDaysOfTravel() != null) { + document.setNumberOfDaysOfTravel(asnDocument.getNumberOfDaysOfTravel().intValue()); + } + if (asnDocument.getNumberOfPossibleTrips() != null) { + document.setNumberOfPossibleTrips(asnDocument.getNumberOfPossibleTrips().intValue()); + } + + document.setPassDescription(asnDocument.getPassDescription()); + if(asnDocument.getPassType()!=null){ + document.setPassType(asnDocument.getPassType().intValue()); + } + + + + if (asnDocument.getCountries()!=null && !asnDocument.getCountries().isEmpty()){ + for(Long number :asnDocument.getCountries()){ + document.addCountry(number.intValue()); + } + } + if (asnDocument.getExcludedServiceBrands()!=null && !asnDocument.getExcludedServiceBrands().isEmpty()){ + for(Long number :asnDocument.getExcludedServiceBrands()){ + document.addExcludedServiceBrand(number.intValue()); + } + } + + if (asnDocument.getIncludedCarriersNum()!=null && !asnDocument.getIncludedCarriersNum().isEmpty()){ + for(Long carrier :asnDocument.getIncludedCarriersNum()){ + document.addIncludedCarrier(carrier.toString()); + } + } + if (asnDocument.getIncludedCarriersIA5()!=null && !asnDocument.getIncludedCarriersIA5().isEmpty()){ + for(String carrier :asnDocument.getIncludedCarriersIA5()){ + document.addIncludedCarrier(carrier); + } + } + + if (asnDocument.getExcludedCarriersNum()!=null && !asnDocument.getExcludedCarriersNum().isEmpty()){ + for(Long carrier :asnDocument.getExcludedCarriersNum()){ + document.addExcludedCarrier(carrier.toString()); + } + } + if (asnDocument.getExcludedCarriersIA5()!=null && !asnDocument.getExcludedCarriersIA5().isEmpty()){ + for(String carrier :asnDocument.getExcludedCarriersIA5()){ + document.addExcludedCarrier(carrier); + } + } + + + if (asnDocument.getIncludedServiceBrands()!=null && !asnDocument.getIncludedServiceBrands().isEmpty()){ + for(Long number :asnDocument.getIncludedServiceBrands()){ + document.addIncludedServiceBrand(number.intValue()); + } + } + + if (asnDocument.getTariffs()!=null && !asnDocument.getTariffs().isEmpty()){ + for(TariffType asnTariff :asnDocument.getTariffs()){ + document.addTariff(convertTariff(asnTariff)); + } + } + + if (asnDocument.getValidRegion()!=null && !asnDocument.getValidRegion().isEmpty()){ + for(RegionalValidityType asnRegion :asnDocument.getValidRegion()){ + document.addValidRegion(convertValidRegion(asnRegion, issuingDate)); + } + } + + + if (asnDocument.getValidityPeriodDetails() != null) { + document.setValidityDetails(convertValidityDetails(asnDocument.getValidityPeriodDetails(), issuingDate)); + } + + document.setPrice(asnDocument.getPrice()); + + if (asnDocument.getVatDetails() != null && !asnDocument.getVatDetails().isEmpty()){ + for (VatDetailType vat : asnDocument.getVatDetails()) { + document.addVatDetail(decodeVatDetail(vat)); + } + } + + + return document; + } + + private IValidityDetails convertValidityDetails(ValidityPeriodDetailType asnDetails, Date issuingDate) { + + if (asnDetails == null) return null; + + IValidityDetails details = factory.createValidityDetails(); + + if (asnDetails.getExcludedTimeRange()!= null && !asnDetails.getExcludedTimeRange().isEmpty()) { + + ITimeRange range = factory.createTimeRange(); + + for (TimeRangeType asnRange : asnDetails.getExcludedTimeRange() ) { + + if (asnRange.getFromTime() != null) { + range.setFromTime(asnRange.getFromTime().intValue()); + } + if (asnRange.getUntilTime() != null) { + range.setUntilTime(asnRange.getUntilTime().intValue()); + } + + details.addTimeRanges(range); + } + + + + } + + if (asnDetails.getValidityPeriod()!= null && !asnDetails.getValidityPeriod().isEmpty()) { + + IValidityRange range = factory.createValidityRange(); + + for (ValidityPeriodType asnRange : asnDetails.getValidityPeriod() ) { + + range.setFromDate(asnRange.getValidFromDate(issuingDate)); + range.setUntilDate(asnRange.getValidUntilDate(issuingDate)); + + range.setValidFromUTCoffset(asnRange.getValidFromUTCOffset()); + if (asnRange.getValidUntilUTCOffset() != null) { + range.setValidUntilUTCoffset(asnRange.getValidUntilUTCOffset()); + } else { + range.setValidUntilUTCoffset(asnRange.getValidFromUTCOffset()); + } + + details.addValidityRanges(range); + } + + } + + return details; + } + + + + + + + + /** + * Convert voucher. + * + * @param asnDocument the asn document + * @param issuingDate the issuing date + * @return the i voucher + */ + protected IVoucher convertVoucher(VoucherData asnDocument , Date issuingDate) { + + if (asnDocument == null) return null; + + IVoucher document = factory.createVoucher(); + + document.setValidFrom(asnDocument.getValidFrom()); + + document.setValidUntil(asnDocument.getValidUntil()); + + document.setInfoText(asnDocument.getInfoText()); + document.setProductId(UicEncoderUtils.mapToString(asnDocument.getProductIdNum(),asnDocument.getProductIdIA5())); + document.setProductOwner(UicEncoderUtils.mapToString(asnDocument.getProductOwnerNum(),asnDocument.getProductOwnerIA5())); + document.setReference(UicEncoderUtils.mapToString(asnDocument.getReferenceNum(),asnDocument.getReferenceIA5())); + + if (asnDocument.getValue()!=null) { + document.setAmount(asnDocument.getValue().intValue()); + } + + if (asnDocument.getType()!= null) { + document.setType(asnDocument.getType().intValue()); + } + + document.setExtension(convertExtension(asnDocument.getExtension())); + + return document; + } + + /** + * Convert customer card. + * + * @param asnCard the asn card + * @param issuingDate the issuing date + * @return the i customer card + */ + protected ICustomerCard convertCustomerCard(CustomerCardData asnCard, Date issuingDate ) { + + if (asnCard == null) return null; + + ICustomerCard card = factory.createCustomerCard(); + + card.setCardId(UicEncoderUtils.mapToString(asnCard.getCardIdNum(), asnCard.getCardIdIA5())); + if (asnCard.getCardType()!= null) { + card.setCardType(asnCard.getCardType().intValue()); + } + card.setCardTypeDescr(asnCard.getCardTypeDescr()); + if(asnCard.getClassCode()!=null){ + card.setClassCode(asnCard.getClassCode()); + } + if(asnCard.getCustomer()!=null) { + card.setCustomer(convertTraveler(asnCard.getCustomer())); + } + if (asnCard.getCustomerStatus()!=null) { + card.setCustomerStatus(asnCard.getCustomerStatus().intValue()); + } + + card.setCustomerStatusDescr(asnCard.getCustomerStatusDescr()); + + card.setValidFrom(asnCard.getValidFromDate()); + + card.setValidUntil(asnCard.getValidUntilDate()); + + + if (asnCard.getIncludedServices() != null && !asnCard.getIncludedServices().isEmpty()){ + for (Asn1BigInteger service: asnCard.getIncludedServices()){ + card.addIncludedService(new Integer(service.intValue())); + } + } + return card; + } + + + /** + * Convert document extension. + * + * @param extension the extension + * @return the i document extension + */ + protected IDocumentExtension convertDocumentExtension(ExtensionData asnExtension) { + + if (asnExtension == null) return null; + + IDocumentExtension documentExtension = factory.createDocumentExtension(); + documentExtension.setId(asnExtension.getExtensionId()); + documentExtension.setBinarydata(asnExtension.getExtensionData()); + return documentExtension; + } + + + /** + * Populate traveler details. + * + * @param asnTravelerDetails the asn traveler details + * @param travelerDetails the traveler details + */ + protected void populateTravelerDetails(TravelerData asnTravelerDetails,ITravelerDetail travelerDetails) { + + if (asnTravelerDetails == null) return; + + travelerDetails.setGroupName(asnTravelerDetails.getGroupName()); + + travelerDetails.setPreferredLanguage(asnTravelerDetails.getPreferedLanguage()); + + if (asnTravelerDetails.getTraveler() != null && !asnTravelerDetails.getTraveler().isEmpty() ) { + for ( TravelerType asnTraveler : asnTravelerDetails.getTraveler() ){ + travelerDetails.addTraveler(convertTraveler(asnTraveler)); + } + } + } + + /** + * Convert traveler. + * + * @param asnTraveler the asn traveler + * @return the i traveler + */ + protected ITraveler convertTraveler(TravelerType asnTraveler) { + + if (asnTraveler == null) return null; + + ITraveler traveler = factory.createTraveler(); + + if (asnTraveler.getCountryOfResidence() != null){ + traveler.setCountryOfResidence(asnTraveler.getCountryOfResidence().intValue()); + } + if (asnTraveler.getCountryOfPassport() != null) { + traveler.setPassportCountry(asnTraveler.getCountryOfPassport().intValue()); + } + + if (asnTraveler.getCountryOfIdCard() != null) { + traveler.setIDCardCountry(asnTraveler.getCountryOfIdCard().intValue()); + } + + + traveler.setCustomerId(UicEncoderUtils.mapToString(asnTraveler.getCustomerIdNum(),asnTraveler.getCustomerIdIA5())); + + traveler.setDateOfBirth(asnTraveler.getDateOfBirth()); + + traveler.setFirstName(asnTraveler.getFirstName()); + if (asnTraveler.getGender()!= null) { + traveler.setGender(asnTraveler.getGender()); + } + traveler.setIdCard(asnTraveler.getIdCard()); + traveler.setLastName(asnTraveler.getLastName()); + + if (asnTraveler.getPassengerType()!= null) { + traveler.setPassengerType(asnTraveler.getPassengerType()); + } + if (asnTraveler.getPassengerWithReducedMobility()!= null) { + traveler.setPassengerWithReducedMobility(asnTraveler.getPassengerWithReducedMobility()); + } + + traveler.setPassportId(asnTraveler.getPassportId()); + traveler.setSecondName(asnTraveler.getSecondName()); + + if (asnTraveler.getTicketHolder() != null) { + traveler.setTicketHolder(asnTraveler.getTicketHolder()); + } + + if (asnTraveler.getStatus()!= null && !asnTraveler.getStatus().isEmpty()) { + + for (CustomerStatusType asnStatus : asnTraveler.getStatus()){ + + traveler.addStatusDescription( mapToStatusDescription(asnStatus) ); + + } + + } + + traveler.setTitle(asnTraveler.getTitle()); + + return traveler; + } + + /** + * Map to status description. + * + * @param asnStatus the asn status + * @return the i customer status description + */ + private ICustomerStatusDescription mapToStatusDescription( CustomerStatusType asnStatus) { + + if (asnStatus == null) return null; + + ICustomerStatusDescription status = factory.createCustomerStatusDescription(); + + status.setDescription(asnStatus.getCustomerStatusDescr()); + + if (asnStatus.getCustomerStatus()!=null) { + status.setStatus(asnStatus.getCustomerStatus().intValue()); + } + + status.setStatusProvider(UicEncoderUtils.mapToString(asnStatus.getStatusProviderNum(), asnStatus.getStatusProviderIA5())); + + return status; + } + + + + + + + + /** + * Populate control details. + * + * @param asnControlDetails the asn control details + * @param controlDetails the control details + */ + protected void populateControlDetails(ControlData asnControlDetails, IControlDetail controlDetails) { + + if (asnControlDetails == null || controlDetails==null ) return; + + if (asnControlDetails.getAgeCheckRequired()!= null) { + controlDetails.setAgeCheckRequired(asnControlDetails.getAgeCheckRequired()); + } + + controlDetails.setExtension(convertExtension(asnControlDetails.getExtension())); + + if (asnControlDetails.getIdentificationByIdCard()!=null){ + controlDetails.setIdentificationByIdCard(asnControlDetails.getIdentificationByIdCard()); + } + + if (asnControlDetails.getIdentificationByPassportId()!= null){ + controlDetails.setIdentificationByPassportId(asnControlDetails.getIdentificationByPassportId()); + } + + if(asnControlDetails.getIdentificationItem()!=null){ + controlDetails.setIdentificationItem(asnControlDetails.getIdentificationItem().intValue()); + } + + controlDetails.setInfoText(asnControlDetails.getInfoText()); + + if (asnControlDetails.getOnlineValidationRequired()!=null){ + controlDetails.setOnlineValidationRequired(asnControlDetails.getOnlineValidationRequired()); + } + + if (asnControlDetails.getRandomDetailedValidationRequired()!= null){ + controlDetails.setRandomDetailedValidationRequired(asnControlDetails.getRandomDetailedValidationRequired().intValue()); + } + + if (asnControlDetails.getReductionCardCheckRequired() != null){ + controlDetails.setReductionCardCheckRequired(asnControlDetails.getReductionCardCheckRequired()); + } + + if(asnControlDetails.getIdentificationByCardReference()!=null && !asnControlDetails.getIdentificationByCardReference().isEmpty()) { + for (CardReferenceType asnCardReference : asnControlDetails.getIdentificationByCardReference()){ + controlDetails.addIdentificationByCardReference(convertCardReference(asnCardReference)); + } + } + + if (asnControlDetails.getIncludedTickets()!=null && !asnControlDetails.getIncludedTickets().isEmpty()) { + for (TicketLinkType asnTicketLink : asnControlDetails.getIncludedTickets()) { + controlDetails.addLinkedTicket(convertTicketLink(asnTicketLink)); + } + } + + + } + + + + + + /** + * Populate issuing detail. + * + * @param asnIssuingDetail the asn issuing detail + * @param issuingDetail the issuing detail + */ + protected void populateIssuingDetail(IssuingData asnIssuingDetail,IIssuingDetail issuingDetail){ + + if (asnIssuingDetail== null || issuingDetail == null) return; + + if (asnIssuingDetail.getActivated() != null) { + issuingDetail.setActivated(asnIssuingDetail.getActivated()); + } + + if (asnIssuingDetail.getSpecimen() != null) { + issuingDetail.setSpecimen(asnIssuingDetail.getSpecimen()); + } else { + issuingDetail.setSpecimen(false); + } + + + if (asnIssuingDetail.getSecurePaperTicket() != null) { + issuingDetail.setSecurePaperTicket(asnIssuingDetail.getSecurePaperTicket()); + } else { + issuingDetail.setSecurePaperTicket(false); + } + + issuingDetail.setExtension(convertExtension(asnIssuingDetail.getExtension())); + + if (asnIssuingDetail.getIssuedOnLine()!= null) { + issuingDetail.setIssuedOnLine(asnIssuingDetail.getIssuedOnLine().intValue()); + } + + if (asnIssuingDetail.getIssuedOnTrainIA5()!= null) { + issuingDetail.setIssuedOnTrain(asnIssuingDetail.getIssuedOnTrainIA5()); + } + if (asnIssuingDetail.getIssuedOnTrainNum()!= null) { + issuingDetail.setIssuedOnTrain(asnIssuingDetail.getIssuedOnTrainNum().toString()); + } + + if (asnIssuingDetail.getIssuerNum() != null || asnIssuingDetail.getIssuerIA5() != null) { + issuingDetail.setIssuer(UicEncoderUtils.mapToString(asnIssuingDetail.getIssuerNum(), asnIssuingDetail.getIssuerIA5())); + } + + issuingDetail.setIssuerName(asnIssuingDetail.getIssuerName()); + + issuingDetail.setIssuerPNR(asnIssuingDetail.getIssuerPNR()); + + issuingDetail.setSecurityProvider(UicEncoderUtils.mapToString(asnIssuingDetail.getSecurityProviderNum(),asnIssuingDetail.getSecurityProviderIA5())); + + + issuingDetail.setIssuingDate(asnIssuingDetail.getIssuingDate()); + + + if (asnIssuingDetail.getPointOfSale() != null) { + issuingDetail.setPointOfSale(convertGeoCoordinate(asnIssuingDetail.getPointOfSale())); + } + + } + + + + /** + * Convert geo coordinate. + * + * @param asnCoordinate the asn coordinate + * @return the i geo coordinate + */ + protected IGeoCoordinate convertGeoCoordinate(GeoCoordinateType asnCoordinate) { + + if (asnCoordinate == null) return null; + + IGeoCoordinate coordinate = factory.createGeoCoordinate(); + + if (asnCoordinate.getCoordinateSystem() != null) { + coordinate.setSystem(asnCoordinate.getCoordinateSystem()); + } + + if (asnCoordinate.getAccuracy() != null) { + coordinate.setAccuracy(asnCoordinate.getAccuracy()); + } + + if (asnCoordinate.getGeoUnit() != null) { + coordinate.setUnit(asnCoordinate.getGeoUnit()); + } + + if (asnCoordinate.getHemisphereLatitude() != null) { + coordinate.setHemisphereLatitude(asnCoordinate.getHemisphereLatitude()); + } + + if (asnCoordinate.getHemisphereLongitude() != null) { + coordinate.setHemisphereLongitude(asnCoordinate.getHemisphereLongitude()); + } + + coordinate.setLongitude (asnCoordinate.getLongitude()); + coordinate.setLatitude (asnCoordinate.getLatitude()); + + + return coordinate; + } + + + /** + * Convert extension. + * + * @param asnExtension the asn extension + * @return the i extension + */ + protected IExtension convertExtension(ExtensionData asnExtension) { + if (asnExtension == null) return null; + IExtension extension = factory.createExtension(); + extension.setId(asnExtension.getExtensionId()); + extension.setBinarydata(asnExtension.getExtensionData()); + return extension; + } + + + /** + * Convert ticket link. + * + * @param asnTicketLink the asn ticket link + * @return the i ticket link + */ + protected ITicketLink convertTicketLink(TicketLinkType asnTicketLink) { + + if (asnTicketLink == null) return null; + + ITicketLink ticketLink = factory.createTicketLink(); + ticketLink.setIssuer(asnTicketLink.getIssuerName()); + ticketLink.setIssuerPNR(asnTicketLink.getIssuerPNR()); + ticketLink.setProductOwner(UicEncoderUtils.mapToString(asnTicketLink.getProductOwnerNum(),asnTicketLink.getProductOwnerIA5())); + ticketLink.setReference(UicEncoderUtils.mapToString(asnTicketLink.getReferenceNum(),asnTicketLink.getReferenceIA5())); + if(asnTicketLink.getTicketType()!=null) { + ticketLink.setTicketType(asnTicketLink.getTicketType()); + } + if(asnTicketLink.getLinkMode()!=null) { + ticketLink.setLinkMode(asnTicketLink.getLinkMode()); + } + return ticketLink; + } + + /** + * Convert card reference. + * + * @param asnCardReference the asn card reference + * @return the card reference + */ + protected ICardReference convertCardReference(CardReferenceType asnCardReference) { + + if (asnCardReference == null) return null; + + ICardReference cardReference = factory.createCardReference(); + cardReference.setCardId(UicEncoderUtils.mapToString(asnCardReference.getCardIdNum(),asnCardReference.getCardIdIA5())); + cardReference.setCardIssuer(UicEncoderUtils.mapToString(asnCardReference.getCardIssuerNum(),asnCardReference.getCardIssuerIA5())); + cardReference.setCardName(asnCardReference.getCardName()); + + if(asnCardReference.getCardType()!=null) { + cardReference.setCardType(asnCardReference.getCardType().intValue()); + } + + + cardReference.setLeadingCardId(UicEncoderUtils.mapToString(asnCardReference.getLeadingCardIdNum(),asnCardReference.getLeadingCardIdIA5())); + cardReference.setTrailingCardId(UicEncoderUtils.mapToString(asnCardReference.getTrailingCardIdNum(),asnCardReference.getTrailingCardIdIA5())); + + return cardReference; + } + +} diff --git a/src/org/uic/ticket/api/utils/UicEncoderUtils.java b/src/org/uic/ticket/api/utils/UicEncoderUtils.java new file mode 100644 index 0000000..0f3a5b4 --- /dev/null +++ b/src/org/uic/ticket/api/utils/UicEncoderUtils.java @@ -0,0 +1,517 @@ +/* + * + */ +package org.uic.ticket.api.utils; + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; + + + +import net.gcdc.asn1.datatypes.Asn1BigInteger; +import net.gcdc.asn1.datatypesimpl.SequenceOfStringIA5; +import net.gcdc.asn1.datatypesimpl.SequenceOfStringUTF8; + +import org.uic.ticket.api.EncodingFormatException; + + +/** + * The Class UicEncoderUtils. + */ +public class UicEncoderUtils { + + + + /** + * Map to int. + * + * @param o the object to be mapped to an integer + * @return the int + */ + public static int mapToInt(Object o){ + + if (o == null){ + return 0; + } + + if (o instanceof Integer) { + return ((Integer)o).intValue(); + } + + if (o instanceof Long) { + return ((Long)o).intValue(); + } + + if (o instanceof String) { + + int i = 0; + + try { + i = Integer.parseInt((String)o); + } catch (NumberFormatException e) { + return 0; + } + + + return i; + } + + int i = 0; + + try { + i = Integer.parseInt(o.toString()); + } catch (NumberFormatException e) { + return 0; + } + + + return i; + + } + + /** + * Map to string. + * + * @param number the number + * @param text the text + * @return the string + */ + public static String mapToString(Asn1BigInteger number, String text) { + if (text != null && text.length() > 0) { + return text; + } else { + if (number != null){ + return number.value().toString(); + } else { + return null; + } + } + } + + /** + * Map to string. + * + * @param number the number + * @param text the text + * @return the string + */ + public static String mapToString(BigInteger number, String text) { + if (text != null && text.length() > 0) { + return text; + } else { + if (number != null){ + return number.toString(); + } else { + return null; + } + } + } + + /** + * Map to string. + * + * @param number the number + * @param text the text + * @return the string + */ + public static String mapToString(Integer number, String text) { + + if (text != null && text.length() > 0) { + return text; + } else { + if (number != null){ + return number.toString(); + } else { + return null; + } + } + } + + /** + * Map to string. + * + * @param number the number + * @param text the text + * @return the string + */ + public static String mapToString(Long number, String text) { + + if (text != null && text.length() > 0) { + return text; + } else { + if (number != null){ + return number.toString(); + } else { + return null; + } + } + } + + + + + /** + * Map to string. + * + * @param numbers the numbers + * @return the collection + */ + public static Collection mapToString(Collection numbers) { + + Collection list = new HashSet(); + + if (numbers == null || numbers.isEmpty()) { + return list; + } + + for (Long number : numbers){ + list.add(number.toString()); + } + + return list; + } + + + + + /** + * Gets the num. + * + * @param text the text + * @return the num + */ + public static Long getNum(String text) { + + if (text == null || text.length() == 0) { + return null; + } + + Long i; + try { + i = Long.parseLong(text); + } catch (NumberFormatException e) { + return null; + } + return i; + } + + /** + * Gets the num. + * + * @param text the text + * @return the num + */ + public static Asn1BigInteger getLargeNum(String text) { + + if (text == null || text.length() == 0) { + return null; + } + + BigInteger i; + try { + i = new BigInteger(text); + } catch (NumberFormatException e) { + return null; + } + return new Asn1BigInteger(i); + } + + /** + * Gets the num list. + * + * @param list the list + * @return the num list + * @throws EncodingFormatException the encoding format exception + */ + public static List getNumList(Collection list) throws EncodingFormatException { + + if (list== null || list.isEmpty()) { + return null; + } + List numList = new ArrayList(); + + for (String text : list){ + Long num = getNum (text); + if (num != null) { + numList.add(num); + } + } + + + if (numList.isEmpty()) { + return null; + } + + return numList; + } + + /** + * Gets the i a5 non num list. + * + * @param list the list + * @return the i a5 non num list + * @throws EncodingFormatException the encoding format exception + */ + public static SequenceOfStringIA5 getIA5NonNumList(Collection list) throws EncodingFormatException { + + if (list== null || list.isEmpty()) { + return null; + } + SequenceOfStringIA5 ia5List = new SequenceOfStringIA5(); + + for (String text : list){ + String ia5 = getIA5NonNum (text); + if (ia5 != null && ia5.length() > 0) { + ia5List.add(ia5); + } + } + + + if (ia5List.isEmpty()) { + return null; + } + + return ia5List; + } + + + + + /** + * Gets the i a5. + * + * @param text the text + * @return the i a5 + * @throws EncodingFormatException the encoding format exception + */ + public static String getIA5(String text) throws EncodingFormatException { + + if (text == null || text.length() == 0) { + return null; + } + + for (int i = 0; i < text.length(); i++){ + int index = text.charAt(i); + if (index < 0 || index > 127) { + throw new EncodingFormatException("Wrong Characters in IA5 String encoding"); + } + } + + return text; + + } + + /** + * Gets the i a5 non num. + * + * @param text the text + * @return the i a5 non num + * @throws EncodingFormatException the encoding format exception + */ + public static String getIA5NonNum(String text) throws EncodingFormatException { + + if (text == null || text.length() == 0) { + return null; + } + + for (int i = 0; i < text.length(); i++){ + int index = text.charAt(i); + if (index < 0 || index > 127) { + throw new EncodingFormatException("Wrong Characters in IA5 String encoding"); + } + } + + try { + Long.parseLong(text); + return null; + } catch (NumberFormatException e) { + return text; + } + + } + + /** + * Gets the restricted int. + * + * @param value the value + * @param min the min + * @param max the max + * @return the restricted int + * @throws EncodingFormatException the encoding format exception + */ + public static Long getRestrictedInt(int value, int min, int max) throws EncodingFormatException { + if (value == 0) return null; + + if (value < min || value > max) { + throw new EncodingFormatException("Integer value exceeds boundaries"); + } + return new Long(value); + } + + /** + * Gets the restricted int with default. + * + * @param value the value + * @param min the min + * @param max the max + * @param defaultValue the default value + * @return the restricted int with default + * @throws EncodingFormatException the encoding format exception + */ + public static Long getRestrictedIntWithDefault(int value, int min, int max, int defaultValue) throws EncodingFormatException { + if (value == defaultValue || value == 0) return null; + + if (value < min || value > max) { + throw new EncodingFormatException("Integer value exceeds boundaries"); + } + return new Long(value); + } + + + /** + * Gets the un restricted int. + * + * @param value the value + * @return the un restricted int + */ + public static Long getUnRestrictedInt(int value) { + if (value == 0) return null; + return new Long(value); + } + + /** + * Gets the un restricted int list. + * + * @param intList the int list + * @return the un restricted int list + */ + public static List getUnRestrictedIntList( Collection intList) { + if (intList == null || intList.isEmpty()) return null; + + List list = new ArrayList(); + + for (Integer i : intList){ + if (i != 0){ + list.add(i.longValue()); + } + } + + if (list.isEmpty()) return null; + return list; + } + + /** + * Gets the restricted int list. + * + * @param intList the int list + * @param min the min + * @param max the max + * @return the restricted int list + * @throws EncodingFormatException the encoding format exception + */ + public static List getRestrictedIntList( Collection intList, int min, int max) throws EncodingFormatException { + if (intList == null || intList.isEmpty()) return null; + + ArrayList list = new ArrayList(); + + for (Integer i : intList){ + if (i != 0){ + + if (i < min || i > max){ + throw new EncodingFormatException("Integer value exceeds boundaries"); + } + + list.add(i); + } + } + + if (list.isEmpty()) return null; + return list; + } + + /** + * Encode restricted integer collection. + * + * @param collection the collection + * @param min the min + * @param max the max + * @return the list + * @throws EncodingFormatException the encoding format exception + */ + public static List encodeRestrictedIntegerCollection(Collection collection, int min, int max) throws EncodingFormatException { + + if ( collection == null || collection.isEmpty()) { + return null; + } + + List list = new ArrayList(); + for (Integer item : collection){ + Long listItem = getRestrictedInt(item, min, max); + if (listItem != null){ + list.add(listItem); + } + } + if (list.isEmpty()){ + return null; + } + return list; + + } + + /** + * Encode integer collection. + * + * @param collection the collection + * @return the list + * @throws EncodingFormatException the encoding format exception + */ + public static SequenceOfStringUTF8 encodeStringCollection(Collection collection) throws EncodingFormatException { + + if ( collection == null || collection.isEmpty()) { + return null; + } + + SequenceOfStringUTF8 list = new SequenceOfStringUTF8(); + for (String item : collection){ + if (item.length() > 0){ + list.add(item); + } + } + if (list.isEmpty()){ + return null; + } + return list; + + } + + /** + * Encode integer collection. + * + * @param collection the collection + * @return the list + * @throws EncodingFormatException the encoding format exception + */ + public static List encodeIntegerCollection(Collection collection) throws EncodingFormatException { + + if ( collection == null || collection.isEmpty()) { + return null; + } + + List list = new ArrayList(); + for (Integer item : collection){ + if (item != null){ + list.add(item.longValue()); + } + } + if (list.isEmpty()){ + return null; + } + return list; + + } + + +} diff --git a/src/org/uic/ticket/api/utils/package.html b/src/org/uic/ticket/api/utils/package.html new file mode 100644 index 0000000..a926c2d --- /dev/null +++ b/src/org/uic/ticket/api/utils/package.html @@ -0,0 +1,7 @@ + + +Utilities + + Provides utilities for the asn.1 encoding and decoding of ticket data. + + \ No newline at end of file -- cgit v1.2.3