001 /*
002 * Databinder: a simple bridge from Wicket to Hibernate
003 * Copyright (C) 2006 Nathan Hamblen nathan@technically.us
004
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either
008 * version 2.1 of the License, or (at your option) any later version.
009 *
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013 * Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public
016 * License along with this library; if not, write to the Free Software
017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
018 */
019 package net.databinder.components.hib;
020
021 import java.io.IOException;
022
023 import org.hibernate.Hibernate;
024
025 import org.apache.wicket.markup.html.form.upload.FileUploadField;
026 import org.apache.wicket.model.IModel;
027
028 /**
029 * Extension of FileUploadField optimized for blob resources. This upload field can be
030 * bound to an object with a compound property model where the property corresponds
031 * to the destination blob setter. This allows for file uploads with no specific code for each
032 * upload component.
033 * @author Nathan Hamblen
034 */
035 public class BlobUploadField extends FileUploadField {
036 /**
037 * Costructor to be used with compound property model.
038 * @param id component id, should resolve to a stream property
039 */
040 public BlobUploadField(String id) {
041 super(id);
042 }
043
044 /**
045 * Constructor to be used with PropertyModel or other model.
046 * @param id component id
047 * @param model should resolve to a stream setter
048 */
049 public BlobUploadField(String id, IModel model) {
050 super(id, model);
051 }
052
053 /**
054 * Converts the upload's inputstream to the resolved blob setter.
055 */
056 @Override
057 public void updateModel() {
058 try {
059 if (getFileUpload() != null) {
060 setModelObject(Hibernate.createBlob(getFileUpload().getInputStream()));
061 onUpdated();
062 }
063 } catch (IOException e) {
064 throw new RuntimeException(e);
065 }
066 }
067 protected void onUpdated() { }
068 }