Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package com.spice.profitmandi.common.model;

/**
 * Where a request came from: device, app, network and location. Plain value — reading headers belongs to the
 * transport layer. Strings are cut to the user_action_audit column lengths, so storage is predictable
 * (hadb1 runs with an empty sql_mode and would otherwise truncate silently).
 */
public final class ClientInfo {

        public static final ClientInfo EMPTY = builder().build();

        private final String deviceId;
        private final String platform;
        private final String appVersion;
        private final String clientIp;
        private final String userAgent;
        private final Double latitude;
        private final Double longitude;
        private final Integer geoAccuracyM;
        private final String geoSource;
        private final String geoStatus;

        private ClientInfo(Builder b) {
                this.deviceId = cut(b.deviceId, 128);
                this.platform = cut(b.platform, 16);
                this.appVersion = cut(b.appVersion, 32);
                this.clientIp = cut(b.clientIp, 45);
                this.userAgent = cut(b.userAgent, 512);
                this.latitude = b.latitude;
                this.longitude = b.longitude;
                this.geoAccuracyM = b.geoAccuracyM;
                this.geoSource = cut(b.geoSource, 16);
                this.geoStatus = cut(b.geoStatus, 16);
        }

        private static String cut(String value, int max) {
                if (value == null) return null;
                String trimmed = value.trim();
                if (trimmed.isEmpty()) return null;
                return trimmed.length() > max ? trimmed.substring(0, max) : trimmed;
        }

        public static Builder builder() {
                return new Builder();
        }

        public boolean hasLocation() {
                return latitude != null && longitude != null;
        }

        public String getDeviceId() {
                return deviceId;
        }

        public String getPlatform() {
                return platform;
        }

        public String getAppVersion() {
                return appVersion;
        }

        public String getClientIp() {
                return clientIp;
        }

        public String getUserAgent() {
                return userAgent;
        }

        public Double getLatitude() {
                return latitude;
        }

        public Double getLongitude() {
                return longitude;
        }

        public Integer getGeoAccuracyM() {
                return geoAccuracyM;
        }

        public String getGeoSource() {
                return geoSource;
        }

        public String getGeoStatus() {
                return geoStatus;
        }

        @Override
        public String toString() {
                return "ClientInfo[deviceId=" + deviceId + ", platform=" + platform + ", appVersion=" + appVersion
                                + ", clientIp=" + clientIp + ", geo=" + latitude + "," + longitude + " " + geoSource + "/" + geoStatus + "]";
        }

        public static final class Builder {
                private String deviceId;
                private String platform;
                private String appVersion;
                private String clientIp;
                private String userAgent;
                private Double latitude;
                private Double longitude;
                private Integer geoAccuracyM;
                private String geoSource;
                private String geoStatus;

                private Builder() {
                }

                public Builder deviceId(String deviceId) {
                        this.deviceId = deviceId;
                        return this;
                }

                public Builder platform(String platform) {
                        this.platform = platform;
                        return this;
                }

                public Builder appVersion(String appVersion) {
                        this.appVersion = appVersion;
                        return this;
                }

                public Builder clientIp(String clientIp) {
                        this.clientIp = clientIp;
                        return this;
                }

                public Builder userAgent(String userAgent) {
                        this.userAgent = userAgent;
                        return this;
                }

                public Builder location(Double latitude, Double longitude, Integer accuracyM, String source) {
                        this.latitude = latitude;
                        this.longitude = longitude;
                        this.geoAccuracyM = accuracyM;
                        this.geoSource = source;
                        return this;
                }

                public Builder geoStatus(String geoStatus) {
                        this.geoStatus = geoStatus;
                        return this;
                }

                public ClientInfo build() {
                        return new ClientInfo(this);
                }
        }
}