|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| HibernatePrayerRequestManager.java | 80.8% | 76.1% | 89.5% | 77.9% |
|
||||||||||||||
| 1 |
package org.marketchangers.prayer.hibernate;
|
|
| 2 |
|
|
| 3 |
import java.util.Collection;
|
|
| 4 |
import java.util.Collections;
|
|
| 5 |
import java.util.Iterator;
|
|
| 6 |
import java.util.LinkedList;
|
|
| 7 |
import java.util.List;
|
|
| 8 |
|
|
| 9 |
import net.sf.hibernate.Hibernate;
|
|
| 10 |
import net.sf.hibernate.HibernateException;
|
|
| 11 |
import net.sf.hibernate.Session;
|
|
| 12 |
import net.sf.hibernate.type.Type;
|
|
| 13 |
|
|
| 14 |
import org.marketchangers.prayer.PrayerCategory;
|
|
| 15 |
import org.marketchangers.prayer.PrayerComment;
|
|
| 16 |
import org.marketchangers.prayer.PrayerRequest;
|
|
| 17 |
import org.marketchangers.prayer.PrayerRequestManager;
|
|
| 18 |
import org.marketchangers.prayer.PrayerRequestQuery;
|
|
| 19 |
import org.marketchangers.prayer.PrayerRequestSearcher;
|
|
| 20 |
import org.marketchangers.prayer.UserContract;
|
|
| 21 |
import org.nanocontainer.hibernate.SessionProvider;
|
|
| 22 |
|
|
| 23 |
/**
|
|
| 24 |
* @author <a href="mailto:jwilliams@wc-group.com">Jason Williams</a>
|
|
| 25 |
* @author <a href="mailto:mtodd@wc-group.com">Matthew Todd</a>
|
|
| 26 |
*/
|
|
| 27 |
public class HibernatePrayerRequestManager implements PrayerRequestManager { |
|
| 28 |
private SessionProvider provider;
|
|
| 29 |
private PrayerRequestSearcher searcher;
|
|
| 30 |
|
|
| 31 | 52 |
public HibernatePrayerRequestManager(
|
| 32 |
PrayerRequestSearcher searcher, |
|
| 33 |
SessionProvider provider) {
|
|
| 34 | 52 |
this.provider = provider;
|
| 35 | 52 |
this.searcher = searcher;
|
| 36 |
} |
|
| 37 |
|
|
| 38 | 4 |
public boolean submitPrayerRequest(PrayerRequest prayerRequest) { |
| 39 | 4 |
try {
|
| 40 | 4 |
Session session = provider.getSession(); |
| 41 | 4 |
session.save(prayerRequest); |
| 42 | 2 |
provider.commit(); |
| 43 |
} catch (HibernateException he) {
|
|
| 44 | 2 |
he.printStackTrace(); |
| 45 | 2 |
provider.reset(); |
| 46 | 2 |
return false; |
| 47 |
} |
|
| 48 |
|
|
| 49 |
// TODO can we use a ComponentAdapter to make this asynchronous?
|
|
| 50 | 2 |
searcher.index(prayerRequest); |
| 51 |
|
|
| 52 | 2 |
return true; |
| 53 |
} |
|
| 54 |
|
|
| 55 | 2 |
public boolean createPrayerCategory(PrayerCategory category) { |
| 56 | 2 |
try {
|
| 57 | 2 |
Session session = provider.getSession(); |
| 58 | 2 |
session.save(category); |
| 59 | 2 |
provider.commit(); |
| 60 |
} catch (HibernateException he) {
|
|
| 61 | 0 |
he.printStackTrace(); |
| 62 | 0 |
provider.reset(); |
| 63 | 0 |
return false; |
| 64 |
} |
|
| 65 |
|
|
| 66 | 2 |
return true; |
| 67 |
} |
|
| 68 |
|
|
| 69 | 2 |
public PrayerCategory getPrayerCategory(String category) {
|
| 70 | 2 |
PrayerCategory prayerCategory = null;
|
| 71 |
|
|
| 72 | 2 |
try {
|
| 73 | 2 |
List results = |
| 74 |
provider.getSession().find( |
|
| 75 |
"from PrayerCategory as pc where pc.name = ?",
|
|
| 76 |
category, |
|
| 77 |
Hibernate.STRING); |
|
| 78 |
|
|
| 79 | 2 |
if (!results.isEmpty()) {
|
| 80 | 2 |
prayerCategory = (PrayerCategory)results.get(0); |
| 81 |
} |
|
| 82 |
|
|
| 83 | 2 |
provider.close(); |
| 84 |
|
|
| 85 |
} catch (HibernateException he) {
|
|
| 86 | 0 |
he.printStackTrace(); |
| 87 | 0 |
provider.reset(); |
| 88 | 0 |
return null; |
| 89 |
} |
|
| 90 |
|
|
| 91 | 2 |
return prayerCategory;
|
| 92 |
} |
|
| 93 |
|
|
| 94 | 2 |
public List getCategoriesForRequestor(String user) {
|
| 95 | 2 |
List results = Collections.EMPTY_LIST; |
| 96 |
|
|
| 97 | 2 |
try {
|
| 98 | 2 |
results = |
| 99 |
provider.getSession().find( |
|
| 100 |
"from PrayerCategory as pc where pc.requestorUserId = ?",
|
|
| 101 |
user, |
|
| 102 |
Hibernate.STRING); |
|
| 103 |
|
|
| 104 | 2 |
provider.close(); |
| 105 |
} catch (HibernateException he) {
|
|
| 106 | 0 |
results = Collections.EMPTY_LIST; |
| 107 | 0 |
provider.reset(); |
| 108 | 0 |
he.printStackTrace(); |
| 109 |
} |
|
| 110 |
|
|
| 111 | 2 |
return results;
|
| 112 |
} |
|
| 113 |
|
|
| 114 | 0 |
public List getCategoriesForIntercessor(String user) {
|
| 115 |
// TODO Auto-generated method stub
|
|
| 116 | 0 |
return null; |
| 117 |
} |
|
| 118 |
|
|
| 119 | 6 |
public List getRequestorsForIntercessor(String user) {
|
| 120 | 6 |
List requestors = new LinkedList();
|
| 121 |
|
|
| 122 | 6 |
try {
|
| 123 | 6 |
Iterator contracts = |
| 124 |
provider.getSession().iterate( |
|
| 125 |
"from UserContract as uc where uc.status = ? and uc.requesteeUserId = ?",
|
|
| 126 |
new Object[] { UserContract.ACCEPTED, user },
|
|
| 127 |
new Type[] { Hibernate.STRING, Hibernate.STRING });
|
|
| 128 |
|
|
| 129 | 6 |
while (contracts.hasNext()) {
|
| 130 | 6 |
UserContract contract = (UserContract)contracts.next(); |
| 131 | 6 |
requestors.add(contract.getRequestorUserId()); |
| 132 |
} |
|
| 133 |
|
|
| 134 | 6 |
provider.close(); |
| 135 |
|
|
| 136 |
} catch (HibernateException he) {
|
|
| 137 | 0 |
provider.reset(); |
| 138 | 0 |
he.printStackTrace(); |
| 139 |
} |
|
| 140 |
|
|
| 141 | 6 |
return requestors;
|
| 142 |
} |
|
| 143 |
|
|
| 144 | 0 |
public List getRequestorOrganizationsForIntercessor(String user) {
|
| 145 |
// TODO Auto-generated method stub
|
|
| 146 | 0 |
return null; |
| 147 |
} |
|
| 148 |
|
|
| 149 |
/* (non-Javadoc)
|
|
| 150 |
* @see org.marketchangers.prayer.PrayerRequestManager#getCurrentIntercessors(java.lang.String)
|
|
| 151 |
*
|
|
| 152 |
* Used by requestor to view current relationships with intercessors.
|
|
| 153 |
* returns a list of contracts associated with the requestor.
|
|
| 154 |
*
|
|
| 155 |
*/
|
|
| 156 | 2 |
public List getCurrentIntercessors(String requestorUserId) {
|
| 157 | 2 |
List results = Collections.EMPTY_LIST; |
| 158 |
|
|
| 159 | 2 |
try {
|
| 160 | 2 |
results = |
| 161 |
provider.getSession().find( |
|
| 162 |
"from UserContract as uc where requestorUserId = ?",
|
|
| 163 |
requestorUserId, |
|
| 164 |
Hibernate.STRING); |
|
| 165 |
|
|
| 166 | 2 |
provider.close(); |
| 167 |
} catch (HibernateException he) {
|
|
| 168 | 0 |
results = Collections.EMPTY_LIST; |
| 169 | 0 |
he.printStackTrace(); |
| 170 |
} |
|
| 171 |
|
|
| 172 | 2 |
return results;
|
| 173 |
} |
|
| 174 |
|
|
| 175 |
/* (non-Javadoc)
|
|
| 176 |
* @see org.marketchangers.prayer.PrayerRequestManager#getCurrentRequestors(java.lang.String)
|
|
| 177 |
*
|
|
| 178 |
* Used by intercessor to view current relationships with requestors.
|
|
| 179 |
* returns a list of contracts associated with the intercessor.
|
|
| 180 |
*
|
|
| 181 |
*/
|
|
| 182 | 2 |
public List getCurrentRequestors(String intercessorUserId) {
|
| 183 | 2 |
List results = Collections.EMPTY_LIST; |
| 184 |
|
|
| 185 | 2 |
try {
|
| 186 | 2 |
results = |
| 187 |
provider.getSession().find( |
|
| 188 |
"from UserContract as uc where requesteeUserId = ? and status != 'declined' and status != 'cancelled'",
|
|
| 189 |
intercessorUserId, |
|
| 190 |
Hibernate.STRING); |
|
| 191 |
|
|
| 192 | 2 |
provider.close(); |
| 193 |
} catch (HibernateException he) {
|
|
| 194 | 0 |
results = Collections.EMPTY_LIST; |
| 195 | 0 |
he.printStackTrace(); |
| 196 |
} |
|
| 197 |
|
|
| 198 | 2 |
return results;
|
| 199 |
} |
|
| 200 |
|
|
| 201 | 6 |
public boolean removeContract(UserContract contract) { |
| 202 | 6 |
int deletedEntries;
|
| 203 |
|
|
| 204 | 6 |
try {
|
| 205 | 6 |
deletedEntries = |
| 206 |
provider.getSession().delete( |
|
| 207 |
"from UserContract as uc where uc.id = ?",
|
|
| 208 |
contract.getId(), |
|
| 209 |
Hibernate.INTEGER); |
|
| 210 |
|
|
| 211 | 6 |
if (deletedEntries > 0) {
|
| 212 | 2 |
provider.commit(); |
| 213 |
} else {
|
|
| 214 | 4 |
provider.close(); |
| 215 |
} |
|
| 216 |
} catch (HibernateException he) {
|
|
| 217 | 0 |
he.printStackTrace(); |
| 218 | 0 |
provider.reset(); |
| 219 | 0 |
return false; |
| 220 |
} |
|
| 221 |
|
|
| 222 | 6 |
return deletedEntries > 0;
|
| 223 |
} |
|
| 224 |
|
|
| 225 | 10 |
public boolean addContract(UserContract contract) { |
| 226 | 10 |
List results = Collections.EMPTY_LIST; |
| 227 |
|
|
| 228 | 10 |
if ((contract == null) || (!contract.validate())) { |
| 229 | 4 |
return false; |
| 230 |
} |
|
| 231 |
|
|
| 232 | 6 |
try {
|
| 233 | 6 |
Session session = provider.getSession(); |
| 234 |
|
|
| 235 |
//see if the two partys already have a contract in place
|
|
| 236 | 6 |
results = |
| 237 |
session.find( |
|
| 238 |
"from UserContract as uc where uc.requesteeUserId = ? and uc.requestorUserId = ?",
|
|
| 239 |
new Object[] {
|
|
| 240 |
contract.getRequesteeUserId(), |
|
| 241 |
contract.getRequestorUserId()}, |
|
| 242 |
new Type[] { Hibernate.STRING, Hibernate.STRING });
|
|
| 243 |
|
|
| 244 | 6 |
if (results.isEmpty()) {
|
| 245 | 4 |
session.save(contract); |
| 246 | 4 |
provider.commit(); |
| 247 |
} else {
|
|
| 248 | 2 |
provider.close(); |
| 249 | 2 |
return false; |
| 250 |
} |
|
| 251 |
} catch (HibernateException he) {
|
|
| 252 | 0 |
he.printStackTrace(); |
| 253 | 0 |
provider.reset(); |
| 254 | 0 |
return false; |
| 255 |
} |
|
| 256 |
|
|
| 257 | 4 |
return true; |
| 258 |
} |
|
| 259 |
|
|
| 260 |
/* (non-Javadoc)
|
|
| 261 |
* @see org.marketchangers.prayer.PrayerRequestManager#updateContractStatus(org.marketchangers.prayer.UserContract)
|
|
| 262 |
*
|
|
| 263 |
* Used by Intercessor to accept or reject a Requestor's invitation to pray.
|
|
| 264 |
* See IntercessorRelationshipsPortlet. The contract param contains only
|
|
| 265 |
* two values - the Id and Status. This service should simply look up the
|
|
| 266 |
* matching contract and udpate the status.
|
|
| 267 |
*
|
|
| 268 |
*/
|
|
| 269 | 6 |
public boolean updateContractStatus(UserContract contract) { |
| 270 | 6 |
if (contract != null) { |
| 271 | 4 |
try {
|
| 272 | 4 |
Session session = provider.getSession(); |
| 273 | 4 |
UserContract storedContract = |
| 274 |
(UserContract)session.get( |
|
| 275 |
UserContract.class,
|
|
| 276 |
contract.getId()); |
|
| 277 | 4 |
if (storedContract != null) { |
| 278 | 2 |
storedContract.setStatus(contract.getStatus()); |
| 279 | 2 |
session.update(storedContract); |
| 280 | 2 |
provider.commit(); |
| 281 | 2 |
return true; |
| 282 |
} |
|
| 283 |
} catch (HibernateException he) {
|
|
| 284 | 0 |
he.printStackTrace(); |
| 285 | 0 |
provider.reset(); |
| 286 |
} |
|
| 287 |
} |
|
| 288 | 4 |
return false; |
| 289 |
} |
|
| 290 |
|
|
| 291 |
/* (non-Javadoc)
|
|
| 292 |
* @see org.marketchangers.prayer.PrayerRequestManager#addComment(org.marketchangers.prayer.PrayerComment)
|
|
| 293 |
*
|
|
| 294 |
* Used by both Intercessors and Requestors - see AddCommentPortlet
|
|
| 295 |
*
|
|
| 296 |
*/
|
|
| 297 | 4 |
public boolean addComment(PrayerComment comment) { |
| 298 | 4 |
try {
|
| 299 | 4 |
Session session = provider.getSession(); |
| 300 | 4 |
session.save(comment); |
| 301 | 2 |
provider.commit(); |
| 302 |
} catch (HibernateException he) {
|
|
| 303 | 2 |
he.printStackTrace(); |
| 304 | 2 |
provider.reset(); |
| 305 | 2 |
return false; |
| 306 |
} |
|
| 307 | 2 |
return true; |
| 308 |
} |
|
| 309 |
|
|
| 310 |
/* (non-Javadoc)
|
|
| 311 |
* @see org.marketchangers.prayer.PrayerRequestManager#updateStatus(org.marketchangers.prayer.PrayerRequest)
|
|
| 312 |
*
|
|
| 313 |
* Used by Requestor to close a request - see AddCommentPortlet
|
|
| 314 |
*
|
|
| 315 |
*/
|
|
| 316 | 2 |
public boolean updateStatus(PrayerRequest prayerRequest) { |
| 317 | 2 |
try {
|
| 318 | 2 |
Session session = provider.getSession(); |
| 319 | 2 |
PrayerRequest request = |
| 320 |
(PrayerRequest)session.get( |
|
| 321 |
PrayerRequest.class,
|
|
| 322 |
prayerRequest.getId()); |
|
| 323 | 2 |
request.setStatus(prayerRequest.getStatus()); |
| 324 | 2 |
session.update(request); |
| 325 | 2 |
provider.commit(); |
| 326 |
|
|
| 327 |
//update lucene index not tested yet
|
|
| 328 | 2 |
searcher.updateStatus(request); |
| 329 |
} catch (HibernateException he) {
|
|
| 330 | 0 |
he.printStackTrace(); |
| 331 | 0 |
provider.reset(); |
| 332 | 0 |
return false; |
| 333 |
} |
|
| 334 | 2 |
return true; |
| 335 |
} |
|
| 336 |
|
|
| 337 |
/* (non-Javadoc)
|
|
| 338 |
* @see org.marketchangers.prayer.PrayerRequestManager#getPrayerRequestById(java.lang.Integer)
|
|
| 339 |
*
|
|
| 340 |
* Used in various screens by various users to obtain prayer details.
|
|
| 341 |
* There currently is a separate method getCommentsByPrayerId to obtain
|
|
| 342 |
* comments in a separate call.
|
|
| 343 |
*
|
|
| 344 |
*/
|
|
| 345 | 6 |
public PrayerRequest getPrayerRequestById(String user, Integer id) {
|
| 346 | 6 |
PrayerRequest request; |
| 347 |
|
|
| 348 | 6 |
try {
|
| 349 | 6 |
Session session = provider.getSession(); |
| 350 |
|
|
| 351 | 6 |
request = (PrayerRequest) session.get(PrayerRequest.class, id);
|
| 352 |
|
|
| 353 | 6 |
if (!request.getRequestorUserId().equals(user)) {
|
| 354 | 4 |
Collection contracts = |
| 355 |
session.find( |
|
| 356 |
"from UserContract c where requestorUserId = ? and requesteeUserId = ? and status = ?",
|
|
| 357 |
new Object[] {
|
|
| 358 |
request.getRequestorUserId(), |
|
| 359 |
user, |
|
| 360 |
UserContract.ACCEPTED }, |
|
| 361 |
new Type[] {
|
|
| 362 |
Hibernate.STRING, |
|
| 363 |
Hibernate.STRING, |
|
| 364 |
Hibernate.STRING }); |
|
| 365 |
|
|
| 366 | 4 |
request = contracts.isEmpty() ? null : request;
|
| 367 |
} |
|
| 368 |
|
|
| 369 | 6 |
provider.close(); |
| 370 |
|
|
| 371 |
} catch (HibernateException he) {
|
|
| 372 | 0 |
request = null;
|
| 373 | 0 |
provider.reset(); |
| 374 | 0 |
he.printStackTrace(); |
| 375 |
} |
|
| 376 |
|
|
| 377 | 6 |
return request;
|
| 378 |
}/* (non-Javadoc)
|
|
| 379 |
* @see org.marketchangers.prayer.PrayerRequestManager#getCommentsByPrayerId(java.lang.Integer)
|
|
| 380 |
*
|
|
| 381 |
* Used in various screens by various users to obtain prayer comments.
|
|
| 382 |
*
|
|
| 383 |
*/
|
|
| 384 | 2 |
public List getCommentsByPrayerId(Integer prayerRequestId) {
|
| 385 | 2 |
List comments = Collections.EMPTY_LIST; |
| 386 |
|
|
| 387 | 2 |
try {
|
| 388 | 2 |
comments = |
| 389 |
provider.getSession().find( |
|
| 390 |
"from PrayerComment as pc where pc.parentRequest.id = ? order by pc.postDate desc",
|
|
| 391 |
prayerRequestId, |
|
| 392 |
Hibernate.INTEGER); |
|
| 393 | 2 |
provider.close(); |
| 394 |
} catch (HibernateException he) {
|
|
| 395 | 0 |
comments = Collections.EMPTY_LIST; |
| 396 | 0 |
provider.reset(); |
| 397 | 0 |
he.printStackTrace(); |
| 398 |
} |
|
| 399 |
|
|
| 400 | 2 |
return comments;
|
| 401 |
} |
|
| 402 |
|
|
| 403 |
/* (non-Javadoc)
|
|
| 404 |
* @see org.marketchangers.prayer.PrayerRequestManager#getPrayerRequestList(java.lang.String, java.lang.String)
|
|
| 405 |
*
|
|
| 406 |
* Return list of prayer requests based on role of user - the role param is needed
|
|
| 407 |
* because an intercessor who is also a requestor may wish to see either his own
|
|
| 408 |
* requests, or other users' requests with whom he has contracts. Perhaps it might
|
|
| 409 |
* be better to split this into two methods - getPrayerRequestsForIntercessor etc.
|
|
| 410 |
* value of role may be "intercessor" or "requestor" INSTEAD OF "Intercessor" or
|
|
| 411 |
* "Businessperson"
|
|
| 412 |
*
|
|
| 413 |
*/
|
|
| 414 | 2 |
public List getPrayerRequestList(String user, String role) {
|
| 415 | 2 |
List results = Collections.EMPTY_LIST; |
| 416 |
|
|
| 417 | 2 |
if (role.equals("requestor")) { |
| 418 | 2 |
try {
|
| 419 | 2 |
results = |
| 420 |
provider.getSession().find( |
|
| 421 |
"from PrayerRequest as pr where requestorUserId = ?",
|
|
| 422 |
user, |
|
| 423 |
Hibernate.STRING); |
|
| 424 |
|
|
| 425 | 2 |
provider.close(); |
| 426 |
} catch (HibernateException he) {
|
|
| 427 | 0 |
results = Collections.EMPTY_LIST; |
| 428 | 0 |
he.printStackTrace(); |
| 429 |
} |
|
| 430 | 0 |
} else if (role.equals("intercessor")) { |
| 431 |
//TODO - need to put proper query here to obtain requests that this intercessor
|
|
| 432 |
// can see
|
|
| 433 | 0 |
results = Collections.EMPTY_LIST; |
| 434 |
} |
|
| 435 | 2 |
return results;
|
| 436 |
} |
|
| 437 |
|
|
| 438 | 4 |
public List search(String user, PrayerRequestQuery query) {
|
| 439 | 4 |
List searchResults = new LinkedList();
|
| 440 | 4 |
List allowedRequestors = getRequestorsForIntercessor(user); |
| 441 |
|
|
| 442 |
// Assumption: user can only pick organizations containing at least one
|
|
| 443 |
// visible requestor. And if there're no requestors specified, the user
|
|
| 444 |
// must want to search all.
|
|
| 445 | 4 |
if (query.getRequestorUserIds().size() == 0) {
|
| 446 | 2 |
query.getRequestorUserIds().addAll(allowedRequestors); |
| 447 |
} else {
|
|
| 448 | 2 |
query.getRequestorUserIds().retainAll(allowedRequestors); |
| 449 |
} |
|
| 450 |
|
|
| 451 | 4 |
Iterator prayerRequestIds = searcher.search(query).iterator(); |
| 452 |
|
|
| 453 |
// TODO this is grossly inefficient. Write a query that takes many ids?
|
|
| 454 | 4 |
while (prayerRequestIds.hasNext()) {
|
| 455 | 4 |
Integer id = (Integer) prayerRequestIds.next(); |
| 456 | 4 |
searchResults.add(getPrayerRequestById(user, id)); |
| 457 |
} |
|
| 458 |
|
|
| 459 | 4 |
return searchResults;
|
| 460 |
} |
|
| 461 |
} |
|
| 462 |
|
|
||||||||||