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.AlternateName;
37  import com.gisgraphy.domain.geoloc.entity.GisFeature;
38  import com.gisgraphy.domain.repository.IAlternateNameDao;
39  import com.gisgraphy.domain.repository.IGisFeatureDao;
40  import com.gisgraphy.domain.repository.ISolRSynchroniser;
41  import com.gisgraphy.domain.valueobject.AlternateNameSource;
42  import com.gisgraphy.helper.StringHelper;
43  import com.gisgraphy.service.IInternationalisationService;
44  import com.opensymphony.xwork2.ActionContext;
45  import com.opensymphony.xwork2.Preparable;
46  
47  /**
48   * Edit AlternateName action (crud) specially in ajax
49   * 
50   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
51   */
52  public class EditAlternateNameAction extends BaseAction implements Preparable {
53  
54      private static final long serialVersionUID = 3331811022397633034L;
55  
56      private static Logger logger = LoggerFactory.getLogger(EditAlternateNameAction.class);
57  
58      private IAlternateNameDao alternateNameDao;
59  
60      private IGisFeatureDao gisFeatureDao;
61  
62      private AlternateName alternatename;
63  
64      private IInternationalisationService internationalisationService;
65  
66      private ISolRSynchroniser solRSynchroniser;
67      /**
68       * The transaction manager
69       */
70      private PlatformTransactionManager transactionManager;
71  
72      private TransactionStatus txStatus = null;
73  
74      private DefaultTransactionDefinition txDefinition;
75  
76      private Long id;
77  
78      private Long gisFeatureId;
79  
80      /*
81       * Those specific fields needs to be process separately because of decimal
82       * separator or enum type
83       */
84      private String errorMessage;
85      private String stackTrace;
86  
87      public void prepare() {
88  	// we have to test httpparameter because prepare is called before the
89  	// setters are called
90  	HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
91  	String parameter = request.getParameter("id");
92  	if (parameter != null && !parameter.equals("")) {
93  	    Long idAsLong = null;
94  	    try {
95  		idAsLong = Long.parseLong(parameter);
96  	    } catch (NumberFormatException e) {
97  		errorMessage = "id should be numeric";
98  		logger.error(errorMessage);
99  	    }
100 	    id = idAsLong;
101 	}
102 	if (id != null) {
103 	    alternatename = alternateNameDao.get(getId());
104 	}
105     }
106 
107     public String persist() {
108 	return doPersist();
109     }
110 
111     public String doPersist() {
112 	if (alternatename != null) {
113 	    if (alternatename.getGisFeature()!=null){
114 		setGisFeatureId(alternatename.getGisFeature().getFeatureId());
115 	    }
116 	    GisFeature gisfeature = gisFeatureDao.getByFeatureId(getGisFeatureId());
117 	    if (gisfeature == null) {
118 		addFieldError("gisfeatureId", "no gisfeature to be link to the alternatename");
119 		return ERROR;
120 	    }
121 	    checkMissingRequiredfields();
122 	    alternatename.setSource(AlternateNameSource.PERSONAL);
123 	    if (getFieldErrors().keySet().size() > 0) {
124 		return ERROR;
125 	    } else {
126 		if (alternatename.getId() != null) {
127 		    // it is an update
128 		    startTransaction();
129 		    try {
130 			gisfeature.getAlternateNames().remove(alternatename);
131 			gisfeature.addAlternateName(alternatename);
132 			gisFeatureDao.save(gisfeature);
133 		    } catch (Exception e) {
134 			rollbackTransaction();
135 			errorMessage = "could not save alternateName " + e.getMessage();
136 			stackTrace = StringHelper.getStackTraceAsString(e);
137 			logger.error(errorMessage, e);
138 			return ERROR;
139 		    }
140 		    commitTransaction();
141 		    return SUCCESS;
142 		} else {
143 		    // it is a creation
144 			gisfeature.addAlternateName(alternatename);
145 			startTransaction();
146 			try {
147 			    gisFeatureDao.save(gisfeature);
148 			} catch (Exception e) {
149 			    rollbackTransaction();
150 			    errorMessage = "could not save alternateName " + e.getMessage();
151 			    stackTrace = StringHelper.getStackTraceAsString(e);
152 			    logger.error(errorMessage, e);
153 			    return ERROR;
154 			}
155 			commitTransaction();
156 			return SUCCESS;
157 		    }
158 	    }
159 	} else {
160 	    errorMessage = "There is no alternatename to save";
161 	    logger.error(errorMessage);
162 	    return ERROR;
163 	}
164     }
165 
166     private void checkMissingRequiredfields() {
167 	if (alternatename != null) {
168 	    if (alternatename.getName() == null) {
169 		addFieldError("name", internationalisationService.getString("errors.required", new String[] { "name" }));
170 	    }
171 	}
172     }
173 
174     private void commitTransaction() {
175 	transactionManager.commit(txStatus);
176 	solRSynchroniser.commit();
177     }
178 
179     private void rollbackTransaction() {
180 	transactionManager.rollback(txStatus);
181     }
182 
183     private void startTransaction() {
184 	txDefinition = new DefaultTransactionDefinition();
185 	txDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
186 	txDefinition.setReadOnly(false);
187 
188 	txStatus = transactionManager.getTransaction(txDefinition);
189     }
190 
191     public Long getId() {
192 	return id;
193     }
194 
195     public void setId(Long id) {
196 	this.id = id;
197     }
198 
199     public String doDelete() {
200 	if (alternatename != null) {
201 	    GisFeature feature = gisFeatureDao.getByFeatureId(alternatename.getGisFeature().getFeatureId());
202 	    feature.getAlternateNames().remove(alternatename);
203 	   // alternateName.setGisFeature(null);
204 
205 	    startTransaction();
206 	    try {
207 
208 		gisFeatureDao.save(feature);
209 		alternateNameDao.remove(alternatename);
210 		gisFeatureDao.flushAndClear();
211 	    } catch (Exception e) {
212 		logger.error("Can not delete the alternatename : " + e.getMessage(), e);
213 		stackTrace = StringHelper.getStackTraceAsString(e);
214 		rollbackTransaction();
215 		return ERROR;
216 	    }
217 	    commitTransaction();
218 	    return SUCCESS;
219 	} else {
220 	    errorMessage = "there is no entity to delete";
221 	    return ERROR;
222 	}
223     }
224     
225     /**
226      * @return the errorMessage
227      */
228     public String getErrorMessage() {
229 	return errorMessage;
230     }
231 
232     /**
233      * @param errorMessage
234      *            the errorMessage to set
235      */
236     public void setErrorMessage(String errorMessage) {
237 	this.errorMessage = errorMessage;
238     }
239 
240     /**
241      * @return the stackTrace
242      */
243     public String getStackTrace() {
244 	return stackTrace;
245     }
246 
247     /**
248      * @param stackTrace
249      *            the stackTrace to set
250      */
251     public void setStackTrace(String stackTrace) {
252 	this.stackTrace = stackTrace;
253     }
254 
255 
256     public AlternateName getAlternatename() {
257 	return alternatename;
258     }
259 
260     public void setAlternatename(AlternateName alternateName) {
261 	this.alternatename = alternateName;
262     }
263 
264     public Long getGisFeatureId() {
265 	return gisFeatureId;
266     }
267 
268     public void setGisFeatureId(Long gisFeatureId) {
269 	this.gisFeatureId = gisFeatureId;
270     }
271 
272     @Required
273     public void setGisFeatureDao(IGisFeatureDao gisFeatureDao) {
274 	this.gisFeatureDao = gisFeatureDao;
275     }
276     
277     @Required
278     public void setTransactionManager(PlatformTransactionManager transactionManager) {
279 	this.transactionManager = transactionManager;
280     }
281     
282     @Required
283     public void setInternationalisationService(IInternationalisationService internationalisationService) {
284 	this.internationalisationService = internationalisationService;
285     }
286     
287     @Required
288     public void setSolRSynchroniser(ISolRSynchroniser solRSynchroniser) {
289 	this.solRSynchroniser = solRSynchroniser;
290     }
291     
292     @Required
293     public void setAlternateNameDao(IAlternateNameDao alternateNameDao) {
294 	this.alternateNameDao = alternateNameDao;
295     }
296 
297 }