summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/branches/sca-java-1.6.2/modules/binding-gdata-runtime/src/main/java/org/apache/tuscany/sca/binding/gdata/provider/GdataBindingUtil.java
blob: 3d476df764d17ed3d681b01b23173511fb0c649e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

package org.apache.tuscany.sca.binding.gdata.provider;

import java.util.Date;

import org.apache.tuscany.sca.data.collection.Entry;
import org.apache.tuscany.sca.data.collection.Item;
import org.apache.tuscany.sca.databinding.Mediator;
import org.apache.tuscany.sca.interfacedef.DataType;

import com.google.gdata.data.DateTime;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.TextContent;

/**
 * Utility methods used in this package.
 * 
 * @version $Rev$ $Date$
 */
class GdataBindingUtil {

    /**
     * Create a data item from an GData entry.
     * 
     * @param feedEntry
     * @param itemClassType
     * @param itemXMLType
     * @param mediator
     * @return
     */
    static Entry<Object, Object> entry(com.google.gdata.data.Entry feedEntry,
                                       DataType<?> itemClassType,
                                       DataType<?> itemXMLType,
                                       Mediator mediator) {
        if (feedEntry != null) {
            if (itemClassType.getPhysical() == Item.class) {
                String key = feedEntry.getId().toString();

                Item item = new Item();
                item.setTitle(feedEntry.getTitle().toString());
                TextContent content = (TextContent)feedEntry.getContent();
                item.setContents(content.getContent().getPlainText());

                for (com.google.gdata.data.Link link : feedEntry.getLinks()) {
                    if (link.getRel() == null || "self".equals(link.getRel())) {
                        if (item.getLink() == null) {
                            item.setLink(link.getHref().toString());
                        }
                    } else if ("related".equals(link.getRel())) {
                        item.setRelated(link.getHref().toString());
                    } else if ("alternate".equals(link.getRel())) {
                        item.setAlternate(link.getHref().toString());
                    }
                }

                Date date = new Date(feedEntry.getUpdated().getValue());
                item.setDate(date);
                return new Entry<Object, Object>(key, item);

            } else {
                String key = null;
                if (feedEntry.getId() != null) {
                    key = feedEntry.getId();
                }

                // Create the item from XML
                if (feedEntry.getContent() == null) {
                    return null;
                }

                TextContent content = (TextContent)feedEntry.getContent();
                String value = content.getContent().getPlainText();
                Object data = mediator.mediate(value, itemXMLType, itemClassType, null);
                return new Entry<Object, Object>(key, data);
            }
        } else {
            return null;
        }
    }

    /**
     * Create an Gdata entry for a key and item from a collection.
     * 
     * @param entry
     * @param itemClassType
     * @param itemXMLType
     * @param mediator
     * @param factory
     * @return
     */
    static com.google.gdata.data.Entry feedEntry(Entry<Object, Object> entry,
                                                 DataType<?> itemClassType,
                                                 DataType<?> itemXMLType,
                                                 Mediator mediator) {

        Object key = entry.getKey();
        Object data = entry.getData();
        if (data instanceof Item) {
            Item item = (Item)data;

            com.google.gdata.data.Entry feedEntry = new com.google.gdata.data.Entry();
            if (key != null) {
                feedEntry.setId(key.toString());
            }
            feedEntry.setTitle(new PlainTextConstruct(item.getTitle()));
            feedEntry.setContent(new PlainTextConstruct(item.getContents()));

            String href = item.getLink();
            if (href == null && key != null) {
                href = key.toString();
            }

            if (href != null) {
                feedEntry.addHtmlLink(href, "", "");
            }
            String related = item.getRelated();
            if (related != null) {
                feedEntry.addHtmlLink(related, "", "related");
            }
            String alternate = item.getAlternate();
            if (alternate != null) {
                feedEntry.addHtmlLink(alternate, "", "alternate");
            }

            Date date = item.getDate();
            if (date != null) {
                DateTime datetime = new DateTime(date);
                feedEntry.setUpdated(datetime);
            }
            return feedEntry;

        } else if (data != null) {

            com.google.gdata.data.Entry feedEntry = new com.google.gdata.data.Entry();
            feedEntry.setId(key.toString());
            feedEntry.setTitle(new PlainTextConstruct("item"));

            // Convert the item to XML
            String value = mediator.mediate(data, itemClassType, itemXMLType, null).toString();

            // Might be wrong because the example uses XML datatype, I am using
            // plainText here
            feedEntry.setContent(new PlainTextConstruct(value));

            feedEntry.addHtmlLink(key.toString(), "", "");
            return feedEntry;

        } else {
            return null;
        }
    }

}