001 /*
002 * Copyright 2011 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 */
016 package org.kuali.kfs.sys.batch;
017
018 import java.util.Calendar;
019 import java.util.Date;
020
021 import org.kuali.kfs.sys.KFSConstants;
022 import org.kuali.rice.kns.service.AttachmentService;
023
024
025 /**
026 * This class is the batch step used to delete pending attachments that have not yet been associated with a document. When
027 * attachments are attached to docs in an "initiated" state, the attachment is not considered to be linked with the doc in the
028 * persistence layer, and so it becomes a "pending" attachment (i.e. pending persistence). When docs are saved (or submitted, etc.),
029 * pending attachments become permanently persisted to the document and are no longer pending. Pending attachments may have become
030 * orphaned from the document (because the doc has not been saved), and so these orphaned attachments must be deleted. This job uses
031 * the file's last modified time to determine which pending attachments should be deleted. If the modified time is older than the
032 * SYSTEM parameter "pendingAssignmentMaxAge", then it will be deleted.
033 *
034 * @see org.kuali.rice.kns.service.impl.AttachmentServiceImpl
035 * @see KFSConstants.SystemGroupParameterNames#PURGE_PENDING_ATTACHMENTS_STEP_MAX_AGE
036 */
037 public class PurgePendingAttachmentsStep extends AbstractStep {
038
039 private AttachmentService attachmentService;
040
041 /**
042 * Deletes all pending attachments that are older than a configured time (see class description)
043 *
044 * @see org.kuali.kfs.sys.batch.Step#execute(String, Date)
045 */
046 public boolean execute(String jobName, Date jobRunDate) {
047 Calendar calendar = getDateTimeService().getCurrentCalendar();
048 String maxAgeInSecondsStr = getParameterService().getParameterValue(PurgePendingAttachmentsStep.class, KFSConstants.SystemGroupParameterNames.PURGE_PENDING_ATTACHMENTS_STEP_MAX_AGE);
049 int maxAgeInSeconds = Integer.parseInt(maxAgeInSecondsStr);
050 calendar.add(Calendar.SECOND, -maxAgeInSeconds);
051 getAttachmentService().deletePendingAttachmentsModifiedBefore(calendar.getTimeInMillis());
052 return true;
053 }
054
055 /**
056 * Gets the attachmentService attribute.
057 *
058 * @return Returns the attachmentService.
059 */
060 public AttachmentService getAttachmentService() {
061 return attachmentService;
062 }
063
064 /**
065 * Sets the attachmentService attribute value.
066 *
067 * @param attachmentService The attachmentService to set.
068 */
069 public void setAttachmentService(AttachmentService attachmentService) {
070 this.attachmentService = attachmentService;
071 }
072 }