Subversion Repositories SmartDukaan

Rev

Rev 27608 | Rev 28266 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 27608 Rev 28264
Line 4... Line 4...
4
import java.io.File;
4
import java.io.File;
5
import java.io.FileInputStream;
5
import java.io.FileInputStream;
6
import java.io.FileNotFoundException;
6
import java.io.FileNotFoundException;
7
import java.io.InputStream;
7
import java.io.InputStream;
8
import java.io.InputStreamReader;
8
import java.io.InputStreamReader;
-
 
9
import java.sql.Timestamp;
-
 
10
import java.time.LocalDate;
9
import java.time.LocalDateTime;
11
import java.time.LocalDateTime;
10
import java.time.ZoneOffset;
12
import java.time.ZoneOffset;
11
import java.util.ArrayList;
13
import java.util.ArrayList;
12
import java.util.Arrays;
14
import java.util.Arrays;
13
import java.util.HashMap;
15
import java.util.HashMap;
Line 43... Line 45...
43
import com.jcraft.jsch.SftpATTRS;
45
import com.jcraft.jsch.SftpATTRS;
44
import com.jcraft.jsch.SftpException;
46
import com.jcraft.jsch.SftpException;
45
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
47
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
46
import com.spice.profitmandi.common.solr.SolrService;
48
import com.spice.profitmandi.common.solr.SolrService;
47
import com.spice.profitmandi.dao.entity.catalog.Item;
49
import com.spice.profitmandi.dao.entity.catalog.Item;
-
 
50
import com.spice.profitmandi.dao.entity.dtr.DocumentUrl;
48
import com.spice.profitmandi.dao.model.ContentPojo;
51
import com.spice.profitmandi.dao.model.ContentPojo;
49
import com.spice.profitmandi.dao.model.MediaPojo;
52
import com.spice.profitmandi.dao.model.MediaPojo;
50
import com.spice.profitmandi.dao.model.Specification;
53
import com.spice.profitmandi.dao.model.Specification;
51
import com.spice.profitmandi.dao.model.SpecificationGroup;
54
import com.spice.profitmandi.dao.model.SpecificationGroup;
52
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
55
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
-
 
56
import com.spice.profitmandi.dao.repository.dtr.DocumentUrlRepository;
53
import com.spice.profitmandi.dao.repository.dtr.Mongo;
57
import com.spice.profitmandi.dao.repository.dtr.Mongo;
54
import com.spice.profitmandi.web.model.EntityMediaPojo;
58
import com.spice.profitmandi.web.model.EntityMediaPojo;
55
import com.spice.profitmandi.web.util.MVCResponseSender;
59
import com.spice.profitmandi.web.util.MVCResponseSender;
56
 
60
 
57
@Transactional(rollbackOn = Throwable.class)
61
@Transactional(rollbackOn = Throwable.class)
Line 67... Line 71...
67
	SolrService solrService;
71
	SolrService solrService;
68
 
72
 
69
	@Autowired
73
	@Autowired
70
	ItemRepository itemRepository;
74
	ItemRepository itemRepository;
71
 
75
 
-
 
76
	@Autowired
-
 
77
	DocumentUrlRepository documentUrlRepository;
-
 
78
 
72
	private Gson gson = new Gson();
79
	private Gson gson = new Gson();
73
 
80
 
74
	public static final int Entity_Id = 0;
81
	public static final int Entity_Id = 0;
75
	public static final int Title = 1;
82
	public static final int Title = 1;
76
	public static final int KeySpec1 = 2;
83
	public static final int KeySpec1 = 2;
77
	public static final int KeySpec2 = 3;
84
	public static final int KeySpec2 = 3;
78
	public static final int KeySpec3 = 4;
85
	public static final int KeySpec3 = 4;
79
	public static final int KeySpec4 = 5;
86
	public static final int KeySpec4 = 5;
80
	public static final int Warranty = 6;
87
	public static final int Warranty = 6;
81
	public static final int Package_Contents = 7;
88
	public static final int Package_Contents = 7;
-
 
89
	private static final String IMAGE_REMOTE_DIR = "/var/www/dtrdashboard/uploads/campaigns/";
-
 
90
	private static final String IMAGE_STATIC_SERVER_URL = "https://images.smartdukaan.com/uploads/campaigns";
82
 
91
 
83
	private static final String REMOTE_DIR = "/var/www/static.saholic.com/images/media/";
92
	private static final String REMOTE_DIR = "/var/www/static.saholic.com/images/media/";
84
	private static final String STATIC_SERVER_URL = "https://static%d.smartdukaan.com/images/media/";
93
	private static final String STATIC_SERVER_URL = "https://static%d.smartdukaan.com/images/media/";
85
 
94
 
86
	private static final String THUMBNAIL = "thumbnail";
95
	private static final String THUMBNAIL = "thumbnail";
Line 118... Line 127...
118
		mongoClient.persistEntity(contentPojo);
127
		mongoClient.persistEntity(contentPojo);
119
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
128
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
120
		return "response";
129
		return "response";
121
	}
130
	}
122
 
131
 
-
 
132
	@PostMapping(value = "/image/media/upload")
-
 
133
	public String uploadImageMediaContent(HttpServletRequest request, @RequestBody MediaPojo mediaPojo, Model model)
-
 
134
			throws Exception {
-
 
135
 
-
 
136
		LOGGER.info("mediaPojo" + mediaPojo);
-
 
137
		Map<String, InputStream> fileStreamsMap = new HashMap<>();
-
 
138
		String extension;
-
 
139
		String base64String = mediaPojo.getImageData();
-
 
140
		String[] strings = base64String.split(",");
-
 
141
		switch (strings[0]) {// check image's extension
-
 
142
		case "data:image/jpeg;base64":
-
 
143
			extension = "jpeg";
-
 
144
			break;
-
 
145
		case "data:image/png;base64":
-
 
146
			extension = "png";
-
 
147
			break;
-
 
148
		default:// should write cases for more images types
-
 
149
			extension = ".jpg";
-
 
150
			break;
-
 
151
		}
-
 
152
		LOGGER.info("After switch statement = {}", extension);
-
 
153
		// convert base64 string to binary data
-
 
154
		byte[] data = DatatypeConverter.parseBase64Binary(strings[1]);
-
 
155
		Timestamp tm = new Timestamp(System.currentTimeMillis());
-
 
156
		String fileName = mediaPojo.getTitle() + tm.getTime() + "." + extension;
-
 
157
		LOGGER.info("After switch statement Filename = {}", fileName);
-
 
158
		mediaPojo.setImageData(null);
-
 
159
		mediaPojo.setUrl(IMAGE_STATIC_SERVER_URL + "/" + "image" + LocalDate.now() + "/" + fileName);
-
 
160
		fileStreamsMap.put(fileName, new ByteArrayInputStream(data));
-
 
161
		LOGGER.info("fileStreamsMap" + fileStreamsMap);
-
 
162
 
-
 
163
		uploadFile(fileStreamsMap);
-
 
164
 
-
 
165
		DocumentUrl du = new DocumentUrl();
-
 
166
		du.setTitle(mediaPojo.getTitle());
-
 
167
		du.setUrl(mediaPojo.getUrl());
-
 
168
		du.setCreatedTimestamp(LocalDateTime.now());
-
 
169
		documentUrlRepository.persist(du);
-
 
170
 
-
 
171
		model.addAttribute("documents", du);
-
 
172
		return "image-url";
-
 
173
	}
-
 
174
 
-
 
175
	private void uploadFile(Map<String, InputStream> fileStreamsMap) throws Exception {
-
 
176
		ChannelSftp channelSftp = setupJsch();
-
 
177
		channelSftp.connect();
-
 
178
		this.fileUpload(channelSftp, fileStreamsMap, IMAGE_REMOTE_DIR + "");
-
 
179
		channelSftp.exit();
-
 
180
	}
-
 
181
 
-
 
182
	private void fileUpload(ChannelSftp channelSftp, Map<String, InputStream> streamsFileMap, String destinationPath)
-
 
183
			throws SftpException, FileNotFoundException {
-
 
184
 
-
 
185
		channelSftp.cd(destinationPath);
-
 
186
		String folderName = "image" + LocalDate.now();
-
 
187
 
-
 
188
		channelSftp.cd(destinationPath);
-
 
189
		SftpATTRS attrs = null;
-
 
190
 
-
 
191
		// check if the directory is already existing
-
 
192
		try {
-
 
193
			attrs = channelSftp.stat(folderName);
-
 
194
		} catch (Exception e) {
-
 
195
			System.out.println(destinationPath + "/" + "image" + LocalDate.now() + " not found");
-
 
196
		}
-
 
197
 
-
 
198
		// else create a directory
-
 
199
		if (attrs == null) {
-
 
200
			channelSftp.mkdir(folderName);
-
 
201
			channelSftp.chmod(0755, ".");
-
 
202
		}
-
 
203
		channelSftp.cd(folderName);
-
 
204
 
-
 
205
		for (Map.Entry<String, InputStream> streamsFileEntry : streamsFileMap.entrySet()) {
-
 
206
			channelSftp.put(streamsFileEntry.getValue(), streamsFileEntry.getKey(), ChannelSftp.OVERWRITE);
-
 
207
		}
-
 
208
 
-
 
209
	}
-
 
210
 
-
 
211
	@GetMapping(value = "/search/image/media")
-
 
212
	public String getImage(HttpServletRequest request, Model model, @RequestParam String title) throws Exception {
-
 
213
		List<DocumentUrl> documents = documentUrlRepository.selectBySearch(title);
-
 
214
 
-
 
215
		model.addAttribute("documents", documents);
-
 
216
		return "image-url";
-
 
217
 
-
 
218
	}
-
 
219
 
123
	@GetMapping(value = "/content/media")
220
	@GetMapping(value = "/content/media")
124
	public String getMediaContent(HttpServletRequest request, Model model, @RequestParam int entityId)
221
	public String getMediaContent(HttpServletRequest request, Model model, @RequestParam int entityId)
125
			throws Exception {
222
			throws Exception {
126
		ContentPojo contentPojo = mongoClient.getEntityById(entityId);
223
		ContentPojo contentPojo = mongoClient.getEntityById(entityId);
127
		if (contentPojo == null) {
224
		if (contentPojo == null) {
Line 129... Line 226...
129
		}
226
		}
130
		EntityMediaPojo empojo = getEntityMediaPojo(contentPojo);
227
		EntityMediaPojo empojo = getEntityMediaPojo(contentPojo);
131
		model.addAttribute("response", mvcResponseSender.createResponseString(empojo));
228
		model.addAttribute("response", mvcResponseSender.createResponseString(empojo));
132
		return "response";
229
		return "response";
133
	}
230
	}
134
	
231
 
135
	@GetMapping(value = "/catalog-item")
232
	@GetMapping(value = "/catalog-item")
136
	public String catalogItem(HttpServletRequest request, Model model)
233
	public String catalogItem(HttpServletRequest request, Model model) throws Exception {
137
			throws Exception {
-
 
138
		return "catalog-item";
234
		return "catalog-item";
139
	}
235
	}
140
 
236
 
141
	private EntityMediaPojo getEntityMediaPojo(ContentPojo contentPojo) {
237
	private EntityMediaPojo getEntityMediaPojo(ContentPojo contentPojo) {
142
		EntityMediaPojo ep = new EntityMediaPojo();
238
		EntityMediaPojo ep = new EntityMediaPojo();
Line 157... Line 253...
157
		ep.setDefaultImageIndex(defaultIndex);
253
		ep.setDefaultImageIndex(defaultIndex);
158
		return ep;
254
		return ep;
159
	}
255
	}
160
 
256
 
161
	@GetMapping(value = "/entity")
257
	@GetMapping(value = "/entity")
162
	public String searchEntity(HttpServletRequest request, @RequestParam(defaultValue="null") String query,
258
	public String searchEntity(HttpServletRequest request, @RequestParam(defaultValue = "null") String query,
163
							   @RequestParam(defaultValue="0") int categoryId,
-
 
164
							   @RequestParam(defaultValue="") List<String> brands, 
259
			@RequestParam(defaultValue = "0") int categoryId, @RequestParam(defaultValue = "") List<String> brands,
165
							   @RequestParam(defaultValue="30") int limit, 
260
			@RequestParam(defaultValue = "30") int limit, Model model) throws Exception {
166
							   Model model) throws Exception {
-
 
167
		model.addAttribute("response", solrService.getContent(query, categoryId, brands, limit));
261
		model.addAttribute("response", solrService.getContent(query, categoryId, brands, limit));
168
		return "response";
262
		return "response";
169
	}
263
	}
170
 
264
 
171
	@GetMapping(value = "/content/index")
265
	@GetMapping(value = "/content/index")
Line 236... Line 330...
236
		return specificationGroups;
330
		return specificationGroups;
237
	}
331
	}
238
 
332
 
239
	private ChannelSftp setupJsch() throws JSchException {
333
	private ChannelSftp setupJsch() throws JSchException {
240
		JSch jsch = new JSch();
334
		JSch jsch = new JSch();
241
		Session jschSession = jsch.getSession("root", "192.168.179.131");
335
		Session jschSession = jsch.getSession("root", "50.116.10.120");
242
		jschSession.setPassword("spic@2015static0");
336
		jschSession.setPassword("spic@2015static0");
243
		jschSession.setConfig("StrictHostKeyChecking", "no");
337
		jschSession.setConfig("StrictHostKeyChecking", "no");
244
		jschSession.connect();
338
		jschSession.connect();
245
		return (ChannelSftp) jschSession.openChannel("sftp");
339
		return (ChannelSftp) jschSession.openChannel("sftp");
246
	}
340
	}
Line 369... Line 463...
369
		mediaPrefix = mediaPrefix.replaceAll("/", "-");
463
		mediaPrefix = mediaPrefix.replaceAll("/", "-");
370
		mediaPrefix = mediaPrefix.replaceAll("-+", "-");
464
		mediaPrefix = mediaPrefix.replaceAll("-+", "-");
371
		return mediaPrefix;
465
		return mediaPrefix;
372
	}
466
	}
373
 
467
 
-
 
468
	@GetMapping(value = "/urlGeneration")
-
 
469
	public String urlGeneration(HttpServletRequest request, Model model) throws Exception {
-
 
470
		return "url-generation";
-
 
471
	}
374
}
472
}