01: package com.completex.objective.components.persistency.io.sdl.impl;
02:
03: import com.completex.objective.components.persistency.BasicQuery;
04: import com.completex.objective.components.persistency.Call;
05: import com.completex.objective.components.persistency.Mappable;
06: import com.completex.objective.components.persistency.Query;
07: import com.completex.objective.components.persistency.QueryCtl;
08: import com.completex.objective.components.persistency.OdalRuntimePersistencyException;
09: import com.completex.objective.components.persistency.io.sdl.QuerySdlConverter;
10: import com.completex.objective.components.persistency.core.impl.query.BaseQueryImpl;
11: import com.completex.objective.components.persistency.core.impl.query.CallImpl;
12: import com.completex.objective.components.persistency.core.impl.query.QueryDefinitionImpl;
13: import com.completex.objective.components.persistency.core.impl.query.QueryImpl;
14: import com.completex.objective.components.sdl.reader.impl.SdlReaderImpl;
15: import com.completex.objective.components.sdl.writer.impl.SdlPrinterImpl;
16: import com.completex.objective.util.PropertyMap;
17:
18: import java.io.IOException;
19: import java.io.StringReader;
20: import java.io.StringWriter;
21: import java.io.Writer;
22: import java.io.Reader;
23: import java.util.Map;
24:
25: /**
26: * @author Gennady Krizhevsky
27: */
28: public class QuerySdlConverterImpl implements QuerySdlConverter {
29: private SdlReaderImpl sdlReader = new SdlReaderImpl();
30: private SdlPrinterImpl sdlPrinter = new SdlPrinterImpl();
31:
32: public void toSdl(Query query, Writer writer) throws IOException {
33: toSdl(((Mappable) query), writer);
34: }
35:
36: public void toSdl(Call call, Writer writer) throws IOException {
37: toSdl(((Mappable) call), writer);
38: }
39:
40: public String toSdl(Query query) {
41: try {
42: Mappable mappable = (Mappable) query;
43: return toSdl(mappable);
44: } catch (IOException e) {
45: throw new OdalRuntimePersistencyException(
46: "Cannot convert query to sdl: " + query, e);
47: }
48: }
49:
50: public String toSdl(Call call) {
51: try {
52: Mappable mappable = (Mappable) call;
53: return toSdl(mappable);
54: } catch (IOException e) {
55: throw new OdalRuntimePersistencyException(
56: "Cannot convert call to sdl: " + call, e);
57: }
58: }
59:
60: public BasicQuery fromSdl(String sdl) throws IOException {
61: StringReader reader = new StringReader(sdl);
62: return fromSdl(reader);
63: }
64:
65: public BasicQuery fromSdl(Reader reader) throws IOException {
66: Map map = (Map) sdlReader.read(reader);
67: PropertyMap propertyMap = PropertyMap.toPropertyMap(map, true);
68:
69: PropertyMap queryDefMap = propertyMap.getPropertyMap(
70: BaseQueryImpl.TAG_QUERY_DEFINITION, true);
71: boolean call = queryDefMap
72: .getBoolean(QueryDefinitionImpl.TAG_CALL);
73:
74: BasicQuery query;
75: if (call) {
76: query = new CallImpl(map);
77: } else {
78: query = new QueryImpl(map);
79: }
80: return query;
81: }
82:
83: protected String toSdl(Mappable mappable) throws IOException {
84: Writer stringWriter = new StringWriter();
85: if (mappable != null) {
86: toSdl(mappable, stringWriter);
87: return stringWriter.toString();
88: } else {
89: return null;
90: }
91: }
92:
93: protected void toSdl(Mappable mappable, Writer stringWriter)
94: throws IOException {
95: Map queryMap = mappable.toMap();
96: sdlPrinter.write(stringWriter, queryMap);
97: }
98:
99: }
|