854a20f43ee290a6f121ab0383b737e9019f322b
[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 Formatter get(Format format) throws AAIException {
50         return get(format, new MultivaluedHashMap<String, String>());
51     }
52
53     public Formatter get(Format format, MultivaluedMap<String, String> params) throws AAIException {
54
55         Formatter formatter = null;
56
57         switch (format) {
58             case graphson:
59                 formatter = new Formatter(inject(new GraphSON(), params));
60                 break;
61             case pathed:
62                 formatter = new Formatter(inject(new PathedURL(loader, urlBuilder), params));
63                 break;
64             case pathed_resourceversion:
65                 formatter = new Formatter(inject(new PathedURL(loader, urlBuilder).includeUrl(), params));
66                 break;
67             case id:
68                 formatter = new Formatter(inject(new IdURL(loader, urlBuilder), params));
69                 break;
70             case resource:
71                 formatter = new Formatter(inject(new Resource.Builder(loader, serializer, urlBuilder), params).build());
72                 break;
73             case resource_and_url:
74                 formatter = new Formatter(
75                         inject(new Resource.Builder(loader, serializer, urlBuilder).includeUrl(), params).build());
76                 break;
77             case raw:
78                 formatter =
79                         new Formatter(inject(new RawFormat.Builder(loader, serializer, urlBuilder), params).build());
80                 break;
81             case simple:
82                 formatter = new Formatter(
83                         inject(new RawFormat.Builder(loader, serializer, urlBuilder).depth(0).modelDriven(), params)
84                                 .build());
85                 break;
86             case console:
87                 formatter = new Formatter(inject(new Console(), params));
88                 break;
89             case count:
90                 formatter = new Formatter(inject(new Count(), params));
91                 break;
92             case resource_with_sot:
93                 formatter = new Formatter(
94                         inject(new ResourceWithSoT.Builder(loader, serializer, urlBuilder), params).build());
95                 break;
96             default:
97                 break;
98
99         }
100
101         return formatter;
102     }
103
104     private <T> T inject(T obj, MultivaluedMap<String, String> params) throws QueryParamInjectionException {
105
106         injector.injectParams(obj, params);
107         return obj;
108     }
109
110 }