001package org.kuali.ole.serviceimpl;
002
003import org.kuali.ole.OleSRUConstants;
004import org.kuali.ole.bo.explain.*;
005import org.kuali.ole.handler.OleSRUExplainOperationHandler;
006import org.kuali.ole.service.OleDiagnosticsService;
007import org.kuali.ole.service.OleExplainOperationService;
008import org.kuali.rice.core.api.config.property.Config;
009import org.kuali.rice.core.api.config.property.ConfigContext;
010
011import java.util.Map;
012
013/**
014 * Created with IntelliJ IDEA.
015 * User: ?
016 * Date: 7/9/12
017 * Time: 6:53 PM
018 * To change this template use File | Settings | File Templates.
019 */
020public class OleExplainOperationServiceImpl implements OleExplainOperationService {
021
022
023    public OleSRUExplainOperationHandler oleSRUExplainOperationHandler;
024    public OleDiagnosticsService oleDiagnosticsService;
025    private Config currentContextConfig;
026
027    public OleExplainOperationServiceImpl() {
028        oleSRUExplainOperationHandler = new OleSRUExplainOperationHandler();
029        oleDiagnosticsService = new OleDiagnosticsServiceImpl();
030    }
031
032    /**
033     * will generate the explain service response xml
034     *
035     * @param reqParamMap
036     * @return explain xml response as a string
037     */
038    public String getExplainResponse(Map reqParamMap) {
039        OleSRUExplainResponse oleSRUExplainResponse = new OleSRUExplainResponse();
040        oleSRUExplainResponse.setVersion((String) reqParamMap.get(OleSRUConstants.VERSION));
041        OleSRUExplainRecord oleSRUExplainRecord = getExplainRecord(reqParamMap);
042        oleSRUExplainResponse.setRecord(oleSRUExplainRecord);
043        if (OleSRUConstants.RECORD_PACK_XML.equalsIgnoreCase((String) reqParamMap.get(OleSRUConstants.RECORD_PACKING))) {
044            String explainXML = oleSRUExplainOperationHandler.toXML(oleSRUExplainResponse);
045            String styleSheet = (String) reqParamMap.get(OleSRUConstants.STYLE_SHEET);
046            if (styleSheet != null) {
047                explainXML = explainXML.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n <?xml-stylesheet type=\"text/xsl\" href=\"" + styleSheet + "\"?>");
048            }
049            return explainXML;
050        } else if (OleSRUConstants.RECORD_PACK_STRING.equalsIgnoreCase((String) reqParamMap.get(OleSRUConstants.RECORD_PACKING))) {
051            return oleSRUExplainResponse.toString();
052        }
053        return null;
054    }
055
056    /**
057     * will fetch the values from the property file
058     *
059     * @return OleSRUExplainRecord object
060     */
061    public OleSRUExplainRecord getExplainRecord(Map reqParamMap) {
062        OleSRUExplainRecord oleSRUExplainRecord = new OleSRUExplainRecord();
063        oleSRUExplainRecord.setRecordPacking(OleSRUConstants.RECORD_PACK_XML);
064        oleSRUExplainRecord.setRecordSchema(getCurrentContextConfig().getProperty(OleSRUConstants.EXPLAIN_RECORD_SCHEMA));
065        OleSRUExplainRecordData oleSRUExplainRecordData = getExplainRecordData(reqParamMap);
066        oleSRUExplainRecord.setRecordData(oleSRUExplainRecordData);
067        return oleSRUExplainRecord;
068    }
069
070    private Config getCurrentContextConfig() {
071        if (null == currentContextConfig) {
072            currentContextConfig = ConfigContext.getCurrentContextConfig();
073        }
074        return currentContextConfig;
075    }
076
077    public void setCurrentContextConfig(Config currentContextConfig) {
078        this.currentContextConfig = currentContextConfig;
079    }
080
081    /**
082     * will fetch the values from the property file
083     *
084     * @return OleSRUExplainRecordData object
085     */
086    public OleSRUExplainRecordData getExplainRecordData(Map reqParamMap) {
087        OleSRUExplainRecordData oleSRUExplainRecordData = new OleSRUExplainRecordData();
088        OleSRUExplainOperation oleSRUExplainOperation = new OleSRUExplainOperation();
089        OleSRUExplainServerInfo oleSRUExplainServerInfo = getExplainServerInfo();
090        OleSRUExplainDatabaseInfo oleSRUExplainDatabaseInfo = getExplainDatabaseInfo();
091        OleSRUExplainConfigurationInfo oleSRUExplainConfigurationInfo = getExplainConfigInfo();
092        OleSRUExplainIndexInfo oleSRUExplainIndexInfo = getExplainIndexInfo();
093        OleSRUExplainSchemaInfo oleSRUExplainSchemaInfo = getExplainSchemaInfo();
094        oleSRUExplainOperation.setServerInfo(oleSRUExplainServerInfo);
095        oleSRUExplainOperation.setDatabaseInfo(oleSRUExplainDatabaseInfo);
096        oleSRUExplainOperation.setIndexInfo(oleSRUExplainIndexInfo);
097        oleSRUExplainOperation.setSchemaInfo(oleSRUExplainSchemaInfo);
098        oleSRUExplainOperation.setConfigInfo(oleSRUExplainConfigurationInfo);
099        if (reqParamMap.containsKey(OleSRUConstants.EXTRA_REQ_DATA_KEY))
100            oleSRUExplainOperation.setExtraRequestData(getExtraReqDataInfo(reqParamMap));
101        oleSRUExplainRecordData.setExplain(oleSRUExplainOperation);
102        return oleSRUExplainRecordData;
103    }
104
105    /**
106     * will fetch the values from the property file
107     *
108     * @return oleSRUExplainServerInfo object
109     */
110    public OleSRUExplainServerInfo getExplainServerInfo() {
111        OleSRUExplainServerInfo oleSRUExplainServerInfo = new OleSRUExplainServerInfo();
112        oleSRUExplainServerInfo.setDatabase(getCurrentContextConfig().getProperty(OleSRUConstants.EXPLAIN_SERVER_DATABASE_INFO));
113        oleSRUExplainServerInfo.setHost(getCurrentContextConfig().getProperty(OleSRUConstants.EXPLAIN_SERVER_HOST));
114        oleSRUExplainServerInfo.setMethod(getCurrentContextConfig().getProperty(OleSRUConstants.EXPLAIN_SERVER_METHOD));
115        oleSRUExplainServerInfo.setPort(getCurrentContextConfig().getProperty(OleSRUConstants.EXPLAIN_SERVER_PORT));
116        oleSRUExplainServerInfo.setProtocol(getCurrentContextConfig().getProperty(OleSRUConstants.EXPLAIN_SERVER_PROTOCOL));
117        oleSRUExplainServerInfo.setTransport(getCurrentContextConfig().getProperty(OleSRUConstants.EXPLAIN_SERVER_TRANSPORT));
118        oleSRUExplainServerInfo.setVersion(getCurrentContextConfig().getProperty(OleSRUConstants.EXPLAIN_SERVER_VERSION));
119        oleSRUExplainServerInfo.setValue("");
120        return oleSRUExplainServerInfo;
121    }
122
123    /**
124     * will fetch the values from the property file
125     *
126     * @return oleSRUExplainDatabaseInfo object
127     */
128    public OleSRUExplainDatabaseInfo getExplainDatabaseInfo() {
129        OleSRUExplainDatabaseInfo oleSRUExplainDatabaseInfo = new OleSRUExplainDatabaseInfo();
130        OleSRUExplainDatabaseTitle oleSRUExplainDatabaseTitle = new OleSRUExplainDatabaseTitle();
131        oleSRUExplainDatabaseTitle.setLang(getCurrentContextConfig().getProperty(OleSRUConstants.EXPLAIN_DATABASE_LANG));
132        oleSRUExplainDatabaseTitle.setPrimary(getCurrentContextConfig().getProperty(OleSRUConstants.EXPLAIN_DATABASE_PRIMARY));
133        oleSRUExplainDatabaseTitle.setValue("");
134        oleSRUExplainDatabaseInfo.setTitle(oleSRUExplainDatabaseTitle);
135        return oleSRUExplainDatabaseInfo;
136    }
137
138    /**
139     * will fetch the values from the property file
140     *
141     * @return oleSRUExplainIndexInfo object
142     */
143    public OleSRUExplainIndexInfo getExplainIndexInfo() {
144        OleSRUExplainIndexInfo oleSRUExplainIndexInfo = new OleSRUExplainIndexInfo();
145        OleSRUExplainIndex oleSRUExplainIndex = new OleSRUExplainIndex();
146        OleSRUExplainIndexSet oleSRUExplainIndexSet = new OleSRUExplainIndexSet();
147        oleSRUExplainIndexSet.setName(getCurrentContextConfig().getProperty(OleSRUConstants.EXPLAIN_INDEX_SET_NAME));
148        oleSRUExplainIndexSet.setIdentifier(getCurrentContextConfig().getProperty(OleSRUConstants.EXPLAIN_INDEX_SET_IDENTIFIER));
149        OleSRUExplainIndexMapName oleSRUExplainIndexMapName = new OleSRUExplainIndexMapName();
150        oleSRUExplainIndexMapName.setSet(getCurrentContextConfig().getProperty(OleSRUConstants.EXPLAIN_INDEX_MAP_NAME_SET));
151        oleSRUExplainIndexMapName.setValue(getCurrentContextConfig().getProperty(OleSRUConstants.EXPLAIN_INDEX_MAP_NAME_VALUE));
152        OleSRUExplainIndexMap oleSRUExplainIndexMap = new OleSRUExplainIndexMap();
153        oleSRUExplainIndexMap.setName(oleSRUExplainIndexMapName);
154        oleSRUExplainIndex.setIndexMap(oleSRUExplainIndexMap);
155        oleSRUExplainIndexInfo.setIndex(oleSRUExplainIndex);
156        oleSRUExplainIndexInfo.setSet(oleSRUExplainIndexSet);
157/*        oleSRUExplainIndexSet.setName("bath");
158        oleSRUExplainIndexSet.setIdentifier("info:srw/cql-context-set/1/dc-v1.1");    */
159        oleSRUExplainIndexSet.setValue("");
160        return oleSRUExplainIndexInfo;
161    }
162
163    /**
164     * will fetch the values from the property file
165     *
166     * @return oleSRUExplainSchemaInfo object
167     */
168    public OleSRUExplainSchemaInfo getExplainSchemaInfo() {
169        OleSRUExplainSchemaInfo oleSRUExplainSchemaInfo = new OleSRUExplainSchemaInfo();
170        OleSRUExplainSchema oleSRUExplainSchema = new OleSRUExplainSchema();
171        oleSRUExplainSchema.setTitle(getCurrentContextConfig().getProperty(OleSRUConstants.EXPLAIN_SCHEMA_TITLE));
172        oleSRUExplainSchema.setName(getCurrentContextConfig().getProperty(OleSRUConstants.EXPLAIN_SCHEMA_NAME));
173        oleSRUExplainSchema.setIdentifier(getCurrentContextConfig().getProperty(OleSRUConstants.EXPLAIN_SCHEMA_IDENTIFIER));
174        oleSRUExplainSchema.setValue("");
175        oleSRUExplainSchemaInfo.setSchema(oleSRUExplainSchema);
176        return oleSRUExplainSchemaInfo;
177    }
178
179    /**
180     * will fetch the values from the property file
181     *
182     * @return OleSRUExplainConfigurationInfo object
183     */
184    public OleSRUExplainConfigurationInfo getExplainConfigInfo() {
185        OleSRUExplainConfigurationInfo oleSRUExplainConfigurationInfo = new OleSRUExplainConfigurationInfo();
186        OleSRUExplainConfigDefaultTagField oleSRUExplainConfigDefaultTagField = new OleSRUExplainConfigDefaultTagField();
187        OleSRUExplainConfigSettingTagField oleSRUExplainConfigSettingTagField = new OleSRUExplainConfigSettingTagField();
188        OleSRUExplainConfigSupportTagField oleSRUExplainConfigSupportTagField = new OleSRUExplainConfigSupportTagField();
189        oleSRUExplainConfigDefaultTagField.setType("numberOfRecords");
190        oleSRUExplainConfigDefaultTagField.setValue(Integer.parseInt(getCurrentContextConfig().getProperty(OleSRUConstants.STARTRECORD)));
191        oleSRUExplainConfigurationInfo.setDefaultValue(oleSRUExplainConfigDefaultTagField);
192        oleSRUExplainConfigSettingTagField.setType("maximumRecords");
193        oleSRUExplainConfigSettingTagField.setValue(Integer.parseInt(getCurrentContextConfig().getProperty(OleSRUConstants.MAXRECORD)));
194        oleSRUExplainConfigurationInfo.setSetting(oleSRUExplainConfigSettingTagField);
195        oleSRUExplainConfigSupportTagField.setType("proximity");
196        oleSRUExplainConfigSupportTagField.setValue("");
197        oleSRUExplainConfigurationInfo.setSupports(oleSRUExplainConfigSupportTagField);
198        return oleSRUExplainConfigurationInfo;
199    }
200
201    public String getExtraReqDataInfo(Map reqParamMap) {
202
203        return "<theo:" + reqParamMap.get(OleSRUConstants.EXTRA_REQ_DATA_KEY) + " xmlns:theo=\"" + getCurrentContextConfig().getProperty(OleSRUConstants.EXTRA_REQ_DATA_XML_NAMESPACE) + "\">\n" +
204                reqParamMap.get(OleSRUConstants.EXTRA_REQ_DATA_VALUE) + "\n" +
205                "</theo:" + reqParamMap.get(OleSRUConstants.EXTRA_REQ_DATA_KEY) + ">";
206    }
207
208}