Subversion Repositories SmartDukaan

Rev

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

Rev 35523 Rev 35525
Line 37... Line 37...
37
import org.apache.logging.log4j.LogManager;
37
import org.apache.logging.log4j.LogManager;
38
import org.apache.logging.log4j.Logger;
38
import org.apache.logging.log4j.Logger;
39
import org.json.JSONArray;
39
import org.json.JSONArray;
40
import org.json.JSONObject;
40
import org.json.JSONObject;
41
import org.springframework.beans.factory.annotation.Autowired;
41
import org.springframework.beans.factory.annotation.Autowired;
-
 
42
import org.springframework.http.HttpStatus;
42
import org.springframework.http.ResponseEntity;
43
import org.springframework.http.ResponseEntity;
43
import org.springframework.stereotype.Controller;
44
import org.springframework.stereotype.Controller;
44
import org.springframework.transaction.annotation.Transactional;
45
import org.springframework.transaction.annotation.Transactional;
45
import org.springframework.ui.Model;
46
import org.springframework.ui.Model;
46
import org.springframework.web.bind.annotation.*;
47
import org.springframework.web.bind.annotation.*;
Line 121... Line 122...
121
		String imageString = getMediaPrefix(items.get(0).getItemDescriptionNoColor() + " " + description) + "-"
122
		String imageString = getMediaPrefix(items.get(0).getItemDescriptionNoColor() + " " + description) + "-"
122
				+ LocalDateTime.now().toEpochSecond(ZoneOffset.ofHoursMinutes(5, 30));
123
				+ LocalDateTime.now().toEpochSecond(ZoneOffset.ofHoursMinutes(5, 30));
123
		return imageString + "." + extension;
124
		return imageString + "." + extension;
124
	}
125
	}
125
 
126
 
-
 
127
/*	@PostMapping(value = "/content/upload")
-
 
128
	public String uploadContent(HttpServletRequest request, @RequestPart("file") MultipartFile file, Model model)
-
 
129
			throws Exception {
-
 
130
		List<ContentPojo> contentPojos = readFile(file);
-
 
131
		for (ContentPojo contentPojo : contentPojos) {
-
 
132
			mongoClient.persistEntity(contentPojo);
-
 
133
		}
-
 
134
		//List<SuperCatalog> superCatalog = superCatalogRepository.selectAll();
-
 
135
 
-
 
136
		model.addAttribute("response1", mvcResponseSender.createResponseString(true));
-
 
137
		return "response";
-
 
138
	}*/
-
 
139
 
126
	@PostMapping(value = "/content/upload")
140
	@PostMapping(value = "/content/upload")
127
	public String uploadContent(HttpServletRequest request, @RequestPart("file") MultipartFile file, Model model) {
141
	public ResponseEntity<?> uploadContent(HttpServletRequest request, @RequestPart("file") MultipartFile file, Model model)
128
		try {
-
 
129
			// 1. Validation
142
			throws Exception {
130
			if (file.isEmpty()) {
-
 
131
				model.addAttribute("error", "Please select a file to upload.");
-
 
132
				return "response";
-
 
133
			}
-
 
134
 
143
 
135
			// 2. Processing
144
		try {
136
			List<ContentPojo> contentPojos = readFile(file);
145
			List<ContentPojo> contentPojos = readFile(file);
137
 
146
 
138
			// 3. Persistence
-
 
139
			for (ContentPojo contentPojo : contentPojos) {
147
			for (ContentPojo contentPojo : contentPojos) {
140
				mongoClient.persistEntity(contentPojo);
148
				mongoClient.persistEntity(contentPojo);
141
			}
149
			}
142
 
150
 
143
			// 4. Success
151
			return ResponseEntity.ok(
144
			model.addAttribute("response1", mvcResponseSender.createResponseString(true));
152
					mvcResponseSender.createResponseString(true)
145
			model.addAttribute("message", "Upload successful!");
153
			);
146
 
154
 
147
		} catch (ProfitMandiBusinessException e) {
-
 
148
			// Catch your custom business exceptions (e.g., "No records found")
-
 
149
			LOGGER.error("Business error: {}", e.getMessage());
-
 
150
			model.addAttribute("error", e.getMessage());
-
 
151
		} catch (Exception e) {
155
		} catch (Exception e) {
-
 
156
			e.printStackTrace();
152
			// Catch technical errors (IO, DB, Parsing)
157
			return ResponseEntity
153
			LOGGER.error("System error during upload: ", e);
158
					.status(HttpStatus.INTERNAL_SERVER_ERROR)
154
			model.addAttribute("error", "An internal error occurred: " + e.getMessage());
159
					.body(mvcResponseSender.createResponseString(false));
155
		}
160
		}
156
 
-
 
157
		// This return is now reachable even if an error occurs
-
 
158
		return "response";
-
 
159
	}
161
	}
160
 
162
 
-
 
163
 
161
	@Autowired
164
	@Autowired
162
	ItemLoaderService itemLoaderService;
165
	ItemLoaderService itemLoaderService;
163
	@PostMapping(value = "/catalog/upload")
166
	@PostMapping(value = "/catalog/upload")
164
	public String uploadCatalog(HttpServletRequest request, @RequestPart("file") MultipartFile file, Model model)
167
	public String uploadCatalog(HttpServletRequest request, @RequestPart("file") MultipartFile file, Model model)
165
			throws Exception {
168
			throws Exception {