Source Code Cross Referenced for ServiceUtil.java in  » ERP-CRM-Financial » ofbiz » org » ofbiz » service » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ERP CRM Financial » ofbiz » org.ofbiz.service 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Licensed to the Apache Software Foundation (ASF) under one
003:         * or more contributor license agreements.  See the NOTICE file
004:         * distributed with this work for additional information
005:         * regarding copyright ownership.  The ASF licenses this file
006:         * to you under the Apache License, Version 2.0 (the
007:         * "License"); you may not use this file except in compliance
008:         * with the License.  You may obtain a copy of the License at
009:         * 
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         * 
012:         * Unless required by applicable law or agreed to in writing,
013:         * software distributed under the License is distributed on an
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         * KIND, either express or implied.  See the License for the
016:         * specific language governing permissions and limitations
017:         * under the License.
018:         *******************************************************************************/package org.ofbiz.service;
019:
020:        import java.sql.Timestamp;
021:        import java.util.Calendar;
022:        import java.util.Iterator;
023:        import java.util.LinkedList;
024:        import java.util.List;
025:        import java.util.Locale;
026:        import java.util.Map;
027:        import javax.servlet.http.HttpServletRequest;
028:        import javax.transaction.Transaction;
029:
030:        import javolution.util.FastMap;
031:        import javolution.util.FastList;
032:
033:        import org.ofbiz.base.util.Debug;
034:        import org.ofbiz.base.util.UtilDateTime;
035:        import org.ofbiz.base.util.UtilMisc;
036:        import org.ofbiz.base.util.UtilProperties;
037:        import org.ofbiz.base.util.UtilValidate;
038:        import org.ofbiz.entity.GenericDelegator;
039:        import org.ofbiz.entity.GenericEntityException;
040:        import org.ofbiz.entity.GenericValue;
041:        import org.ofbiz.entity.transaction.TransactionUtil;
042:        import org.ofbiz.entity.transaction.GenericTransactionException;
043:        import org.ofbiz.entity.condition.EntityCondition;
044:        import org.ofbiz.entity.condition.EntityConditionList;
045:        import org.ofbiz.entity.condition.EntityExpr;
046:        import org.ofbiz.entity.condition.EntityOperator;
047:        import org.ofbiz.entity.util.EntityFindOptions;
048:        import org.ofbiz.entity.util.EntityListIterator;
049:        import org.ofbiz.security.Security;
050:        import org.ofbiz.service.config.ServiceConfigUtil;
051:
052:        /**
053:         * Generic Service Utility Class
054:         */
055:        public class ServiceUtil {
056:
057:            public static final String module = ServiceUtil.class.getName();
058:            public static final String resource = "ServiceErrorUiLabels";
059:
060:            /** A little short-cut method to check to see if a service returned an error */
061:            public static boolean isError(Map results) {
062:                if (results == null
063:                        || results.get(ModelService.RESPONSE_MESSAGE) == null) {
064:                    return false;
065:                }
066:                return ModelService.RESPOND_ERROR.equals(results
067:                        .get(ModelService.RESPONSE_MESSAGE));
068:            }
069:
070:            public static boolean isFailure(Map results) {
071:                if (results == null
072:                        || results.get(ModelService.RESPONSE_MESSAGE) == null) {
073:                    return false;
074:                }
075:                return ModelService.RESPOND_FAIL.equals(results
076:                        .get(ModelService.RESPONSE_MESSAGE));
077:            }
078:
079:            /** A small routine used all over to improve code efficiency, make a result map with the message and the error response code */
080:            public static Map returnError(String errorMessage) {
081:                return returnProblem(ModelService.RESPOND_ERROR, errorMessage,
082:                        null, null, null);
083:            }
084:
085:            /** A small routine used all over to improve code efficiency, make a result map with the message and the error response code */
086:            public static Map returnError(String errorMessage,
087:                    List errorMessageList) {
088:                return returnProblem(ModelService.RESPOND_ERROR, errorMessage,
089:                        errorMessageList, null, null);
090:            }
091:
092:            /** A small routine used all over to improve code efficiency, make a result map with the message and the error response code */
093:            public static Map returnError(List errorMessageList) {
094:                return returnProblem(ModelService.RESPOND_ERROR, null,
095:                        errorMessageList, null, null);
096:            }
097:
098:            public static Map returnFailure(String errorMessage) {
099:                return returnProblem(ModelService.RESPOND_FAIL, errorMessage,
100:                        null, null, null);
101:            }
102:
103:            public static Map returnFailure(List errorMessageList) {
104:                return returnProblem(ModelService.RESPOND_FAIL, null,
105:                        errorMessageList, null, null);
106:            }
107:
108:            public static Map returnFailure() {
109:                return returnProblem(ModelService.RESPOND_FAIL, null, null,
110:                        null, null);
111:            }
112:
113:            /** A small routine used all over to improve code efficiency, make a result map with the message and the error response code, also forwards any error messages from the nestedResult */
114:            public static Map returnError(String errorMessage,
115:                    List errorMessageList, Map errorMessageMap, Map nestedResult) {
116:                return returnProblem(ModelService.RESPOND_ERROR, errorMessage,
117:                        errorMessageList, errorMessageMap, nestedResult);
118:            }
119:
120:            public static Map returnProblem(String returnType,
121:                    String errorMessage, List errorMessageList,
122:                    Map errorMessageMap, Map nestedResult) {
123:                Map result = FastMap.newInstance();
124:                result.put(ModelService.RESPONSE_MESSAGE, returnType);
125:                if (errorMessage != null) {
126:                    result.put(ModelService.ERROR_MESSAGE, errorMessage);
127:                }
128:
129:                List errorList = new LinkedList();
130:                if (errorMessageList != null) {
131:                    errorList.addAll(errorMessageList);
132:                }
133:
134:                Map errorMap = FastMap.newInstance();
135:                if (errorMessageMap != null) {
136:                    errorMap.putAll(errorMessageMap);
137:                }
138:
139:                if (nestedResult != null) {
140:                    if (nestedResult.get(ModelService.ERROR_MESSAGE) != null) {
141:                        errorList.add(nestedResult
142:                                .get(ModelService.ERROR_MESSAGE));
143:                    }
144:                    if (nestedResult.get(ModelService.ERROR_MESSAGE_LIST) != null) {
145:                        errorList.addAll((List) nestedResult
146:                                .get(ModelService.ERROR_MESSAGE_LIST));
147:                    }
148:                    if (nestedResult.get(ModelService.ERROR_MESSAGE_MAP) != null) {
149:                        errorMap.putAll((Map) nestedResult
150:                                .get(ModelService.ERROR_MESSAGE_MAP));
151:                    }
152:                }
153:
154:                if (errorList.size() > 0) {
155:                    result.put(ModelService.ERROR_MESSAGE_LIST, errorList);
156:                }
157:                if (errorMap.size() > 0) {
158:                    result.put(ModelService.ERROR_MESSAGE_MAP, errorMap);
159:                }
160:                return result;
161:            }
162:
163:            /** A small routine used all over to improve code efficiency, make a result map with the message and the success response code */
164:            public static Map returnSuccess(String successMessage) {
165:                return returnMessage(ModelService.RESPOND_SUCCESS,
166:                        successMessage);
167:            }
168:
169:            /** A small routine used all over to improve code efficiency, make a result map with the message and the success response code */
170:            public static Map returnSuccess() {
171:                return returnMessage(ModelService.RESPOND_SUCCESS, null);
172:            }
173:
174:            /** A small routine to make a result map with the message and the response code
175:             * NOTE: This brings out some bad points to our message convention: we should be using a single message or message list
176:             *  and what type of message that is should be determined by the RESPONSE_MESSAGE (and there's another annoyance, it should be RESPONSE_CODE)
177:             */
178:            public static Map returnMessage(String code, String message) {
179:                Map result = FastMap.newInstance();
180:                if (code != null)
181:                    result.put(ModelService.RESPONSE_MESSAGE, code);
182:                if (message != null)
183:                    result.put(ModelService.SUCCESS_MESSAGE, message);
184:                return result;
185:            }
186:
187:            /** A small routine used all over to improve code efficiency, get the partyId and does a security check
188:             *<b>security check</b>: userLogin partyId must equal partyId, or must have [secEntity][secOperation] permission
189:             */
190:            public static String getPartyIdCheckSecurity(
191:                    GenericValue userLogin, Security security, Map context,
192:                    Map result, String secEntity, String secOperation) {
193:                String partyId = (String) context.get("partyId");
194:                Locale locale = getLocale(context);
195:                if (partyId == null || partyId.length() == 0) {
196:                    partyId = userLogin.getString("partyId");
197:                }
198:
199:                // partyId might be null, so check it
200:                if (partyId == null || partyId.length() == 0) {
201:                    result.put(ModelService.RESPONSE_MESSAGE,
202:                            ModelService.RESPOND_ERROR);
203:                    String errMsg = UtilProperties.getMessage(
204:                            ServiceUtil.resource,
205:                            "serviceUtil.party_id_missing", locale)
206:                            + ".";
207:                    result.put(ModelService.ERROR_MESSAGE, errMsg);
208:                    return partyId;
209:                }
210:
211:                // <b>security check</b>: userLogin partyId must equal partyId, or must have PARTYMGR_CREATE permission
212:                if (!partyId.equals(userLogin.getString("partyId"))) {
213:                    if (!security.hasEntityPermission(secEntity, secOperation,
214:                            userLogin)) {
215:                        result.put(ModelService.RESPONSE_MESSAGE,
216:                                ModelService.RESPOND_ERROR);
217:                        String errMsg = UtilProperties.getMessage(
218:                                ServiceUtil.resource,
219:                                "serviceUtil.no_permission_to_operation",
220:                                locale)
221:                                + ".";
222:                        result.put(ModelService.ERROR_MESSAGE, errMsg);
223:                        return partyId;
224:                    }
225:                }
226:                return partyId;
227:            }
228:
229:            public static void setMessages(HttpServletRequest request,
230:                    String errorMessage, String eventMessage,
231:                    String defaultMessage) {
232:                if (UtilValidate.isNotEmpty(errorMessage))
233:                    request.setAttribute("_ERROR_MESSAGE_", errorMessage);
234:
235:                if (UtilValidate.isNotEmpty(eventMessage))
236:                    request.setAttribute("_EVENT_MESSAGE_", eventMessage);
237:
238:                if (UtilValidate.isEmpty(errorMessage)
239:                        && UtilValidate.isEmpty(eventMessage)
240:                        && UtilValidate.isNotEmpty(defaultMessage))
241:                    request.setAttribute("_EVENT_MESSAGE_", defaultMessage);
242:
243:            }
244:
245:            public static void getMessages(HttpServletRequest request,
246:                    Map result, String defaultMessage) {
247:                getMessages(request, result, defaultMessage, null, null, null,
248:                        null, null, null);
249:            }
250:
251:            public static void getMessages(HttpServletRequest request,
252:                    Map result, String defaultMessage, String msgPrefix,
253:                    String msgSuffix, String errorPrefix, String errorSuffix,
254:                    String successPrefix, String successSuffix) {
255:                String errorMessage = ServiceUtil.makeErrorMessage(result,
256:                        msgPrefix, msgSuffix, errorPrefix, errorSuffix);
257:                String successMessage = ServiceUtil.makeSuccessMessage(result,
258:                        msgPrefix, msgSuffix, successPrefix, successSuffix);
259:                setMessages(request, errorMessage, successMessage,
260:                        defaultMessage);
261:            }
262:
263:            public static String getErrorMessage(Map result) {
264:                StringBuffer errorMessage = new StringBuffer();
265:
266:                if (result.get(ModelService.ERROR_MESSAGE) != null)
267:                    errorMessage.append((String) result
268:                            .get(ModelService.ERROR_MESSAGE));
269:
270:                if (result.get(ModelService.ERROR_MESSAGE_LIST) != null) {
271:                    List errors = (List) result
272:                            .get(ModelService.ERROR_MESSAGE_LIST);
273:                    Iterator errorIter = errors.iterator();
274:                    while (errorIter.hasNext()) {
275:                        // NOTE: this MUST use toString and not cast to String because it may be a MessageString object
276:                        String curMessage = errorIter.next().toString();
277:                        if (errorMessage.length() > 0) {
278:                            errorMessage.append(", ");
279:                        }
280:                        errorMessage.append(curMessage);
281:                    }
282:                }
283:
284:                return errorMessage.toString();
285:            }
286:
287:            public static String makeErrorMessage(Map result, String msgPrefix,
288:                    String msgSuffix, String errorPrefix, String errorSuffix) {
289:                if (result == null) {
290:                    Debug.logWarning("A null result map was passed", module);
291:                    return null;
292:                }
293:                String errorMsg = (String) result
294:                        .get(ModelService.ERROR_MESSAGE);
295:                List errorMsgList = (List) result
296:                        .get(ModelService.ERROR_MESSAGE_LIST);
297:                Map errorMsgMap = (Map) result
298:                        .get(ModelService.ERROR_MESSAGE_MAP);
299:                StringBuffer outMsg = new StringBuffer();
300:
301:                if (errorMsg != null) {
302:                    if (msgPrefix != null)
303:                        outMsg.append(msgPrefix);
304:                    outMsg.append(errorMsg);
305:                    if (msgSuffix != null)
306:                        outMsg.append(msgSuffix);
307:                }
308:
309:                outMsg.append(makeMessageList(errorMsgList, msgPrefix,
310:                        msgSuffix));
311:
312:                if (errorMsgMap != null) {
313:                    Iterator mapIter = errorMsgMap.entrySet().iterator();
314:
315:                    while (mapIter.hasNext()) {
316:                        Map.Entry entry = (Map.Entry) mapIter.next();
317:
318:                        outMsg.append(msgPrefix);
319:                        outMsg.append(entry.getKey());
320:                        outMsg.append(": ");
321:                        outMsg.append(entry.getValue());
322:                        outMsg.append(msgSuffix);
323:                    }
324:                }
325:
326:                if (outMsg.length() > 0) {
327:                    StringBuffer strBuf = new StringBuffer();
328:
329:                    if (errorPrefix != null)
330:                        strBuf.append(errorPrefix);
331:                    strBuf.append(outMsg.toString());
332:                    if (errorSuffix != null)
333:                        strBuf.append(errorSuffix);
334:                    return strBuf.toString();
335:                } else {
336:                    return null;
337:                }
338:            }
339:
340:            public static String makeSuccessMessage(Map result,
341:                    String msgPrefix, String msgSuffix, String successPrefix,
342:                    String successSuffix) {
343:                if (result == null) {
344:                    return "";
345:                }
346:                String successMsg = (String) result
347:                        .get(ModelService.SUCCESS_MESSAGE);
348:                List successMsgList = (List) result
349:                        .get(ModelService.SUCCESS_MESSAGE_LIST);
350:                StringBuffer outMsg = new StringBuffer();
351:
352:                outMsg.append(makeMessageList(successMsgList, msgPrefix,
353:                        msgSuffix));
354:
355:                if (successMsg != null) {
356:                    if (msgPrefix != null)
357:                        outMsg.append(msgPrefix);
358:                    outMsg.append(successMsg);
359:                    if (msgSuffix != null)
360:                        outMsg.append(msgSuffix);
361:                }
362:
363:                if (outMsg.length() > 0) {
364:                    StringBuffer strBuf = new StringBuffer();
365:                    if (successPrefix != null)
366:                        strBuf.append(successPrefix);
367:                    strBuf.append(outMsg.toString());
368:                    if (successSuffix != null)
369:                        strBuf.append(successSuffix);
370:                    return strBuf.toString();
371:                } else {
372:                    return null;
373:                }
374:            }
375:
376:            public static String makeMessageList(List msgList,
377:                    String msgPrefix, String msgSuffix) {
378:                StringBuffer outMsg = new StringBuffer();
379:                if (msgList != null && msgList.size() > 0) {
380:                    Iterator iter = msgList.iterator();
381:                    while (iter.hasNext()) {
382:                        Object msg = iter.next();
383:                        if (msg == null)
384:                            continue;
385:                        String curMsg = msg.toString();
386:                        if (msgPrefix != null)
387:                            outMsg.append(msgPrefix);
388:                        outMsg.append(curMsg);
389:                        if (msgSuffix != null)
390:                            outMsg.append(msgSuffix);
391:                    }
392:                }
393:                return outMsg.toString();
394:            }
395:
396:            /**
397:             * Takes the result of an invocation and extracts any error messages
398:             * and adds them to the targetList or targetMap. This will handle both List and String
399:             * error messags.
400:             *
401:             * @param targetList    The List to add the error messages to
402:             * @param targetMap The Map to add any Map error messages to
403:             * @param callResult The result from an invocation
404:             */
405:            public static void addErrors(List targetList, Map targetMap,
406:                    Map callResult) {
407:                List newList;
408:                Map errorMsgMap;
409:
410:                //See if there is a single message
411:                if (callResult.containsKey(ModelService.ERROR_MESSAGE)) {
412:                    targetList.add(callResult.get(ModelService.ERROR_MESSAGE));
413:                }
414:
415:                //See if there is a message list
416:                if (callResult.containsKey(ModelService.ERROR_MESSAGE_LIST)) {
417:                    newList = (List) callResult
418:                            .get(ModelService.ERROR_MESSAGE_LIST);
419:                    targetList.addAll(newList);
420:                }
421:
422:                //See if there are an error message map
423:                if (callResult.containsKey(ModelService.ERROR_MESSAGE_MAP)) {
424:                    errorMsgMap = (Map) callResult
425:                            .get(ModelService.ERROR_MESSAGE_MAP);
426:                    targetMap.putAll(errorMsgMap);
427:                }
428:            }
429:
430:            public static Map purgeOldJobs(DispatchContext dctx, Map context) {
431:                String sendPool = ServiceConfigUtil.getSendPool();
432:                int daysToKeep = ServiceConfigUtil.getPurgeJobDays();
433:                GenericDelegator delegator = dctx.getDelegator();
434:
435:                Timestamp now = UtilDateTime.nowTimestamp();
436:                Calendar cal = Calendar.getInstance();
437:                cal.setTimeInMillis(now.getTime());
438:                cal.add(Calendar.DAY_OF_YEAR, daysToKeep * -1);
439:                Timestamp purgeTime = new Timestamp(cal.getTimeInMillis());
440:
441:                // create the conditions to query
442:                EntityCondition pool = new EntityExpr("poolId",
443:                        EntityOperator.EQUALS, sendPool);
444:
445:                List finExp = UtilMisc.toList(new EntityExpr("finishDateTime",
446:                        EntityOperator.NOT_EQUAL, null));
447:                finExp.add(new EntityExpr("finishDateTime",
448:                        EntityOperator.LESS_THAN, purgeTime));
449:
450:                List canExp = UtilMisc.toList(new EntityExpr("cancelDateTime",
451:                        EntityOperator.NOT_EQUAL, null));
452:                canExp.add(new EntityExpr("cancelDateTime",
453:                        EntityOperator.LESS_THAN, purgeTime));
454:
455:                EntityCondition cancelled = new EntityConditionList(canExp,
456:                        EntityOperator.AND);
457:                EntityCondition finished = new EntityConditionList(finExp,
458:                        EntityOperator.AND);
459:
460:                EntityCondition doneCond = new EntityConditionList(UtilMisc
461:                        .toList(cancelled, finished), EntityOperator.OR);
462:                EntityCondition mainCond = new EntityConditionList(UtilMisc
463:                        .toList(doneCond, pool), EntityOperator.AND);
464:
465:                // configure the find options
466:                EntityFindOptions findOptions = new EntityFindOptions();
467:                findOptions
468:                        .setResultSetType(EntityFindOptions.TYPE_SCROLL_INSENSITIVE);
469:                findOptions.setMaxRows(1000);
470:
471:                // always suspend the current transaction; use the one internally
472:                Transaction parent = null;
473:                try {
474:                    if (TransactionUtil.getStatus() != TransactionUtil.STATUS_NO_TRANSACTION) {
475:                        parent = TransactionUtil.suspend();
476:                    }
477:
478:                    // lookup the jobs - looping 1000 at a time to avoid problems with cursors
479:                    // also, using unique transaction to delete as many as possible even with errors
480:                    boolean noMoreResults = false;
481:                    boolean beganTx1 = false;
482:                    while (!noMoreResults) {
483:                        // current list of records
484:                        List curList = null;
485:                        try {
486:                            // begin this transaction
487:                            beganTx1 = TransactionUtil.begin();
488:
489:                            EntityListIterator foundJobs = delegator
490:                                    .findListIteratorByCondition("JobSandbox",
491:                                            mainCond, null, null, null,
492:                                            findOptions);
493:                            curList = foundJobs.getPartialList(1, 1000);
494:                            foundJobs.close();
495:
496:                        } catch (GenericEntityException e) {
497:                            Debug.logError(e,
498:                                    "Cannot obtain job data from datasource",
499:                                    module);
500:                            try {
501:                                TransactionUtil.rollback(beganTx1, e
502:                                        .getMessage(), e);
503:                            } catch (GenericTransactionException e1) {
504:                                Debug.logWarning(e1, module);
505:                            }
506:                            return ServiceUtil.returnError(e.getMessage());
507:                        } finally {
508:                            try {
509:                                TransactionUtil.commit(beganTx1);
510:                            } catch (GenericTransactionException e) {
511:                                Debug.logWarning(e, module);
512:                            }
513:                        }
514:
515:                        // remove each from the list in its own transaction
516:                        if (curList != null && curList.size() > 0) {
517:                            // list of runtime data IDs to attempt to delete
518:                            List runtimeToDelete = FastList.newInstance();
519:
520:                            Iterator curIter = curList.iterator();
521:                            while (curIter.hasNext()) {
522:                                GenericValue job = (GenericValue) curIter
523:                                        .next();
524:                                String runtimeId = job
525:                                        .getString("runtimeDataId");
526:                                String jobId = job.getString("jobId");
527:                                boolean beganTx2 = false;
528:                                try {
529:                                    beganTx2 = TransactionUtil.begin();
530:                                    job.remove();
531:                                    runtimeToDelete.add(runtimeId);
532:                                } catch (GenericEntityException e) {
533:                                    Debug.logInfo(
534:                                            "Cannot remove job data for ID: "
535:                                                    + jobId, module);
536:                                    try {
537:                                        TransactionUtil.rollback(beganTx2, e
538:                                                .getMessage(), e);
539:                                    } catch (GenericTransactionException e1) {
540:                                        Debug.logWarning(e1, module);
541:                                    }
542:                                } finally {
543:                                    try {
544:                                        TransactionUtil.commit(beganTx2);
545:                                    } catch (GenericTransactionException e) {
546:                                        Debug.logWarning(e, module);
547:                                    }
548:                                }
549:                            }
550:
551:                            // delete the runtime data - in a new transaction for each delete
552:                            // we do this so that the ones which cannot be deleted to not cause
553:                            // the entire group to rollback; some may be attached to multiple jobs.
554:                            if (runtimeToDelete.size() > 0) {
555:                                Iterator delIter = runtimeToDelete.iterator();
556:                                while (delIter.hasNext()) {
557:                                    String runtimeId = (String) delIter.next();
558:                                    boolean beganTx3 = false;
559:                                    try {
560:                                        beganTx3 = TransactionUtil.begin();
561:                                        delegator.removeByAnd("RuntimeData",
562:                                                UtilMisc.toMap("runtimeDataId",
563:                                                        runtimeId));
564:
565:                                    } catch (GenericEntityException e) {
566:                                        Debug.logInfo(
567:                                                "Cannot remove runtime data for ID: "
568:                                                        + runtimeId, module);
569:                                        try {
570:                                            TransactionUtil.rollback(beganTx3,
571:                                                    e.getMessage(), e);
572:                                        } catch (GenericTransactionException e1) {
573:                                            Debug.logWarning(e1, module);
574:                                        }
575:                                    } finally {
576:                                        try {
577:                                            TransactionUtil.commit(beganTx3);
578:                                        } catch (GenericTransactionException e) {
579:                                            Debug.logWarning(e, module);
580:                                        }
581:                                    }
582:                                }
583:                            }
584:                        } else {
585:                            noMoreResults = true;
586:                        }
587:                    }
588:                } catch (GenericTransactionException e) {
589:                    Debug
590:                            .logError(
591:                                    e,
592:                                    "Unable to suspend transaction; cannot purge jobs!",
593:                                    module);
594:                    return ServiceUtil.returnError(e.getMessage());
595:                } finally {
596:                    if (parent != null) {
597:                        try {
598:                            TransactionUtil.resume(parent);
599:                        } catch (GenericTransactionException e) {
600:                            Debug.logWarning(e, module);
601:                        }
602:                    }
603:                }
604:
605:                return ServiceUtil.returnSuccess();
606:            }
607:
608:            public static Map cancelJob(DispatchContext dctx, Map context) {
609:                GenericDelegator delegator = dctx.getDelegator();
610:                Security security = dctx.getSecurity();
611:                GenericValue userLogin = (GenericValue) context
612:                        .get("userLogin");
613:                Locale locale = getLocale(context);
614:
615:                if (!security.hasPermission("SERVICE_INVOKE_ANY", userLogin)) {
616:                    String errMsg = UtilProperties.getMessage(
617:                            ServiceUtil.resource,
618:                            "serviceUtil.no_permission_to_run", locale)
619:                            + ".";
620:                    return ServiceUtil.returnError(errMsg);
621:                }
622:
623:                String jobId = (String) context.get("jobId");
624:                Map fields = UtilMisc.toMap("jobId", jobId);
625:
626:                GenericValue job = null;
627:                try {
628:                    job = delegator.findByPrimaryKey("JobSandbox", fields);
629:                    if (job != null) {
630:                        job.set("cancelDateTime", UtilDateTime.nowTimestamp());
631:                        job.set("statusId", "SERVICE_CANCELLED");
632:                        job.store();
633:                    }
634:                } catch (GenericEntityException e) {
635:                    Debug.logError(e, module);
636:                    String errMsg = UtilProperties.getMessage(
637:                            ServiceUtil.resource,
638:                            "serviceUtil.unable_to_cancel_job", locale)
639:                            + " : " + fields;
640:                    return ServiceUtil.returnError(errMsg);
641:                }
642:
643:                Timestamp cancelDate = job.getTimestamp("cancelDateTime");
644:                if (cancelDate != null) {
645:                    Map result = ServiceUtil.returnSuccess();
646:                    result.put("cancelDateTime", cancelDate);
647:                    return result;
648:                } else {
649:                    String errMsg = UtilProperties.getMessage(
650:                            ServiceUtil.resource,
651:                            "serviceUtil.unable_to_cancel_job", locale)
652:                            + " : " + job;
653:                    return ServiceUtil.returnError(errMsg);
654:                }
655:            }
656:
657:            public static Map cancelJobRetries(DispatchContext dctx, Map context) {
658:                GenericDelegator delegator = dctx.getDelegator();
659:                Security security = dctx.getSecurity();
660:                GenericValue userLogin = (GenericValue) context
661:                        .get("userLogin");
662:                Locale locale = getLocale(context);
663:                if (!security.hasPermission("SERVICE_INVOKE_ANY", userLogin)) {
664:                    String errMsg = UtilProperties.getMessage(
665:                            ServiceUtil.resource,
666:                            "serviceUtil.no_permission_to_run", locale)
667:                            + ".";
668:                    return ServiceUtil.returnError(errMsg);
669:                }
670:
671:                String jobId = (String) context.get("jobId");
672:                Map fields = UtilMisc.toMap("jobId", jobId);
673:
674:                GenericValue job = null;
675:                try {
676:                    job = delegator.findByPrimaryKey("JobSandbox", fields);
677:                    if (job != null) {
678:                        job.set("maxRetry", new Long(0));
679:                        job.store();
680:                    }
681:                } catch (GenericEntityException e) {
682:                    Debug.logError(e, module);
683:                    String errMsg = UtilProperties.getMessage(
684:                            ServiceUtil.resource,
685:                            "serviceUtil.unable_to_cancel_job_retries", locale)
686:                            + " : " + fields;
687:                    return ServiceUtil.returnError(errMsg);
688:                }
689:
690:                Timestamp cancelDate = job.getTimestamp("cancelDateTime");
691:                if (cancelDate != null) {
692:                    return ServiceUtil.returnSuccess();
693:                } else {
694:                    String errMsg = UtilProperties.getMessage(
695:                            ServiceUtil.resource,
696:                            "serviceUtil.unable_to_cancel_job_retries", locale)
697:                            + " : " + job;
698:                    return ServiceUtil.returnError(errMsg);
699:                }
700:            }
701:
702:            public static GenericValue getUserLogin(DispatchContext dctx,
703:                    Map context, String runAsUser) {
704:                GenericValue userLogin = (GenericValue) context
705:                        .get("userLogin");
706:                GenericDelegator delegator = dctx.getDelegator();
707:                if (runAsUser != null) {
708:                    try {
709:                        GenericValue runAs = delegator.findByPrimaryKeyCache(
710:                                "UserLogin", UtilMisc.toMap("userLoginId",
711:                                        runAsUser));
712:                        if (runAs != null) {
713:                            userLogin = runAs;
714:                        }
715:                    } catch (GenericEntityException e) {
716:                        Debug.logError(e, module);
717:                    }
718:                }
719:                return userLogin;
720:            }
721:
722:            private static Locale getLocale(Map context) {
723:                Locale locale = (Locale) context.get("locale");
724:                if (locale == null) {
725:                    locale = Locale.getDefault();
726:                }
727:                return locale;
728:            }
729:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.