|
Lines 12-21
Link Here
|
| 12 |
**********************************************************************/ |
12 |
**********************************************************************/ |
| 13 |
package org.eclipse.hyades.resources.database.internal.impl; |
13 |
package org.eclipse.hyades.resources.database.internal.impl; |
| 14 |
import java.sql.ResultSet; |
14 |
import java.sql.ResultSet; |
|
|
15 |
import java.sql.SQLException; |
| 15 |
import java.sql.Statement; |
16 |
import java.sql.Statement; |
|
|
17 |
import java.util.ArrayList; |
| 16 |
import java.util.Collection; |
18 |
import java.util.Collection; |
| 17 |
import java.util.Collections; |
19 |
import java.util.Collections; |
|
|
20 |
import java.util.Iterator; |
| 21 |
import java.util.List; |
| 18 |
|
22 |
|
|
|
23 |
import org.eclipse.emf.common.notify.Notification; |
| 19 |
import org.eclipse.emf.common.notify.NotificationChain; |
24 |
import org.eclipse.emf.common.notify.NotificationChain; |
| 20 |
import org.eclipse.emf.ecore.EAttribute; |
25 |
import org.eclipse.emf.ecore.EAttribute; |
| 21 |
import org.eclipse.emf.ecore.EClass; |
26 |
import org.eclipse.emf.ecore.EClass; |
|
Lines 32-38
Link Here
|
| 32 |
import org.eclipse.hyades.resources.database.internal.DBResource; |
37 |
import org.eclipse.hyades.resources.database.internal.DBResource; |
| 33 |
import org.eclipse.hyades.resources.database.internal.Database; |
38 |
import org.eclipse.hyades.resources.database.internal.Database; |
| 34 |
import org.eclipse.hyades.resources.database.internal.InternalDatabase; |
39 |
import org.eclipse.hyades.resources.database.internal.InternalDatabase; |
| 35 |
import org.eclipse.hyades.resources.database.internal.ReferenceQuery; |
|
|
| 36 |
import org.eclipse.hyades.resources.database.internal.extensions.DBCommandFactory; |
40 |
import org.eclipse.hyades.resources.database.internal.extensions.DBCommandFactory; |
| 37 |
import org.eclipse.hyades.resources.database.internal.extensions.IdsTypes; |
41 |
import org.eclipse.hyades.resources.database.internal.extensions.IdsTypes; |
| 38 |
import org.eclipse.hyades.resources.database.internal.extensions.JDBCHelper; |
42 |
import org.eclipse.hyades.resources.database.internal.extensions.JDBCHelper; |
|
Lines 46-51
Link Here
|
| 46 |
private static final long serialVersionUID = 3762247548060316217L; |
50 |
private static final long serialVersionUID = 3762247548060316217L; |
| 47 |
protected InternalDatabase database; |
51 |
protected InternalDatabase database; |
| 48 |
protected int ownerId; |
52 |
protected int ownerId; |
|
|
53 |
private int updateCount; |
| 49 |
public DynamicPagingListImpl(InternalEObject owner, EStructuralFeature eStructuralFeature, DBResource resource, int pagingSize) { |
54 |
public DynamicPagingListImpl(InternalEObject owner, EStructuralFeature eStructuralFeature, DBResource resource, int pagingSize) { |
| 50 |
super(owner, eStructuralFeature); |
55 |
super(owner, eStructuralFeature); |
| 51 |
this.database = (InternalDatabase) resource.getDatabase(); |
56 |
this.database = (InternalDatabase) resource.getDatabase(); |
|
Lines 59-65
Link Here
|
| 59 |
protected void init(int pagingSize) { |
64 |
protected void init(int pagingSize) { |
| 60 |
this.pagingSize = pagingSize; |
65 |
this.pagingSize = pagingSize; |
| 61 |
size = -1; |
66 |
size = -1; |
| 62 |
modified = false; |
67 |
// modified = false; |
| 63 |
this.cache = database.getObjectCache(); |
68 |
this.cache = database.getObjectCache(); |
| 64 |
Integer id = cache.getId(owner); |
69 |
Integer id = cache.getId(owner); |
| 65 |
if(id==null) |
70 |
if(id==null) |
|
Lines 71-77
Link Here
|
| 71 |
//TODO add better error handling here |
76 |
//TODO add better error handling here |
| 72 |
} |
77 |
} |
| 73 |
if(id!=null) |
78 |
if(id!=null) |
|
|
79 |
{ |
| 80 |
id = new Integer(id.intValue()+1); |
| 81 |
|
| 74 |
cache.add(owner, id); |
82 |
cache.add(owner, id); |
|
|
83 |
} |
| 75 |
else |
84 |
else |
| 76 |
cache.add(owner, new Integer(0)); |
85 |
cache.add(owner, new Integer(0)); |
| 77 |
} |
86 |
} |
|
Lines 89-104
Link Here
|
| 89 |
else |
98 |
else |
| 90 |
return resource.isInDatabase(); |
99 |
return resource.isInDatabase(); |
| 91 |
} |
100 |
} |
| 92 |
|
|
|
| 93 |
protected EObject[] getObjects(ReferenceQuery query) { |
| 94 |
EObject[] referenced = null; |
| 95 |
try { |
| 96 |
referenced = getInternalDatabase().getObjects(query); |
| 97 |
} catch (Exception e) { |
| 98 |
throw new DBCollectedExceptions(e); |
| 99 |
} |
| 100 |
return referenced; |
| 101 |
} |
| 102 |
|
101 |
|
| 103 |
/** |
102 |
/** |
| 104 |
* @see org.eclipse.hyades.resources.getInternalDatabase().PagingList#getPagingSize() |
103 |
* @see org.eclipse.hyades.resources.getInternalDatabase().PagingList#getPagingSize() |
|
Lines 133-150
Link Here
|
| 133 |
} |
132 |
} |
| 134 |
return database; |
133 |
return database; |
| 135 |
} |
134 |
} |
| 136 |
protected void addAdapter(EObject object) { |
|
|
| 137 |
if (object == null) |
| 138 |
return; |
| 139 |
DBResource resource = (DBResource) object.eResource(); |
| 140 |
if (resource == null) |
| 141 |
return; |
| 142 |
EContentAdapter contentAdapter = resource.getContentAdapter(); |
| 143 |
if (contentAdapter == null) |
| 144 |
return; |
| 145 |
if (!object.eAdapters().contains(contentAdapter)) |
| 146 |
object.eAdapters().add(contentAdapter); |
| 147 |
} |
| 148 |
|
135 |
|
| 149 |
protected EStructuralFeature getFeatureByColumn() { |
136 |
protected EStructuralFeature getFeatureByColumn() { |
| 150 |
return getEStructuralFeature(); |
137 |
return getEStructuralFeature(); |
|
Lines 163-170
Link Here
|
| 163 |
} |
150 |
} |
| 164 |
return null; |
151 |
return null; |
| 165 |
} |
152 |
} |
| 166 |
protected void intiRS() { |
153 |
protected void initRS() { |
| 167 |
try { |
154 |
try { |
|
|
155 |
if(updateCount>0) |
| 156 |
{ |
| 157 |
try { |
| 158 |
database.forceUpdates(); |
| 159 |
} catch (Exception e) { |
| 160 |
throw new DBCollectedExceptions(e); |
| 161 |
} |
| 162 |
updateCount=0; |
| 163 |
} |
| 164 |
|
| 168 |
if(rs!=null) |
165 |
if(rs!=null) |
| 169 |
rs.close(); |
166 |
rs.close(); |
| 170 |
|
167 |
|
|
Lines 175-181
Link Here
|
| 175 |
if(getEStructuralFeature() instanceof EReference) |
172 |
if(getEStructuralFeature() instanceof EReference) |
| 176 |
{ |
173 |
{ |
| 177 |
query = factory.createReferenceQuery(idsTypes, (EReference)getEStructuralFeature(), getDBMap(), getJDBCHelper().getDatabaseType() , getEStructuralFeature().isOrdered()); |
174 |
query = factory.createReferenceQuery(idsTypes, (EReference)getEStructuralFeature(), getDBMap(), getJDBCHelper().getDatabaseType() , getEStructuralFeature().isOrdered()); |
| 178 |
|
|
|
| 179 |
} |
175 |
} |
| 180 |
else |
176 |
else |
| 181 |
{ |
177 |
{ |
|
Lines 209-226
Link Here
|
| 209 |
return null; |
205 |
return null; |
| 210 |
} |
206 |
} |
| 211 |
} |
207 |
} |
| 212 |
// protected void addCurrentPageToContainer() { |
208 |
|
| 213 |
// for (int i = 0,length =currentPage.size(); i < length; i++) { |
|
|
| 214 |
// if(currentPage.get(i)!=null && ((EObject)currentPage.get(i)).eContainer()==null) |
| 215 |
// { |
| 216 |
// InternalEObject elem = ((InternalEObject)currentPage.get(i)); |
| 217 |
// if(getInverseEReference()!=null) |
| 218 |
// elem.eBasicSetContainer(owner,getInverseFeatureID(),null); |
| 219 |
// else |
| 220 |
// elem.eBasicSetContainer(owner,elem.eContainerFeatureID(),null); |
| 221 |
// } |
| 222 |
// } |
| 223 |
// } |
| 224 |
protected boolean useId_TYPE() { |
209 |
protected boolean useId_TYPE() { |
| 225 |
if(getDataType()==null) |
210 |
if(getDataType()==null) |
| 226 |
return true; |
211 |
return true; |
|
Lines 241-245
Link Here
|
| 241 |
} |
226 |
} |
| 242 |
} |
227 |
} |
| 243 |
} |
228 |
} |
|
|
229 |
protected Object delegateGet(int index) { |
| 230 |
if(updateCount>0) |
| 231 |
{ |
| 232 |
try { |
| 233 |
database.forceUpdates(); |
| 234 |
} catch (Exception e) { |
| 235 |
throw new DBCollectedExceptions(e); |
| 236 |
} |
| 237 |
updateCount=0; |
| 238 |
} |
| 239 |
return super.delegateGet(index); |
| 240 |
} |
| 241 |
protected void delegateAdd(Object object) { |
| 242 |
delegateAdd(size(), object); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
protected void delegateAdd(int index, Object element) { |
| 247 |
if(element instanceof EObject) |
| 248 |
{ |
| 249 |
DBResourceContentAdapter adapter = addAdapter((EObject)element); |
| 250 |
if(adapter!=null) |
| 251 |
{ |
| 252 |
adapter.notifyChanged(createNotification(Notification.ADD, null, element, index, delegateIsEmpty())); |
| 253 |
addToPageIfLoaded(index,element); |
| 254 |
} |
| 255 |
} |
| 256 |
else |
| 257 |
{ |
| 258 |
for (Iterator iterator = owner.eAdapters().iterator(); iterator.hasNext();) { |
| 259 |
Object adapter = (Object) iterator.next(); |
| 260 |
if (adapter instanceof DBResourceContentAdapter) { |
| 261 |
((DBResourceContentAdapter)adapter).notifyChanged(createNotification(Notification.ADD, null, element, index, delegateIsEmpty())); |
| 262 |
addToPageIfLoaded(index,element); |
| 263 |
break; |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
delegateSize(); |
| 268 |
size++; |
| 269 |
updateCount++; |
| 270 |
} |
| 271 |
|
| 272 |
protected void addToPageIfLoaded(int index, Object element) { |
| 273 |
if(index-currentIndex > 0 && index-currentIndex<pagingSize-1) |
| 274 |
{ |
| 275 |
if(currentPage==null) |
| 276 |
currentPage = new ArrayList(pagingSize); |
| 277 |
currentPage.add(element); |
| 278 |
currentLength=currentPage.size(); |
| 279 |
if(currentLength==pagingSize) |
| 280 |
{ |
| 281 |
PageInfo pageInfo=new PageInfo(currentPage,currentIndex/pagingSize,currentLength); |
| 282 |
if(getFIFO().size()==FIFO_LIMIT) |
| 283 |
getFIFO().remove(0); |
| 284 |
getFIFO().add(pageInfo); |
| 285 |
currentPage=null; |
| 286 |
} |
| 287 |
} |
| 288 |
else |
| 289 |
{ |
| 290 |
int pageIndex = index/pagingSize; |
| 291 |
pageIndex = pageIndex*pagingSize; |
| 292 |
|
| 293 |
PageInfo pageInfo = getPageByPageIndex(pageIndex); |
| 294 |
if(pageInfo==null) |
| 295 |
{ |
| 296 |
List l = new ArrayList(); |
| 297 |
l.add(element); |
| 298 |
pageInfo=new PageInfo(l,pageIndex*pagingSize,1); |
| 299 |
if(getFIFO().size()==FIFO_LIMIT) |
| 300 |
getFIFO().remove(0); |
| 301 |
getFIFO().add(pageInfo); |
| 302 |
} |
| 303 |
} |
| 304 |
if(rs!=null) |
| 305 |
try { |
| 306 |
rs.close(); |
| 307 |
rs=null; |
| 308 |
} catch (SQLException e) { |
| 309 |
rs=null; |
| 310 |
throw new DBCollectedExceptions(e); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
protected PageInfo getPageByPageIndex(int pageIndex) |
| 315 |
{ |
| 316 |
//find page |
| 317 |
for (int i = getFIFO().size(); i-->0;) { |
| 318 |
PageInfo pageInfo = (PageInfo)getFIFO().get(i); |
| 319 |
if(pageInfo.getIndex()==pageIndex ) |
| 320 |
{ |
| 321 |
return pageInfo; |
| 322 |
} |
| 323 |
} |
| 324 |
return null; |
| 325 |
} |
| 326 |
protected DBResourceContentAdapter addAdapter(EObject object) { |
| 327 |
if (object != null) |
| 328 |
{ |
| 329 |
DBResource resource = (DBResource) object.eResource(); |
| 330 |
if (resource != null) |
| 331 |
{ |
| 332 |
EContentAdapter contentAdapter = resource.getContentAdapter(); |
| 333 |
if (contentAdapter != null) |
| 334 |
{ |
| 335 |
if (!object.eAdapters().contains(contentAdapter)) |
| 336 |
object.eAdapters().add(contentAdapter); |
| 337 |
return (DBResourceContentAdapter)contentAdapter; |
| 338 |
} |
| 339 |
} |
| 340 |
for (Iterator iterator = owner.eAdapters().iterator(); iterator.hasNext();) { |
| 341 |
Object adapter = (Object) iterator.next(); |
| 342 |
if (adapter instanceof DBResourceContentAdapter) { |
| 343 |
if (!object.eAdapters().contains(adapter)) |
| 344 |
object.eAdapters().add(adapter); |
| 345 |
return (DBResourceContentAdapter)adapter; |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
else |
| 350 |
{ |
| 351 |
for (Iterator iterator = owner.eAdapters().iterator(); iterator.hasNext();) { |
| 352 |
Object adapter = (Object) iterator.next(); |
| 353 |
if (adapter instanceof DBResourceContentAdapter) { |
| 354 |
return (DBResourceContentAdapter)adapter; |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
return null; |
| 359 |
} |
| 360 |
|
| 361 |
/* |
| 362 |
* (non-Javadoc) |
| 363 |
* |
| 364 |
* @see org.eclipse.emf.common.util.DelegatingEList#delegateClear() |
| 365 |
*/ |
| 366 |
protected void delegateClear() { |
| 367 |
init(pagingSize); |
| 368 |
size=0; |
| 369 |
} |
| 244 |
|
370 |
|
|
|
371 |
|
| 372 |
public void set(Object newValue) { |
| 373 |
throw new UnsupportedOperationException(); |
| 374 |
} |
| 375 |
|
| 376 |
public void unset() { |
| 377 |
throw new UnsupportedOperationException(); |
| 378 |
} |
| 379 |
|
| 245 |
} // DynamicPagingListImpl |
380 |
} // DynamicPagingListImpl |