Код: Выделить всё
package com.example.justin_revature_project.controllers;
import com.example.justin_revature_project.exceptions.InvalidInputException;
import com.example.justin_revature_project.exceptions.SpecFileNotFoundException;
import com.example.justin_revature_project.exceptions.UserNotFoundException;
import com.example.justin_revature_project.models.MetadataFile;
import com.example.justin_revature_project.models.ParsedFile;
import com.example.justin_revature_project.models.SpecFile;
import com.example.justin_revature_project.models.User;
import com.example.justin_revature_project.services.MetadataFileService;
import com.example.justin_revature_project.services.ParsedFileService;
import com.example.justin_revature_project.services.SpecFileService;
import com.example.justin_revature_project.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
@RestController
@RequestMapping("/users/{username}/file")
public class ParsedFileController {
private ParsedFileService parsedFileService;
private SpecFileService specFileService;
private UserService userService;
private MetadataFileService metadataFileService;
@Autowired
public ParsedFileController(ParsedFileService parsedFileService, SpecFileService specFileService, UserService userService, MetadataFileService metadataFileService){
this.parsedFileService = parsedFileService;
this.specFileService = specFileService;
this.userService = userService;
this.metadataFileService = metadataFileService;
}
@GetMapping("")
@ResponseStatus(HttpStatus.OK)
public List
getParsedFilesByUserId(@PathVariable String username) throws UserNotFoundException {
return this.parsedFileService.findParsedFilesByUserId(this.userService.findByUsername(username).getId());
}
@PostMapping("specFile/{specFileName}")
@ResponseStatus(HttpStatus.ACCEPTED)
public ParsedFile postNewParsedFileWithSpecFileName(@RequestParam MultipartFile flatFile, @PathVariable String specFileName, @PathVariable String username)
throws SpecFileNotFoundException, IOException, UserNotFoundException, InvalidInputException {
User currUser = this.userService.findByUsername(username);
SpecFile specFile = this.specFileService.findByName(specFileName);
ParsedFile newParsedFile = this.parsedFileService.insertFile(flatFile, specFile, currUser);
// Add Parsed FileID to User who uploaded the file.
this.userService.addParsedFileId(newParsedFile.getId(), currUser);
// TODO: Find a way to update MetadataID field in parsedFile
this.metadataFileService.insertMetadataFileLocal(new MetadataFile(newParsedFile.getId(), LocalDate.now()),
"src\\main\\java\\resources\\" + currUser.getUsername() + "\\" + newParsedFile.getId().toHexString());
return newParsedFile;
}
}
Код: Выделить всё
package com.example.justin_revature_project.services;
import com.example.justin_revature_project.exceptions.InvalidInputException;
import com.example.justin_revature_project.models.ParsedFile;
import com.example.justin_revature_project.models.SpecFile;
import com.example.justin_revature_project.models.User;
import com.example.justin_revature_project.repositores.ParsedFileRepository;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
@Service
public class ParsedFileService {
private ParsedFileRepository parsedFileRepository;
@Autowired
public ParsedFileService(ParsedFileRepository parsedFileRepository){
this.parsedFileRepository = parsedFileRepository;
}
//TODO: ADD METADATA ID AND STORE FLAT FILE TO BLOCK STORAGE
public ParsedFile insertFile(MultipartFile flatFile, SpecFile specFile, User user) throws IOException, InvalidInputException {
//TODO: Create New EXCEPTION if wanted
//User Cannot use specfile that it did not create
if(!user.getListOfSpecFileIds().contains(specFile.getId())){
throw new InvalidInputException(String.format("Cannot find specFile: %s in the user: %s", specFile.getName(), user.getUsername()));
}
ParsedFile newParsedFile = new ParsedFile();
Document docParsedFileInfo = new Document();
String fileContent = HelperForService.parseFileToString(flatFile);
Document specFileDoc = specFile.getDocOfFields();
for(String fieldName : specFileDoc.keySet()){
//TODO: Add Class Types
Document fieldDoc = specFileDoc.get(fieldName, Document.class);
String fieldValue = fileContent.substring(
fieldDoc.getInteger("start_pos"), fieldDoc.getInteger("end_pos") + 1)
.trim();
docParsedFileInfo.put(fieldName, fieldValue);
}
newParsedFile.setUserId(user.getId());
newParsedFile.setSpecId(specFile.getId());
newParsedFile.setFileInfo(docParsedFileInfo);
ParsedFile parsedFileWithId = this.parsedFileRepository.save(newParsedFile);
//TODO: Replace with Non-Local save
HelperForService.uploadFileLocally(fileContent, "src\\main\\resources\\flatFileStorage\\" + user.getUsername(), parsedFileWithId.getId().toHexString());
return parsedFileWithId;
}
public List
findParsedFilesByUserId(ObjectId userID){
return this.parsedFileRepository.findByUserId(userID);
}

Вот файл спецификации:
Код: Выделить всё
{
"name": "example_spec",
"docOfFields": {
"field_1": "date",
"field_2": "date",
"field_3": "date",
"field_4": "date"
}
}
Эта строка, указанная в журналах, является источником ошибки:
Код: Выделить всё
Document fieldDoc = specFileDoc.get(fieldName, Document.class);
Подробнее здесь: https://stackoverflow.com/questions/782 ... not-cast-j