Java tutorial
package blankd.acme.pet.licensing.rest.controller; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import blankd.acme.pet.licensing.model.Account; import blankd.acme.pet.licensing.model.ErrorMessage; import blankd.acme.pet.licensing.model.License; import blankd.acme.pet.licensing.model.Message; import blankd.acme.pet.licensing.model.Pet; import blankd.acme.pet.licensing.repo.AccountRepository; import blankd.acme.pet.licensing.repo.LicenseRepository; import blankd.acme.pet.licensing.repo.PetRepository; @RestController @RequestMapping("/license") public class LicenseRestController { private static final Logger log = LoggerFactory.getLogger(LicenseRestController.class); private LicenseRepository repo; private PetRepository pRepo; private AccountRepository aRepo; @Autowired public LicenseRestController(LicenseRepository lRepo, PetRepository pRepo, AccountRepository aRepo) { this.repo = lRepo; this.aRepo = aRepo; this.pRepo = pRepo; } @RequestMapping(value = "/find/assigned", method = RequestMethod.GET) public Page<License> getAllAssignedLicenses(Pageable pages) { return this.repo.findByPetOwnerNotNull(pages); } @RequestMapping(value = "/find/assigned/{id}", method = RequestMethod.GET) public ResponseEntity<?> findAssignedPets(Pageable pages, @PathVariable String id) { License l = this.repo.findOne(id); if (l != null && l.getPetOwner() != null) { return ResponseEntity.ok(this.pRepo.findByPetLicense(l, pages)); } else { ErrorMessage err = null; if (l == null) { err = new ErrorMessage("License could not be found"); } else if (l.getPetOwner() == null) { err = new ErrorMessage("License is not assigned"); } return ResponseEntity.ok(err); } } @RequestMapping(value = "/find/unassigned", method = RequestMethod.GET) public ResponseEntity<?> getAllUnassignedLicenses(Pageable pages) { Page<License> unassigned = repo.findByPetOwnerNull(pages); if (unassigned != null && unassigned.getSize() > 0) { return new ResponseEntity<Page<License>>(unassigned, HttpStatus.OK); } else { ErrorMessage err = new ErrorMessage("No Unassigned Licenses Available"); return new ResponseEntity<ErrorMessage>(err, HttpStatus.OK); } } @RequestMapping(value = "/find/expired", method = RequestMethod.GET) public ResponseEntity<?> getAllExpiredLicenses(Pageable pages) { Page<License> unassigned = repo.findByExpiresBefore(pages, new Date()); return new ResponseEntity<Page<License>>(unassigned, HttpStatus.OK); } @RequestMapping(method = RequestMethod.GET) public Page<License> getLicensePages(Pageable pagination) { return repo.findAll(pagination); } @RequestMapping(value = "/new", method = RequestMethod.POST) public ResponseEntity<?> addNewLicense(@RequestParam(required = false) String license) { License ret = this.repo.save(new License(license)); if (ret != null) { return ResponseEntity.ok(ret); } else { ErrorMessage err = new ErrorMessage("Could not create new License"); return ResponseEntity.ok(err); } } @RequestMapping(value = "/assign/{id}", method = RequestMethod.POST) public ResponseEntity<?> assignPetToLicense(@PathVariable String id, @RequestParam Long petId, @RequestParam String username) { License ret = this.repo.findOne(id); if (ret != null && (this.isLicenseExpired(ret) && ret.getPetOwner() == null)) { Pet pet = this.pRepo.findById(petId); if (pet == null) { ErrorMessage err = new ErrorMessage("Could not find pet"); return ResponseEntity.ok(err); } Account owner = this.aRepo.findByUsername(username); if (owner == null) { ErrorMessage err = new ErrorMessage("Could not find Account"); return ResponseEntity.ok(err); } ret.setPet(pet); ret.setPetOwner(owner); ret.setExpires(this.generateExpireDate()); return ResponseEntity.ok(this.repo.save(ret)); } else { if (ret.getPetOwner() != null) { ErrorMessage err = new ErrorMessage("License is already assigned"); return ResponseEntity.ok(err); } else if (!this.isLicenseExpired(ret)) { ErrorMessage err = new ErrorMessage("License is not expired"); return ResponseEntity.ok(err); } else { ErrorMessage err = new ErrorMessage("Could not assign license"); return ResponseEntity.ok(err); } } } @RequestMapping(value = "/assign/force/{id}", method = RequestMethod.POST) public ResponseEntity<?> forceAssignPetToLicense(@PathVariable String id, @RequestParam Long petId, @RequestParam String username) { License ret = this.repo.findOne(id); if (ret != null) { Pet pet = this.pRepo.findById(petId); if (pet == null) { ErrorMessage err = new ErrorMessage("Could not find pet"); return ResponseEntity.ok(err); } Account owner = this.aRepo.findByUsername(username); if (owner == null) { ErrorMessage err = new ErrorMessage("Could not find Account"); return ResponseEntity.ok(err); } ret.setPet(pet); ret.setPetOwner(owner); ret.setExpires(this.generateExpireDate()); return ResponseEntity.ok(this.repo.save(ret)); } else { ErrorMessage err = new ErrorMessage("Could not assign license"); return ResponseEntity.ok(err); } } @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST) public ResponseEntity<?> deleteLicense(@PathVariable String id) { License del = this.repo.findOne(id); if (del == null) { ErrorMessage err = new ErrorMessage("License does not exist"); return ResponseEntity.ok(err); } this.repo.delete(del); Message success = new Message("Deleted License"); return ResponseEntity.ok(success); } private Boolean isLicenseExpired(License l) { Calendar today = Calendar.getInstance(); if (l.getExpires() == null) { return true; } else { return today.after(l.getExpires()); } } private Date generateExpireDate() { Calendar cal = Calendar.getInstance(); cal.add(Calendar.YEAR, 1); return cal.getTime(); } }