Merge "Release 1.14.0 maven artifact"
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / FormatFactory.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.serialization.queryformats;
22
23 import javax.ws.rs.core.MultivaluedHashMap;
24 import javax.ws.rs.core.MultivaluedMap;
25
26 import org.onap.aai.exceptions.AAIException;
27 import org.onap.aai.introspection.Loader;
28 import org.onap.aai.serialization.db.DBSerializer;
29 import org.onap.aai.serialization.queryformats.exceptions.QueryParamInjectionException;
30 import org.onap.aai.serialization.queryformats.utils.QueryParamInjector;
31 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
32 import org.onap.aai.setup.SchemaVersions;
33
34 public class FormatFactory {
35
36     private final Loader loader;
37     private final DBSerializer serializer;
38     private final UrlBuilder urlBuilder;
39     private final QueryParamInjector injector;
40
41     public FormatFactory(Loader loader, DBSerializer serializer, SchemaVersions schemaVersions, String basePath)
42             throws AAIException {
43         this.loader = loader;
44         this.serializer = serializer;
45         this.urlBuilder = new UrlBuilder(loader.getVersion(), serializer, schemaVersions, basePath);
46         this.injector = QueryParamInjector.getInstance();
47     }
48
49     public FormatFactory(Loader loader, DBSerializer serializer, SchemaVersions schemaVersions, String basePath,
50             String serverBase) throws AAIException {
51         this.loader = loader;
52         this.serializer = serializer;
53         this.urlBuilder = new UrlBuilder(loader.getVersion(), serializer, serverBase, schemaVersions, basePath);
54         this.injector = QueryParamInjector.getInstance();
55     }
56
57     public Formatter get(Format format) throws AAIException {
58         return get(format, new MultivaluedHashMap<>());
59     }
60
61     public Formatter get(Format format, MultivaluedMap<String, String> params) throws AAIException {
62
63         Formatter formatter = null;
64
65         switch (format) {
66             case graphson:
67                 formatter = new Formatter(inject(new GraphSON(), params));
68                 break;
69             case pathed:
70                 formatter = new Formatter(inject(new PathedURL.Builder(loader, serializer, urlBuilder), params).build(),
71                         params);
72                 break;
73             case pathed_resourceversion:
74                 formatter = new Formatter(
75                         inject(new PathedURL.Builder(loader, serializer, urlBuilder).includeUrl(), params).build(),
76                         params);
77                 break;
78             case id:
79                 formatter = new Formatter(inject(new IdURL.Builder(loader, serializer, urlBuilder), params).build(),
80                         params);
81                 break;
82             case resource:
83                 formatter = new Formatter(
84                         inject(new Resource.Builder(loader, serializer, urlBuilder, params), params).build(), params);
85                 break;
86             case resource_and_url:
87                 formatter = new Formatter(
88                         inject(new Resource.Builder(loader, serializer, urlBuilder, params).includeUrl(), params)
89                                 .build(),
90                         params);
91                 break;
92             case raw:
93                 formatter = new Formatter(inject(new RawFormat.Builder(loader, serializer, urlBuilder), params).build(),
94                         params);
95                 break;
96             case simple:
97                 formatter = new Formatter(
98                         inject(new RawFormat.Builder(loader, serializer, urlBuilder).depth(0).modelDriven(), params)
99                                 .build(),
100                         params);
101                 break;
102             case aggregate:
103                 formatter = new Formatter(
104                         inject(new Aggregate.Builder(loader, serializer, urlBuilder).depth(0).modelDriven(), params)
105                                 .build(),
106                         params);
107                 break;
108             case console:
109                 formatter = new Formatter(inject(new Console(), params));
110                 break;
111             case count:
112                 formatter = new Formatter(inject(new Count(), params));
113                 break;
114             case resource_with_sot:
115                 formatter = new Formatter(
116                         inject(new ResourceWithSoT.Builder(loader, serializer, urlBuilder), params).build(), params);
117                 break;
118             case changes:
119                 formatter = new Formatter(inject(new ChangesFormat(), params));
120                 break;
121             case state:
122                 formatter = new Formatter(
123                         inject(new StateFormat.Builder(loader, serializer, urlBuilder), params).build(format));
124                 break;
125             case lifecycle:
126                 formatter = new Formatter(
127                         inject(new LifecycleFormat.Builder(loader, serializer, urlBuilder), params).build(format));
128                 break;
129             case tree:
130                 formatter =
131                         new Formatter(inject(new TreeFormat.Builder(loader, serializer, urlBuilder), params).build());
132                 break;
133             default:
134                 break;
135
136         }
137
138         return formatter;
139     }
140
141     private <T> T inject(T obj, MultivaluedMap<String, String> params) throws QueryParamInjectionException {
142
143         injector.injectParams(obj, params);
144         return obj;
145     }
146
147 }