Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
38030 amit 1
package com.spice.profitmandi.common.model;
2
 
3
/**
4
 * Where a request came from: device, app, network and location. Plain value — reading headers belongs to the
5
 * transport layer. Strings are cut to the user_action_audit column lengths, so storage is predictable
6
 * (hadb1 runs with an empty sql_mode and would otherwise truncate silently).
7
 */
8
public final class ClientInfo {
9
 
10
	public static final ClientInfo EMPTY = builder().build();
11
 
12
	private final String deviceId;
13
	private final String platform;
14
	private final String appVersion;
15
	private final String clientIp;
16
	private final String userAgent;
17
	private final Double latitude;
18
	private final Double longitude;
19
	private final Integer geoAccuracyM;
20
	private final String geoSource;
21
	private final String geoStatus;
22
 
23
	private ClientInfo(Builder b) {
24
		this.deviceId = cut(b.deviceId, 128);
25
		this.platform = cut(b.platform, 16);
26
		this.appVersion = cut(b.appVersion, 32);
27
		this.clientIp = cut(b.clientIp, 45);
28
		this.userAgent = cut(b.userAgent, 512);
29
		this.latitude = b.latitude;
30
		this.longitude = b.longitude;
31
		this.geoAccuracyM = b.geoAccuracyM;
32
		this.geoSource = cut(b.geoSource, 16);
33
		this.geoStatus = cut(b.geoStatus, 16);
34
	}
35
 
36
	private static String cut(String value, int max) {
37
		if (value == null) return null;
38
		String trimmed = value.trim();
39
		if (trimmed.isEmpty()) return null;
40
		return trimmed.length() > max ? trimmed.substring(0, max) : trimmed;
41
	}
42
 
43
	public static Builder builder() {
44
		return new Builder();
45
	}
46
 
47
	public boolean hasLocation() {
48
		return latitude != null && longitude != null;
49
	}
50
 
51
	public String getDeviceId() {
52
		return deviceId;
53
	}
54
 
55
	public String getPlatform() {
56
		return platform;
57
	}
58
 
59
	public String getAppVersion() {
60
		return appVersion;
61
	}
62
 
63
	public String getClientIp() {
64
		return clientIp;
65
	}
66
 
67
	public String getUserAgent() {
68
		return userAgent;
69
	}
70
 
71
	public Double getLatitude() {
72
		return latitude;
73
	}
74
 
75
	public Double getLongitude() {
76
		return longitude;
77
	}
78
 
79
	public Integer getGeoAccuracyM() {
80
		return geoAccuracyM;
81
	}
82
 
83
	public String getGeoSource() {
84
		return geoSource;
85
	}
86
 
87
	public String getGeoStatus() {
88
		return geoStatus;
89
	}
90
 
91
	@Override
92
	public String toString() {
93
		return "ClientInfo[deviceId=" + deviceId + ", platform=" + platform + ", appVersion=" + appVersion
94
				+ ", clientIp=" + clientIp + ", geo=" + latitude + "," + longitude + " " + geoSource + "/" + geoStatus + "]";
95
	}
96
 
97
	public static final class Builder {
98
		private String deviceId;
99
		private String platform;
100
		private String appVersion;
101
		private String clientIp;
102
		private String userAgent;
103
		private Double latitude;
104
		private Double longitude;
105
		private Integer geoAccuracyM;
106
		private String geoSource;
107
		private String geoStatus;
108
 
109
		private Builder() {
110
		}
111
 
112
		public Builder deviceId(String deviceId) {
113
			this.deviceId = deviceId;
114
			return this;
115
		}
116
 
117
		public Builder platform(String platform) {
118
			this.platform = platform;
119
			return this;
120
		}
121
 
122
		public Builder appVersion(String appVersion) {
123
			this.appVersion = appVersion;
124
			return this;
125
		}
126
 
127
		public Builder clientIp(String clientIp) {
128
			this.clientIp = clientIp;
129
			return this;
130
		}
131
 
132
		public Builder userAgent(String userAgent) {
133
			this.userAgent = userAgent;
134
			return this;
135
		}
136
 
137
		public Builder location(Double latitude, Double longitude, Integer accuracyM, String source) {
138
			this.latitude = latitude;
139
			this.longitude = longitude;
140
			this.geoAccuracyM = accuracyM;
141
			this.geoSource = source;
142
			return this;
143
		}
144
 
145
		public Builder geoStatus(String geoStatus) {
146
			this.geoStatus = geoStatus;
147
			return this;
148
		}
149
 
150
		public ClientInfo build() {
151
			return new ClientInfo(this);
152
		}
153
	}
154
}