6e126486397388e0d1cede57fb3f393c1fde38ec
[policy/gui.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 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 package org.onap.policy.gui.editors.apex.rest.handling;
23
24 import org.onap.policy.apex.model.modelapi.ApexApiResult;
25 import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
26 import org.onap.policy.apex.model.modelapi.ApexModel;
27 import org.onap.policy.apex.model.modelapi.ApexModelFactory;
28
29 /**
30  * This class represents an ongoing editor session in the Apex editor and holds
31  * the information for the session.
32  *
33  */
34 public class RestSession {
35     // The ID of the session
36     private int sessionId;
37
38     // The Apex policy model of the session
39     private ApexModel apexModel;
40
41     // The Apex policy model being edited
42     private ApexModel apexModelEdited;
43
44     public RestSession(final int sessionId) {
45         this.sessionId = sessionId;
46         this.apexModel = new ApexModelFactory().createApexModel(null, true);
47     }
48
49     /**
50      * Commence making changes to the Apex model.
51      *
52      * @return the result of the edit commencement operation
53      */
54     public synchronized ApexApiResult editModel() {
55         if (apexModelEdited != null) {
56             return new ApexApiResult(Result.FAILED, "model is already being edited");
57         }
58
59         apexModelEdited = apexModel.clone();
60         return new ApexApiResult();
61     }
62
63     /**
64      * Commit the changes to the Apex model.
65      *
66      * @return the result of the commit operation
67      */
68     public synchronized ApexApiResult commitChanges() {
69         if (apexModelEdited == null) {
70             return new ApexApiResult(Result.FAILED, "model is not being edited");
71         }
72
73         apexModel = apexModelEdited;
74         apexModelEdited = null;
75         return new ApexApiResult();
76     }
77
78     /**
79      * Discard the changes to the Apex model.
80      *
81      * @return the result of the discard operation
82      */
83     public synchronized ApexApiResult discardChanges() {
84         if (apexModelEdited == null) {
85             return new ApexApiResult(Result.FAILED, "model is not being edited");
86         }
87
88         apexModelEdited = null;
89         return new ApexApiResult();
90     }
91
92     /**
93      * Finish a session by committing or discarding the changes.
94      *
95      * @param commitFlag if ture, commit changes otherwise discard them
96      */
97     public void finishSession(boolean commitFlag) {
98         if (commitFlag) {
99             commitChanges();
100         } else {
101             discardChanges();
102         }
103     }
104
105     /**
106      * Get the session ID of the session.
107      *
108      * @return the sessionId
109      */
110     public int getSessionId() {
111         return sessionId;
112     }
113
114     /**
115      * Get the Apex model of the session.
116      *
117      * @return the apexModel
118      */
119     public ApexModel getApexModel() {
120         return apexModel;
121     }
122
123     /**
124      * Get the edited Apex model of the session.
125      *
126      * @return the apexModel
127      */
128     public ApexModel getApexModelEdited() {
129         return apexModelEdited;
130     }
131 }