/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.spring.services.dwr;
import java.beans.PropertyDescriptor;
import javax.servlet.http.HttpServletRequest;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.dwrp.CommonsFileUpload;
import org.directwebremoting.event.SessionProgressListener;
import org.directwebremoting.io.FileTransfer;
import org.internna.iwebmvc.dao.DAO;
import org.internna.iwebmvc.model.Document;
import org.internna.iwebmvc.model.DomainEntity;
import org.internna.iwebmvc.model.UUID;
import org.internna.iwebmvc.security.UserManager;
import org.internna.iwebmvc.utils.Assert;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
/**
* Uploads documents and monitors the progress.
*
* @author Jose Noheda
* @since 1.0
*/
@RemoteProxy(name = "UploadMonitor")
public class UploadMonitorImpl implements UploadMonitor {
protected DAO dao;
protected UserManager userManager;
public void setDao(DAO dao) {
this.dao = dao;
}
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
/**
* Returns the status of an upload in progress.
* @return a status bean
*/
@RemoteMethod
@Override public Object getStatus(HttpServletRequest request) {
return request.getSession().getAttribute(CommonsFileUpload.PROGRESS_LISTENER);
}
/**
* Cancels an upload in progress.
*/
@RemoteMethod
@Override public void cancelUpload(){
WebContextFactory.get().getSession().setAttribute(SessionProgressListener.CANCEL_UPLOAD, true);
}
/**
* Uploads a document using DWR.
*
* @param file the transferred data
*/
@RemoteMethod
@Override public Object upload(FileTransfer file) throws Exception {
DomainEntity e = Document.fromFileTransfer(userManager.getActiveUser(), file);
dao.create(e);
return e;
}
@RemoteMethod
@Transactional
@Override public void trash(Class<? extends DomainEntity> entityClass, UUID pk, String path) throws Exception {
Assert.notNull(entityClass);
if (pk != null) {
DomainEntity entity = dao.find(entityClass, pk);
if (entity != null) {
PropertyDescriptor property = BeanUtils.getPropertyDescriptor(entityClass, path);
if ((property != null) && (Document.class.isAssignableFrom(property.getPropertyType()))) {
Document doc = (Document) property.getReadMethod().invoke(entity);
if (doc != null) {
doc.setTemporal(true);
dao.update(doc);
}
}
}
}
}
}