View Javadoc

1   /***
2   Copyright (C) 2005 The Java Community
3   
4   This program is free software; you can redistribute it and/or modify  it under
5   the terms of the GNU General Public License as published by  the Free Software
6   Foundation; either version 2 of the License, or  (at your option) any later
7   version.
8   
9   This program is distributed in the hope that it will be useful,  but WITHOUT
10  ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS
11  FOR A PARTICULAR PURPOSE. See the  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License  along with
14  this program; if not, write to the Free Software  Foundation, Inc., 59 Temple
15  Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17  package org.bejug.javacareers.common.view.jsf.utils;
18  
19  import java.io.File;
20  
21  import javax.faces.context.ExternalContext;
22  import javax.faces.context.FacesContext;
23  import javax.servlet.ServletContext;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.bejug.javacareers.common.util.FileUtilities;
28  import org.bejug.javacareers.jobs.model.Interview;
29  import org.bejug.javacareers.jobs.model.Item;
30  import org.bejug.javacareers.jobs.model.Resume;
31  import org.bejug.javacareers.jobs.search.lucene.PdfException;
32  import org.bejug.javacareers.jobs.service.LucenePdfService;
33  import org.bejug.javacareers.jobs.view.jsf.action.ResumeAction;
34  
35  /***
36   *
37   * @author Sven Schauwvliege (Last modified by $Author: shally $)
38   * @version $Revision: 1.6 $ - $Date: 2005/12/21 11:38:42 $
39   * 
40   * deletes ths files that some items have
41   * i rather like to see this in the dao but for now we dont find a solution for the problem with the paths
42   */
43  
44  
45  public class ItemFileRemover {
46  	
47  	 /***
48       * The ManageResume CommentAction logger.
49       */
50      private static final Log LOG = LogFactory.getLog(ResumeAction.class);
51      
52      /***
53       * Todo: a web controller should not use the pdf service directly 
54       * the ResumeService should use this instead
55       * The Job Service reference, injected by Spring container.
56       */
57      private LucenePdfService lucenePdfService;
58  
59  	/***
60  	 * @return the servlet context
61  	 */
62  	private ServletContext getServletContext() {
63  		FacesContext facesContext = FacesContext.getCurrentInstance();
64  		ExternalContext externalContext = facesContext.getExternalContext();
65  		ServletContext servletContext = (ServletContext) externalContext
66  				.getContext();
67  		return servletContext;
68  	}
69  	
70  	
71  	/***
72  	 * deletes the files this item has
73  	 * @param item is the item selected
74  	 */
75  	public void deleteFiles(Item item){
76  		if (item instanceof Interview){
77  			Interview interview = (Interview)item;
78  			ServletContext servletContext = getServletContext();
79  			if (interview.getPictureUrl() != null){
80  				FileUtilities.deleteUploadedFile(new File(servletContext.getRealPath(interview.getPictureUrl())));
81  			}
82  			if (interview.getInterviewUrl() != null){
83  				FileUtilities.deleteUploadedFile(new File(servletContext.getRealPath(interview.getInterviewUrl())));
84  			}
85  			if (interview.getQueueUrl() != null){
86  				FileUtilities.deleteUploadedFile(new File(servletContext.getRealPath(interview.getQueueUrl())));
87  			}
88  		}else if (item instanceof Resume){
89  			 LOG.info("Debug: -- executing deleteResume");
90  			 ServletContext servletContext = getServletContext();
91  			 Resume resume = (Resume)item;
92  			 FileUtilities.deleteUploadedFile(new File(servletContext.getRealPath(resume.getPdfUrl())));
93  		      
94  		            File file = new File(resume.getPdfUrl());
95  		            if (file != null && file.exists()) {
96  		                try {
97  		                    lucenePdfService.removeFromIndex(file.getAbsolutePath());
98  		                } catch (PdfException ex) {
99  		                    LOG.error(ex);          
100 		                }
101 		            } else {
102 		                LOG.error("error loading resume: " + resume.getPdfUrl());
103 		            }
104 		      
105 		}	
106 	}
107 
108 
109 	/***
110 	 * @return Returns the lucenePdfService.
111 	 */
112 	public LucenePdfService getLucenePdfService() {
113 		return lucenePdfService;
114 	}
115 
116 
117 	/***
118 	 * @param lucenePdfService The lucenePdfService to set.
119 	 */
120 	public void setLucenePdfService(LucenePdfService lucenePdfService) {
121 		this.lucenePdfService = lucenePdfService;
122 	}
123 }
124 /*** 
125  * $Log: ItemFileRemover.java,v $
126  * Revision 1.6  2005/12/21 11:38:42  shally
127  * *** empty log message ***
128  *
129  * Revision 1.5  2005/12/20 15:36:47  shally
130  * CheckStyle and PMD changes.
131  *
132  * Revision 1.4  2005/12/08 14:53:46  shally
133  * Opkuis voor checkstyle.
134  *
135  * Revision 1.3  2005/09/30 14:38:07  bavo_jcs
136  * Fixed URL
137  *
138  * Revision 1.2  2005/09/24 17:30:14  schauwvliege
139  * delete content, refactory of resume
140  *
141  * Revision 1.1  2005/09/23 07:29:47  schauwvliege
142  * delete content refactory for interview
143  * 
144  **/