KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jca > cci > object > MappingRecordOperation


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jca.cci.object;
18
19 import java.sql.SQLException JavaDoc;
20
21 import javax.resource.ResourceException JavaDoc;
22 import javax.resource.cci.ConnectionFactory JavaDoc;
23 import javax.resource.cci.InteractionSpec JavaDoc;
24 import javax.resource.cci.Record JavaDoc;
25 import javax.resource.cci.RecordFactory JavaDoc;
26
27 import org.springframework.dao.DataAccessException;
28 import org.springframework.jca.cci.core.RecordCreator;
29 import org.springframework.jca.cci.core.RecordExtractor;
30
31 /**
32  * EIS operation object that expects mapped input and output objects,
33  * converting to and from CCI Records.
34  *
35  * <p>Concrete subclasses must implement the abstract
36  * <code>createInputRecord(RecordFactory, Object)</code> and
37  * <code>extractOutputData(Record)</code> methods, to create an input
38  * Record from an object and to convert an output Record into an object,
39  * respectively.
40  *
41  * @author Thierry Templier
42  * @author Juergen Hoeller
43  * @since 1.2
44  * @see #createInputRecord(javax.resource.cci.RecordFactory, Object)
45  * @see #extractOutputData(javax.resource.cci.Record)
46  */

47 public abstract class MappingRecordOperation extends EisOperation {
48
49     /**
50      * Constructor that allows use as a JavaBean.
51      */

52     public MappingRecordOperation() {
53     }
54
55     /**
56      * Convenient constructor with ConnectionFactory and specifications
57      * (connection and interaction).
58      * @param connectionFactory ConnectionFactory to use to obtain connections
59      */

60     public MappingRecordOperation(ConnectionFactory JavaDoc connectionFactory, InteractionSpec JavaDoc interactionSpec) {
61         getCciTemplate().setConnectionFactory(connectionFactory);
62         setInteractionSpec(interactionSpec);
63     }
64
65     /**
66      * Set a RecordCreator that should be used for creating default output Records.
67      * <p>Default is none: CCI's <code>Interaction.execute</code> variant
68      * that returns an output Record will be called.
69      * <p>Specify a RecordCreator here if you always need to call CCI's
70      * <code>Interaction.execute</code> variant with a passed-in output Record.
71      * This RecordCreator will then be invoked to create a default output Record instance.
72      * @see javax.resource.cci.Interaction#execute(javax.resource.cci.InteractionSpec, Record)
73      * @see javax.resource.cci.Interaction#execute(javax.resource.cci.InteractionSpec, Record, Record)
74      * @see org.springframework.jca.cci.core.CciTemplate#setOutputRecordCreator
75      */

76     public void setOutputRecordCreator(RecordCreator creator) {
77         getCciTemplate().setOutputRecordCreator(creator);
78     }
79
80     /**
81      * Execute the interaction encapsulated by this operation object.
82      * @param inputObject the input data, to be converted to a Record
83      * by the <code>createInputRecord</code> method
84      * @return the output data extracted with the <code>extractOutputData</code> method
85      * @throws DataAccessException if there is any problem
86      * @see #createInputRecord
87      * @see #extractOutputData
88      */

89     public Object JavaDoc execute(Object JavaDoc inputObject) throws DataAccessException {
90         return getCciTemplate().execute(
91                 getInteractionSpec(), new RecordCreatorImpl(inputObject), new RecordExtractorImpl());
92     }
93
94
95     /**
96      * Subclasses must implement this method to generate an input Record
97      * from an input object passed into the <code>execute</code> method.
98      * @param inputObject the passed-in input object
99      * @return the CCI input Record
100      * @throws ResourceException if thrown by a CCI method, to be auto-converted
101      * to a DataAccessException
102      * @see #execute(Object)
103      */

104     protected abstract Record JavaDoc createInputRecord(RecordFactory JavaDoc recordFactory, Object JavaDoc inputObject)
105             throws ResourceException JavaDoc, DataAccessException;
106
107     /**
108      * Subclasses must implement this method to convert the Record returned
109      * by CCI execution into a result object for the <code>execute</code> method.
110      * @param outputRecord the Record returned by CCI execution
111      * @return the result object
112      * @throws ResourceException if thrown by a CCI method, to be auto-converted
113      * to a DataAccessException
114      * @see #execute(Object)
115      */

116     protected abstract Object JavaDoc extractOutputData(Record JavaDoc outputRecord)
117             throws ResourceException JavaDoc, SQLException JavaDoc, DataAccessException;
118
119
120     /**
121      * Implementation of RecordCreator that calls the enclosing
122      * class's <code>createInputRecord</code> method.
123      */

124     protected class RecordCreatorImpl implements RecordCreator {
125
126         private final Object JavaDoc inputObject;
127
128         public RecordCreatorImpl(Object JavaDoc inObject) {
129             this.inputObject = inObject;
130         }
131
132         public Record JavaDoc createRecord(RecordFactory JavaDoc recordFactory) throws ResourceException JavaDoc, DataAccessException {
133             return createInputRecord(recordFactory, this.inputObject);
134         }
135     }
136
137
138     /**
139      * Implementation of RecordExtractor that calls the enclosing
140      * class's <code>extractOutputData</code> method.
141      */

142     protected class RecordExtractorImpl implements RecordExtractor {
143
144         public Object JavaDoc extractData(Record JavaDoc record) throws ResourceException JavaDoc, SQLException JavaDoc, DataAccessException {
145             return extractOutputData(record);
146         }
147     }
148
149 }
150
Popular Tags