001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.uif.element;
017
018import org.kuali.rice.krad.datadictionary.parse.BeanTag;
019import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
020import org.kuali.rice.krad.uif.UifConstants;
021import org.kuali.rice.krad.uif.container.CollectionGroup;
022import org.kuali.rice.krad.uif.util.LifecycleElement;
023import org.kuali.rice.krad.uif.util.UrlInfo;
024import org.kuali.rice.krad.web.form.UifFormBase;
025
026/**
027 * A content element that will display the collection configured with a file upload button.
028 *
029 * <p>This is used to list objects that represent files.
030 * The file will be uploaded to the methodToCall specified and the response is expected to be the refreshed collection.
031 * </p>
032 *
033 * @author Kuali Rice Team (rice.collab@kuali.org)
034 */
035@BeanTag(name = "multiFileUploadCollection", parent = "Uif-MultiFileUploadCollection")
036public class MultiFileUploadCollection extends ContentElementBase {
037    private static final long serialVersionUID = 6324034860109503990L;
038
039    private CollectionGroup collection;
040
041    private String methodToCall;
042    private UrlInfo url;
043
044    private String addFilesButtonText;
045
046    private String acceptFileTypes;
047    private Integer maxFileSize;
048    private Integer minFileSize;
049
050    private String propertyPath;
051
052    /**
053     * This finalize method adds template options to the templateOptions property based on settings in the
054     * parameters of this class
055     *
056     * {@inheritDoc}
057     */
058    @Override
059    public void performFinalize(Object model, LifecycleElement parent) {
060        super.performFinalize(model, parent);
061
062        UifFormBase form = (UifFormBase) model;
063
064        // Set plugin convenience setters into templateOptions to be consumed by the plugin
065        if (url == null && methodToCall != null) {
066            getTemplateOptions().put(UifConstants.MultiFileUploadOptions.URL,
067                    "?methodToCall=" + methodToCall + "&formKey=" + form.getFormKey() + "&viewId=" + form.getViewId()
068                            + "&bindingPath=" + collection.getBindingInfo().getBindingPath()
069                            + "&updateComponentId=" + collection.getId() );
070        } else if (url != null) {
071            getTemplateOptions().put(UifConstants.MultiFileUploadOptions.URL, url.getHref());
072        }
073
074        if (acceptFileTypes != null) {
075            getTemplateOptions().put(UifConstants.MultiFileUploadOptions.ACCEPT_FILE_TYPES, acceptFileTypes);
076        }
077
078        if (maxFileSize != null) {
079            getTemplateOptions().put(UifConstants.MultiFileUploadOptions.MAX_SIZE, maxFileSize.toString());
080        }
081
082        if (minFileSize != null) {
083            getTemplateOptions().put(UifConstants.MultiFileUploadOptions.MIN_SIZE, minFileSize.toString());
084        }
085
086        this.propertyPath = collection.getBindingInfo().getBindingPath();
087
088        // Make collection inherit readOnly
089        this.collection.setReadOnly(this.getReadOnly());
090    }
091
092    /**
093     * The collection which holds the files uploaded
094     *
095     * @return the collection
096     */
097    @BeanTagAttribute
098    public CollectionGroup getCollection() {
099        return collection;
100    }
101
102    /**
103     * @see MultiFileUploadCollection#getCollection()
104     */
105    public void setCollection(CollectionGroup collection) {
106        this.collection = collection;
107    }
108
109    /**
110     * The methodToCall for uploading files, this methodToCall must return the refreshed collection
111     *
112     * @return the methodToCall for file uploads
113     */
114    @BeanTagAttribute
115    public String getMethodToCall() {
116        return methodToCall;
117    }
118
119    /**
120     * @see MultiFileUploadCollection#getMethodToCall()
121     */
122    public void setMethodToCall(String methodToCall) {
123        this.methodToCall = methodToCall;
124    }
125
126    /**
127     * The url override for file uploads, this will be used instead of the methodToCall, if set, as the url to post
128     * the file upload to
129     *
130     * @return the file upload url configuration override
131     */
132    @BeanTagAttribute
133    public UrlInfo getUrl() {
134        return url;
135    }
136
137    /**
138     * @see MultiFileUploadCollection#getUrl()
139     */
140    public void setUrl(UrlInfo url) {
141        this.url = url;
142    }
143
144    /**
145     * The text to be used on the add files button
146     *
147     * @return the text of the add files button
148     */
149    @BeanTagAttribute
150    public String getAddFilesButtonText() {
151        return addFilesButtonText;
152    }
153
154    /**
155     * @see MultiFileUploadCollection#getAddFilesButtonText()
156     */
157    public void setAddFilesButtonText(String addFilesButtonText) {
158        this.addFilesButtonText = addFilesButtonText;
159    }
160
161    /**
162     * A regex used to allow or disallow a certain file types for this file upload component
163     *
164     * @return the regex for file upload verification
165     */
166    @BeanTagAttribute
167    public String getAcceptFileTypes() {
168        return acceptFileTypes;
169    }
170
171    /**
172     * @see MultiFileUploadCollection#getAcceptFileTypes()
173     */
174    public void setAcceptFileTypes(String acceptFileTypes) {
175        this.acceptFileTypes = acceptFileTypes;
176    }
177
178    /**
179     * The maximum file size to allow (in bytes) for a file upload
180     *
181     * @return maximum file size in bytes
182     */
183    @BeanTagAttribute
184    public Integer getMaxFileSize() {
185        return maxFileSize;
186    }
187
188    /**
189     * @see MultiFileUploadCollection#getMaxFileSize()
190     */
191    public void setMaxFileSize(Integer maxFileSize) {
192        this.maxFileSize = maxFileSize;
193    }
194
195    /**
196     * The minimum file size needed (in bytes) for a file to be uploaded
197     *
198     * @return minimum file size in bytes
199     */
200    @BeanTagAttribute
201    public Integer getMinFileSize() {
202        return minFileSize;
203    }
204
205    /**
206     * @see MultiFileUploadCollection#getMinFileSize()
207     */
208    public void setMinFileSize(Integer minFileSize) {
209        this.minFileSize = minFileSize;
210    }
211
212    /**
213     * The property path used for this component, which is the binding path of collection
214     *
215     * @return the property path
216     */
217    public String getPropertyPath() {
218        return propertyPath;
219    }
220}