2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.client.editor.rest.handling;
23 import org.onap.policy.apex.model.modelapi.ApexApiResult;
24 import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
25 import org.onap.policy.apex.model.modelapi.ApexModel;
26 import org.onap.policy.apex.model.modelapi.ApexModelFactory;
29 * This class represents an ongoing editor session in the Apex editor and holds the information for the session.
32 public class RestSession {
33 // The ID of the session
34 private int sessionId;
36 // The Apex policy model of the session
37 private ApexModel apexModel;
39 // The Apex policy model being edited
40 private ApexModel apexModelEdited;
42 public RestSession(final int sessionId) {
43 this.sessionId = sessionId;
44 this.apexModel = new ApexModelFactory().createApexModel(null, true);
48 * Commence making changes to the Apex model.
49 * @return the result of the edit commencement operation
51 public synchronized ApexApiResult editModel() {
52 if (apexModelEdited != null) {
53 return new ApexApiResult(Result.FAILED, "model is already being edited");
56 apexModelEdited = apexModel.clone();
57 return new ApexApiResult();
61 * Commit the changes to the Apex model.
62 * @return the result of the commit operation
64 public synchronized ApexApiResult commitChanges() {
65 if (apexModelEdited == null) {
66 return new ApexApiResult(Result.FAILED, "model is not being edited");
69 apexModel = apexModelEdited;
70 apexModelEdited = null;
71 return new ApexApiResult();
75 * Discard the changes to the Apex model.
76 * @return the result of the discard operation
78 public synchronized ApexApiResult discardChanges() {
79 if (apexModelEdited == null) {
80 return new ApexApiResult(Result.FAILED, "model is not being edited");
83 apexModelEdited = null;
84 return new ApexApiResult();
89 * Finish a session by committing or discarding the changes.
91 * @param commitFlag if ture, commit changes otherwise discard them
93 public void finishSession(boolean commitFlag) {
103 * Get the session ID of the session.
104 * @return the sessionId
106 public int getSessionId() {
111 * Get the Apex model of the session.
112 * @return the apexModel
114 public ApexModel getApexModel() {
119 * Get the edited Apex model of the session.
120 * @return the apexModel
122 public ApexModel getApexModelEdited() {
123 return apexModelEdited;