KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > jcr > query > PropertyMapRowImpl


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.jcr.query;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.jcr.ItemNotFoundException;
23 import javax.jcr.Node;
24 import javax.jcr.PropertyType;
25 import javax.jcr.RepositoryException;
26 import javax.jcr.Value;
27 import javax.jcr.nodetype.PropertyDefinition;
28 import javax.jcr.query.Row;
29
30 import org.alfresco.jcr.item.NodeImpl;
31 import org.alfresco.jcr.item.ValueImpl;
32 import org.alfresco.jcr.session.SessionImpl;
33 import org.alfresco.service.cmr.repository.NodeRef;
34 import org.alfresco.service.namespace.QName;
35
36
37 /**
38  * Node Ref based Row
39  *
40  * @author David Caruana
41  */

42 public class PropertyMapRowImpl implements Row
43 {
44     private SessionImpl session;
45     private Map JavaDoc<QName, PropertyDefinition> columns;
46     private NodeRef nodeRef;
47     private Map JavaDoc<QName, Serializable JavaDoc> properties;
48
49     
50     /**
51      * Construct
52      *
53      * @param session
54      * @param columnNames
55      * @param properties
56      */

57     public PropertyMapRowImpl(SessionImpl session, Map JavaDoc<QName, PropertyDefinition> columns, NodeRef nodeRef, Map JavaDoc<QName, Serializable JavaDoc> properties)
58     {
59         this.session = session;
60         this.columns = columns;
61         this.nodeRef = nodeRef;
62         this.properties = properties;
63     }
64     
65     /* (non-Javadoc)
66      * @see javax.jcr.query.Row#getValues()
67      */

68     public Value[] getValues() throws RepositoryException
69     {
70         Value[] values = new Value[columns.size() + 2];
71         
72         int i = 0;
73         for (QName propertyName : columns.keySet())
74         {
75             values[i++] = createValue(propertyName);
76         }
77         return values;
78     }
79
80     /* (non-Javadoc)
81      * @see javax.jcr.query.Row#getValue(java.lang.String)
82      */

83     public Value getValue(String JavaDoc propertyName) throws ItemNotFoundException, RepositoryException
84     {
85         QName propertyQName = QName.createQName(propertyName, session.getNamespaceResolver());
86         if (!columns.containsKey(propertyQName))
87         {
88             throw new ItemNotFoundException("Column " + propertyName + " does not exist");
89         }
90         return createValue(propertyQName);
91     }
92     
93     /**
94      * Create a Value for specified property name
95      *
96      * @param propertyName
97      * @return
98      * @throws RepositoryException
99      */

100     private Value createValue(QName propertyName)
101         throws RepositoryException
102     {
103         Value value = null;
104         if (propertyName.equals(QueryManagerImpl.JCRPATH_COLUMN))
105         {
106             // derive path from node ref
107
Node node = new NodeImpl(session, nodeRef).getProxy();
108             value = new ValueImpl(session, PropertyType.STRING, node.getPath());
109         }
110         else if (propertyName.equals(QueryManagerImpl.JCRSCORE_COLUMN))
111         {
112             // TODO:
113
// create dummy score
114
value = new ValueImpl(session, PropertyType.LONG, (long)0);
115         }
116         else
117         {
118             // create value from node properties
119
Object JavaDoc objValue = properties.get(propertyName);
120             if (objValue != null)
121             {
122                 PropertyDefinition propDef = columns.get(propertyName);
123                 value = new ValueImpl(session, propDef.getRequiredType(), objValue);
124             }
125         }
126         return value;
127     }
128     
129 }
130
Popular Tags