359bbd4250aedd8d50327513b0360829b8802370
[policy/apex-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 var returnValueType = Java.type("java.lang.Boolean");
23 var returnValue = new returnValueType(true);
24
25 // Load compatibility script for imports etc
26 load("nashorn:mozilla_compat.js");
27 importPackage(java.text);
28 importClass(java.text.SimpleDateFormat);
29
30 executor.logger.info("Task Execution: '"+executor.subject.id+"'. Input Fields: '"+executor.inFields+"'");
31
32 executor.outFields.put("amount"      , executor.inFields.get("amount"));
33 executor.outFields.put("assistant_ID", executor.inFields.get("assistant_ID"));
34 executor.outFields.put("notes"       , executor.inFields.get("notes"));
35 executor.outFields.put("quantity"    , executor.inFields.get("quantity"));
36 executor.outFields.put("branch_ID"   , executor.inFields.get("branch_ID"));
37 executor.outFields.put("item_ID"     , executor.inFields.get("item_ID"));
38 executor.outFields.put("time"        , executor.inFields.get("time"));
39 executor.outFields.put("sale_ID"     , executor.inFields.get("sale_ID"));
40
41 item_id = executor.inFields.get("item_ID");
42
43 //All times in this script are in GMT/UTC since the policy and events assume time is in GMT.
44 var timenow_gmt =  new Date(Number(executor.inFields.get("time")));
45
46 var midnight_gmt = new Date(Number(executor.inFields.get("time")));
47 midnight_gmt.setUTCHours(0,0,0,0);
48
49 var eleven30_gmt = new Date(Number(executor.inFields.get("time")));
50 eleven30_gmt.setUTCHours(11,30,0,0);
51
52 var timeformatter = new java.text.SimpleDateFormat("HH:mm:ss z");
53
54 var itemisalcohol = false;
55 if(item_id != null && item_id >=1000 && item_id < 2000)
56     itemisalcohol = true;
57
58 if( itemisalcohol
59     && timenow_gmt.getTime() >= midnight_gmt.getTime()
60     && timenow_gmt.getTime() <  eleven30_gmt.getTime()) {
61
62   executor.outFields.put("authorised", false);
63   executor.outFields.put("message", "Sale not authorised by policy task " +
64     executor.subject.taskName+ " for time " + timeformatter.format(timenow_gmt.getTime()) +
65     ". Alcohol can not be sold between " + timeformatter.format(midnight_gmt.getTime()) +
66     " and " + timeformatter.format(eleven30_gmt.getTime()));
67 }
68 else{
69   executor.outFields.put("authorised", true);
70   executor.outFields.put("message", "Sale authorised by policy task " +
71     executor.subject.taskName + " for time "+timeformatter.format(timenow_gmt.getTime()));
72 }
73
74 /*
75 This task checks if a sale request is for an item that is an alcoholic drink.
76 If the local time is between 00:00:00 GMT and 11:30:00 GMT then the sale is not
77 authorised. Otherwise the sale is authorised.
78 In this implementation we assume that items with item_ID value between 1000 and
79 2000 are all alcoholic drinks :-)
80 */