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   * 
20   *   David Masclet <davidmasclet@gisgraphy.com>
21   ******************************************************************************/
22  package com.gisgraphy.domain.repository;
23  
24  import java.util.concurrent.atomic.AtomicLong;
25  
26  import javax.annotation.PostConstruct;
27  
28  import org.springframework.beans.factory.annotation.Required;
29  import org.springframework.stereotype.Service;
30  
31  /**
32   * Convenience class to generate if (for openstreetmap, geonames) when they are
33   * not provided, in case of custom add. this can be used in multithreading
34   * 
35   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
36   * 
37   */
38  @Service
39  public class IdGenerator implements IIdGenerator {
40  
41  
42      /**
43       * shift value to allow the addition of geonames features after import
44       */ 
45      public final static long FEATUREID_INCREMENT_NO_CONFLICT = 20000000;
46      
47  
48      /**
49       * shift value to allow the addition of openstreetmap features after import
50       */ 
51      public final static long OPENSTREETMAP_GID_NO_CONFLICT = 100000000;
52      
53     
54  	
55  	private IGisFeatureDao gisFeatureDao;
56  	
57  	private IOpenStreetMapDao openStreetMapDao;
58  	
59  	/* (non-Javadoc)
60  	 * @see com.gisgraphy.domain.repository.IIdGenerator#getNextFeatureId()
61  	 */
62  	public long getNextFeatureId() {
63  		return featureId.incrementAndGet();
64  	}
65  	
66  	
67  	
68  	/* (non-Javadoc)
69  	 * @see com.gisgraphy.domain.repository.IIdGenerator#getNextGId()
70  	 */
71  	public synchronized long getNextGId() {
72  		return openstreetmapGid.incrementAndGet();
73  	}
74  	
75  	/* (non-Javadoc)
76  	 * @see com.gisgraphy.domain.repository.IIdGenerator#getNextFeatureId()
77  	 */
78  	public long getFeatureId() {
79  		return featureId.get();
80  	}
81  	
82  	
83  	
84  	
85  	/* (non-Javadoc)
86  	 * @see com.gisgraphy.domain.repository.IIdGenerator#getNextGId()
87  	 */
88  	public long getGid() {
89  		return openstreetmapGid.get();
90  	}
91  
92  	private AtomicLong featureId ;
93  	
94  	
95  	private AtomicLong openstreetmapGid;
96  
97  	@PostConstruct
98  	public void sync() {
99  		//order is important
100 		initFeatureId();
101 		initGid();
102 	}
103 
104 
105 	private void initGid() {
106 		long maxGidInDatabase = openStreetMapDao.getMaxGid();
107 		if (maxGidInDatabase<OPENSTREETMAP_GID_NO_CONFLICT){
108 			maxGidInDatabase= OPENSTREETMAP_GID_NO_CONFLICT;
109 		}
110 		openstreetmapGid = new AtomicLong(Math.max(featureId.get(), maxGidInDatabase));
111 		
112 	}
113 
114 
115 
116 
117 	private void initFeatureId() {
118 		long maxFeatureIdInDatabase = gisFeatureDao.getMaxFeatureId();
119 		if(maxFeatureIdInDatabase<FEATUREID_INCREMENT_NO_CONFLICT){
120 		    maxFeatureIdInDatabase = FEATUREID_INCREMENT_NO_CONFLICT;
121 		}
122 		featureId = new AtomicLong(maxFeatureIdInDatabase);
123 	}
124 
125 
126 	/**
127 	 * @param gisFeatureDao the gisFeatureDao to set
128 	 */
129 	@Required
130 	public void setGisFeatureDao(IGisFeatureDao gisFeatureDao) {
131 		this.gisFeatureDao = gisFeatureDao;
132 	}
133 
134 
135 	/**
136 	 * @param openStreetMapDao the openStreetMapDao to set
137 	 */
138 	@Required
139 	public void setOpenStreetMapDao(IOpenStreetMapDao openStreetMapDao) {
140 		this.openStreetMapDao = openStreetMapDao;
141 	}
142 
143 }