public void removeVariant(Variant variant) { if (variant != null) { selected.getVariants().remove(variant); // If there is only one variant left, reset its name/code and make it the default if (selected.getVariants().size() == 1) { Variant v = selected.getDefaultVariant(); v.setName(""); v.setDefaultChoice(true); } } }
/** * Utility that deletes any temporary uploads that were performed during editing, but were not * saved. * * @throws IOException if there was a problem reading the uploads directory or deleting any * temporary uploads */ private void deleteTempUploads() throws IOException { if (selected != null) { for (Variant variant : selected.getVariants()) { Collection<File> tmpFiles = FileUtils.listFiles( UploadsServlet.getUploadsDirectory(), FileFilterUtils.prefixFileFilter("tmp-" + variant.getUuid()), null); for (File tmpFile : tmpFiles) { FileUtils.forceDelete(tmpFile); } } } }
/** * JSF method to upload a new image file. * * @param uploadedImage a PrimeFaces upload */ public void setUploadedImage(final UploadedFile uploadedImage) { BufferedInputStream in = null; BufferedOutputStream out = null; try { // Determine if upload has a valid MIME type if (!uploadedImage.getContentType().startsWith("image")) { throw new IOException("Not a valid image type"); } // Determine temporary image name String ext = FilenameUtils.getExtension(uploadedImage.getFileName()); currentVariant.setItemImage( "tmp-" + currentVariant.getUuid() + "-" + new Date().getTime() + "." + ext); File path = new File(UploadsServlet.getUploadsDirectory(), currentVariant.getItemImage()); // Write file contents to disk in = new BufferedInputStream(uploadedImage.getInputstream(), BUFFER_SIZE); out = new BufferedOutputStream(new FileOutputStream(path), BUFFER_SIZE); byte[] buffer = new byte[BUFFER_SIZE]; int length = -1; while ((length = in.read(buffer)) >= 0) { out.write(buffer, 0, length); } uploadedImages.put(currentVariant, uploadedImage); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { // Close streams if (in != null) { in.close(); } if (out != null) { out.flush(); out.close(); } } catch (IOException e) { // Ignore, only closing streams } } }
public void addVariant() { Variant v = new Variant(); // If this is not the first variant, re-use price, weight and stock values if (selected.getVariants().size() > 0) { Variant prev = selected.getVariantsAsList().get(selected.getVariants().size() - 1); v.setPrice(prev.getPrice()); v.setWeight(prev.getWeight()); v.setStock(prev.getStock()); v.setDefaultChoice(false); } else { v.setName(""); v.setDefaultChoice(true); } selected.getVariants().add(v); }