031e2a326d4f42cbc1091009e739f11143778fe7
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / serialization / queryformats / FormatFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.aai.serialization.queryformats;
22
23 import javax.ws.rs.core.MultivaluedHashMap;
24 import javax.ws.rs.core.MultivaluedMap;
25
26 import org.openecomp.aai.exceptions.AAIException;
27 import org.openecomp.aai.introspection.Loader;
28 import org.openecomp.aai.serialization.db.DBSerializer;
29 import org.openecomp.aai.serialization.queryformats.exceptions.QueryParamInjectionException;
30 import org.openecomp.aai.serialization.queryformats.utils.QueryParamInjector;
31 import org.openecomp.aai.serialization.queryformats.utils.UrlBuilder;
32
33 public class FormatFactory {
34
35         private final Loader loader;
36         private final DBSerializer serializer;
37         private final UrlBuilder urlBuilder;
38         private final QueryParamInjector injector;
39         public FormatFactory (Loader loader, DBSerializer serializer) throws AAIException {
40                 this.loader = loader;
41                 this.serializer = serializer;
42                 this.urlBuilder = new UrlBuilder(loader.getVersion(), serializer);
43                 this.injector = QueryParamInjector.getInstance();
44         }
45         
46         public Formatter get(Format format) throws AAIException {
47                 return get(format, new MultivaluedHashMap<String, String>());
48         }
49         
50         public Formatter get(Format format, MultivaluedMap<String, String> params) throws AAIException {
51                 
52                 Formatter formattter = null;
53
54                 switch (format) {
55                         case graphson :
56                                 formattter = new Formatter(inject(new GraphSON(), params));
57                                 break;
58                         case pathed :
59                                 formattter = new Formatter(inject(new PathedURL(loader, urlBuilder), params));
60                                 break;
61                         case id :
62                                 formattter = new Formatter(inject(new IdURL(loader, urlBuilder), params));
63                                 break;
64                         case resource :
65                                 formattter = new Formatter(inject(new Resource.Builder(loader, serializer, urlBuilder), params).build());
66                                 break;
67                         case resource_and_url :
68                                 formattter = new Formatter(inject(new Resource.Builder(loader, serializer, urlBuilder).includeUrl(), params).build());
69                                 break;
70                         case raw :
71                                 formattter = new Formatter(inject(new RawFormat.Builder(loader, serializer, urlBuilder), params).build());
72                                 break;
73                         case simple :
74                                 formattter = new Formatter(inject(new RawFormat.Builder(loader, serializer, urlBuilder).depth(0).modelDriven(), params).build());
75                                 break;
76                         case console :
77                                 formattter = new Formatter(inject(new Console(), params));
78                                 break;
79                         default :
80                                 break;
81
82                 }
83                 
84                 return formattter;
85         }
86         
87         private <T> T inject (T obj, MultivaluedMap<String, String> params) throws QueryParamInjectionException {
88                 
89                 injector.injectParams(obj, params);
90                 return obj;
91         }
92         
93 }