View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.webapp.action;
24  
25  import javax.servlet.http.HttpServletRequest;
26  
27  import org.apache.struts2.ServletActionContext;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.springframework.beans.factory.annotation.Required;
31  import org.springframework.transaction.PlatformTransactionManager;
32  import org.springframework.transaction.TransactionDefinition;
33  import org.springframework.transaction.TransactionStatus;
34  import org.springframework.transaction.support.DefaultTransactionDefinition;
35  
36  import com.gisgraphy.domain.geoloc.entity.GisFeature;
37  import com.gisgraphy.domain.geoloc.entity.ZipCode;
38  import com.gisgraphy.domain.repository.IGisFeatureDao;
39  import com.gisgraphy.domain.repository.ISolRSynchroniser;
40  import com.gisgraphy.domain.repository.IZipCodeDao;
41  import com.gisgraphy.helper.StringHelper;
42  import com.gisgraphy.service.IInternationalisationService;
43  import com.opensymphony.xwork2.ActionContext;
44  import com.opensymphony.xwork2.Preparable;
45  
46  /**
47   * Edit ZipCode action (crud) specially in ajax
48   * 
49   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
50   */
51  public class EditZipCodeAction extends BaseAction implements Preparable {
52  
53      private static final long serialVersionUID = -7493269463137268011L;
54  
55      private static Logger logger = LoggerFactory.getLogger(EditZipCodeAction.class);
56  
57      private IZipCodeDao zipCodeDao;
58  
59      private IGisFeatureDao gisFeatureDao;
60  
61      private ZipCode zipCode;
62  
63      private IInternationalisationService internationalisationService;
64  
65      private ISolRSynchroniser solRSynchroniser;
66      /**
67       * The transaction manager
68       */
69      private PlatformTransactionManager transactionManager;
70  
71      private TransactionStatus txStatus = null;
72  
73      private DefaultTransactionDefinition txDefinition;
74  
75      private Long id;
76  
77      private Long gisFeatureId;
78  
79      /*
80       * Those specific fields needs to be process separately because of decimal
81       * separator or enum type
82       */
83      private String errorMessage;
84      private String stackTrace;
85  
86      public void prepare() {
87  	// we have to test httpparameter because prepare is called before the
88  	// setters are called
89  	HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
90  	String parameter = request.getParameter("id");
91  	if (parameter != null && !parameter.equals("")) {
92  	    Long idAsLong = null;
93  	    try {
94  		idAsLong = Long.parseLong(parameter);
95  	    } catch (NumberFormatException e) {
96  		errorMessage = "id should be numeric";
97  		logger.error(errorMessage);
98  	    }
99  	    id = idAsLong;
100 	}
101 	if (id != null) {
102 	    zipCode = zipCodeDao.get(getId());
103 	}
104     }
105 
106     public String persist() {
107 	return doPersist();
108     }
109 
110     public String doPersist() {
111 	if (zipCode != null) {
112 	    if (zipCode.getGisFeature()!=null){
113 		setGisFeatureId(zipCode.getGisFeature().getFeatureId());
114 	    }
115 	    GisFeature gisfeature = gisFeatureDao.getByFeatureId(getGisFeatureId());
116 	    if (gisfeature == null) {
117 		addFieldError("gisfeatureId", "no gisfeature to be link to the zipCode");
118 		return ERROR;
119 	    }
120 	    checkMissingRequiredfields();
121 	    if (getFieldErrors().keySet().size() > 0) {
122 		return ERROR;
123 	    } else {
124 		if (zipCode.getId() != null) {
125 		    // it is an update
126 		    startTransaction();
127 		    try {
128 			gisfeature.getZipCodes().remove(zipCode);
129 			gisfeature.addZipCode(zipCode);
130 			gisFeatureDao.save(gisfeature);
131 		    } catch (Exception e) {
132 			rollbackTransaction();
133 			errorMessage = "could not save zipCode " + e.getMessage();
134 			stackTrace = StringHelper.getStackTraceAsString(e);
135 			logger.error(errorMessage, e);
136 			return ERROR;
137 		    }
138 		    commitTransaction();
139 		    return SUCCESS;
140 		} else {
141 		    // it is a creation
142 			gisfeature.addZipCode(zipCode);
143 			startTransaction();
144 			try {
145 			    gisFeatureDao.save(gisfeature);
146 			} catch (Exception e) {
147 			    rollbackTransaction();
148 			    errorMessage = "could not save zipCode " + e.getMessage();
149 			    stackTrace = StringHelper.getStackTraceAsString(e);
150 			    logger.error(errorMessage, e);
151 			    return ERROR;
152 			}
153 			commitTransaction();
154 			return SUCCESS;
155 		    }
156 	    }
157 	} else {
158 	    errorMessage = "There is no zipCode to save";
159 	    logger.error(errorMessage);
160 	    return ERROR;
161 	}
162     }
163 
164     private void checkMissingRequiredfields() {
165 	if (zipCode != null) {
166 	    if (zipCode.getCode() == null) {
167 		addFieldError("code", internationalisationService.getString("errors.required", new String[] { "code" }));
168 	    }
169 	}
170     }
171 
172     private void commitTransaction() {
173 	transactionManager.commit(txStatus);
174 	solRSynchroniser.commit();
175     }
176 
177     private void rollbackTransaction() {
178 	transactionManager.rollback(txStatus);
179     }
180 
181     private void startTransaction() {
182 	txDefinition = new DefaultTransactionDefinition();
183 	txDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
184 	txDefinition.setReadOnly(false);
185 
186 	txStatus = transactionManager.getTransaction(txDefinition);
187     }
188 
189     public Long getId() {
190 	return id;
191     }
192 
193     public void setId(Long id) {
194 	this.id = id;
195     }
196 
197     public String doDelete() {
198 	if (zipCode != null) {
199 	    GisFeature feature = gisFeatureDao.getByFeatureId(zipCode.getGisFeature().getFeatureId());
200 	    feature.getZipCodes().remove(zipCode);
201 	   // alternateName.setGisFeature(null);
202 
203 	    startTransaction();
204 	    try {
205 
206 		gisFeatureDao.save(feature);
207 		zipCodeDao.remove(zipCode);
208 		gisFeatureDao.flushAndClear();
209 	    } catch (Exception e) {
210 		logger.error("Can not delete zipcode : " + e.getMessage(), e);
211 		stackTrace = StringHelper.getStackTraceAsString(e);
212 		rollbackTransaction();
213 		return ERROR;
214 	    }
215 	    commitTransaction();
216 	    return SUCCESS;
217 	} else {
218 	    errorMessage = "there is no entity to delete";
219 	    return ERROR;
220 	}
221     }
222     
223     /**
224      * @return the errorMessage
225      */
226     public String getErrorMessage() {
227 	return errorMessage;
228     }
229 
230     /**
231      * @param errorMessage
232      *            the errorMessage to set
233      */
234     public void setErrorMessage(String errorMessage) {
235 	this.errorMessage = errorMessage;
236     }
237 
238     /**
239      * @return the stackTrace
240      */
241     public String getStackTrace() {
242 	return stackTrace;
243     }
244 
245     /**
246      * @param stackTrace
247      *            the stackTrace to set
248      */
249     public void setStackTrace(String stackTrace) {
250 	this.stackTrace = stackTrace;
251     }
252 
253     public Long getGisFeatureId() {
254 	return gisFeatureId;
255     }
256 
257     public void setGisFeatureId(Long gisFeatureId) {
258 	this.gisFeatureId = gisFeatureId;
259     }
260 
261     public ZipCode getZipCode() {
262         return zipCode;
263     }
264 
265     public void setZipCode(ZipCode zipCode) {
266         this.zipCode = zipCode;
267     }
268 
269     @Required
270     public void setGisFeatureDao(IGisFeatureDao gisFeatureDao) {
271 	this.gisFeatureDao = gisFeatureDao;
272     }
273     
274     @Required
275     public void setTransactionManager(PlatformTransactionManager transactionManager) {
276 	this.transactionManager = transactionManager;
277     }
278     
279     @Required
280     public void setInternationalisationService(IInternationalisationService internationalisationService) {
281 	this.internationalisationService = internationalisationService;
282     }
283     
284     @Required
285     public void setSolRSynchroniser(ISolRSynchroniser solRSynchroniser) {
286 	this.solRSynchroniser = solRSynchroniser;
287     }
288     
289     @Required
290     public void setZipCodeDao(IZipCodeDao zipCodeDao) {
291 	this.zipCodeDao = zipCodeDao;
292     }
293     
294 
295    
296 
297 }