1 package org.tigris.scarab.om; 2 3 48 49 import java.util.List ; 50 import java.util.Arrays ; 51 import java.util.Date ; 52 53 import org.apache.turbine.TemplateContext; 54 import org.apache.turbine.Turbine; 55 56 import org.apache.torque.TorqueException; 57 import org.apache.torque.util.Criteria; 58 import org.apache.torque.om.Persistent; 59 60 import org.tigris.scarab.services.security.ScarabSecurity; 61 import org.tigris.scarab.services.cache.ScarabCache; 62 import org.tigris.scarab.tools.localization.L10NKeySet; 63 import org.tigris.scarab.om.Module; 64 import org.tigris.scarab.om.ModuleManager; 65 import org.tigris.scarab.util.ScarabException; 66 import org.tigris.scarab.util.Email; 67 import org.tigris.scarab.util.EmailContext; 68 import org.tigris.scarab.util.Log; 69 70 76 public class Query 77 extends org.tigris.scarab.om.BaseQuery 78 implements Persistent 79 { 80 private static final String GET_R_QUERY_USER = 81 "getRQueryUser"; 82 83 87 private ScarabUser scarabUser; 88 89 93 public ScarabUser getScarabUser() 94 throws TorqueException 95 { 96 ScarabUser user = this.scarabUser; 97 if (user == null) 98 { 99 user = super.getScarabUser(); 100 } 101 102 return user; 103 } 104 105 109 public void setScarabUser(ScarabUser v) 110 throws TorqueException 111 { 112 this.scarabUser = v; 113 super.setScarabUser(v); 114 } 115 116 122 public ScarabModule getScarabModule() 123 { 124 throw new UnsupportedOperationException ( 125 "Should use getModule"); } 127 128 133 public void setScarabModule(ScarabModule module) 134 { 135 throw new UnsupportedOperationException ( 136 "Should use setModule(Module). Note module cannot be new."); } 138 139 142 public void setModule(Module me) 143 throws TorqueException 144 { 145 if (me == null) 146 { 147 setModuleId((Integer )null); 148 } 149 else 150 { 151 Integer id = me.getModuleId(); 152 if (id == null) 153 { 154 throw new TorqueException("Modules must be saved prior to " + 155 "being associated with other objects."); } 157 setModuleId(id); 158 } 159 } 160 161 166 public Module getModule() 167 throws TorqueException 168 { 169 Module module = null; 170 Integer id = getModuleId(); 171 if ( id != null ) 172 { 173 module = ModuleManager.getInstance(id); 174 } 175 176 return module; 177 } 178 179 182 public static Query getInstance() 183 { 184 return new Query(); 185 } 186 187 public boolean canDelete(ScarabUser user) 188 throws Exception 189 { 190 return (user.hasPermission(ScarabSecurity.ITEM__DELETE, getModule()) 193 || (user.getUserId().equals(getUserId()) 194 && (getScopeId().equals(Scope.PERSONAL__PK)))); 195 } 196 197 public boolean canEdit(ScarabUser user) 198 throws Exception 199 { 200 return canDelete(user); 201 } 202 203 213 public boolean saveAndSendEmail(ScarabUser user, Module module, 214 TemplateContext context) 215 throws Exception 216 { 217 Exception exception=null; 221 if (getScopeId().equals(Scope.PERSONAL__PK) 222 || user.hasPermission(ScarabSecurity.ITEM__APPROVE, module)) 223 { 224 setApproved(true); 225 } 226 else 227 { 228 setApproved(false); 229 230 if (context != null) 233 { 234 String template = Turbine.getConfiguration(). 235 getString("scarab.email.requireapproval.template", 236 "RequireApproval.vm"); 237 238 ScarabUser[] toUsers = module 239 .getUsers(ScarabSecurity.ITEM__APPROVE); 240 241 if (Log.get().isDebugEnabled()) 242 { 243 if (toUsers == null || toUsers.length ==0) 244 { 245 Log.get().debug("No users to approve query."); 246 } 247 else 248 { 249 Log.get().debug("Users to approve query: "); 250 for (int i=0; i<toUsers.length; i++) 251 { 252 Log.get().debug(toUsers[i].getEmail()); 253 } 254 } 255 } 256 257 EmailContext ectx = new EmailContext(); 258 ectx.setUser(user); 259 ectx.setModule(module); 260 ectx.setDefaultTextKey("NewQueryRequiresApproval"); 261 262 String fromUser = "scarab.email.default"; 263 try 264 { 265 Email.sendEmail(ectx, 266 module, 267 fromUser, 268 module.getSystemEmail(), 269 Arrays.asList(toUsers), 270 null, 271 template); 272 } 273 catch (Exception e) 274 { 275 exception = e; 276 } 277 } 278 } 279 if (getMITList() != null) 280 { 281 getMITList().save(); 282 setMITList(getMITList()); 285 } 286 save(); 287 if(exception != null) 288 { 289 throw exception; 290 } 291 return true; 292 } 293 294 public MITList getMITList() 295 throws TorqueException 296 { 297 MITList mitlist = super.getMITList(); 298 if (mitlist == null) 299 { 300 mitlist = MITListManager.getSingleItemList(getModule(), 301 getIssueType(), null); 302 } 303 return mitlist; 304 } 305 306 309 public void subscribe(ScarabUser user, Integer frequencyId) 310 throws Exception 311 { 312 RQueryUser rqu = getRQueryUser(user); 313 rqu.setSubscriptionFrequency(frequencyId); 314 rqu.setIsSubscribed(true); 315 rqu.save(); 316 } 317 318 321 public void unSubscribe(ScarabUser user) 322 throws Exception 323 { 324 RQueryUser rqu = getRQueryUser(user); 325 if (rqu.getIsdefault()) 326 { 327 rqu.setIsSubscribed(false); 328 rqu.save(); 329 } 330 else 331 { 332 rqu.delete(user); 333 } 334 } 335 336 337 340 public RQueryUser getRQueryUser(ScarabUser user) 341 throws Exception 342 { 343 RQueryUser result = null; 344 Object obj = ScarabCache.get(this, GET_R_QUERY_USER, user); 345 if (obj == null) 346 { 347 Criteria crit = new Criteria(); 348 crit.add(RQueryUserPeer.QUERY_ID, getQueryId()); 349 crit.add(RQueryUserPeer.USER_ID, user.getUserId()); 350 List rqus = RQueryUserPeer.doSelect(crit); 351 if (!rqus.isEmpty()) 352 { 353 result = (RQueryUser)rqus.get(0); 354 } 355 else 356 { 357 result = new RQueryUser(); 358 result.setQuery(this); 359 result.setUserId(user.getUserId()); 360 } 361 ScarabCache.put(result, this, GET_R_QUERY_USER, user); 362 } 363 else 364 { 365 result = (RQueryUser)obj; 366 } 367 return result; 368 } 369 370 374 public void approve(ScarabUser user, boolean approved) 375 throws Exception 376 { 377 Module module = getModule(); 378 379 if (user.hasPermission(ScarabSecurity.ITEM__APPROVE, module)) 380 { 381 setApproved(true); 382 if (!approved) 383 { 384 setScopeId(Scope.PERSONAL__PK); 385 } 386 save(); 387 } 388 else 389 { 390 throw new ScarabException(L10NKeySet.YouDoNotHavePermissionToAction); 391 } 392 } 393 394 395 400 public void delete(ScarabUser user) 401 throws Exception 402 { 403 Module module = getModule(); 404 if (user.hasPermission(ScarabSecurity.ITEM__APPROVE, module) 405 || (user.getUserId().equals(getUserId()) 406 && getScopeId().equals(Scope.PERSONAL__PK))) 407 { 408 List rqus = getRQueryUsers(); 410 for (int i=0; i<rqus.size(); i++) 411 { 412 RQueryUser rqu = (RQueryUser)rqus.get(i); 413 rqu.delete(user); 414 } 415 setDeleted(true); 416 save(); 417 418 } 419 else 420 { 421 throw new ScarabException(L10NKeySet.YouDoNotHavePermissionToAction); 422 } 423 } 424 425 430 public void copyQuery(ScarabUser user) 431 throws Exception 432 { 433 Query newQuery = new Query(); 434 newQuery.setName(getName() + " (copy)"); 435 newQuery.setDescription(getDescription()); 436 newQuery.setValue(getValue()); 437 newQuery.setModuleId(getModuleId()); 438 newQuery.setIssueTypeId(getIssueTypeId()); 439 newQuery.setListId(getListId()); 440 newQuery.setApproved(getApproved()); 441 newQuery.setCreatedDate(new Date ()); 442 newQuery.setUserId(user.getUserId()); 443 newQuery.setScopeId(getScopeId()); 444 newQuery.save(); 445 446 RQueryUser rqu = getRQueryUser(user); 447 if (rqu != null) 448 { 449 RQueryUser rquNew = new RQueryUser(); 450 rquNew.setQueryId(newQuery.getQueryId()); 451 rquNew.setUserId(user.getUserId()); 452 rquNew.setSubscriptionFrequency(rqu.getSubscriptionFrequency()); 453 rquNew.setIsdefault(rqu.getIsdefault()); 454 rquNew.setIsSubscribed(rqu.getIsSubscribed()); 455 rquNew.save(); 456 } 457 } 458 } 459 | Popular Tags |