1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.chemistry.opencmis.server.impl.atompub;
20
21 import org.apache.chemistry.opencmis.commons.impl.Constants;
22 import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
23
24 import java.math.BigInteger;
25
26 import java.util.GregorianCalendar;
27
28 import javax.xml.stream.XMLStreamException;
29 import javax.xml.stream.XMLStreamWriter;
30
31
32
33
34
35
36
37
38 public class AtomFeed extends AtomDocumentBase
39 {
40 public static final BigInteger DEFAULT_PAGE_SIZE = BigInteger.valueOf( 100 );
41
42
43
44
45 public AtomFeed( )
46 {
47 }
48
49
50
51
52 public AtomFeed( XMLStreamWriter writer )
53 {
54 setWriter( writer );
55 }
56
57
58
59
60 public void startFeed( boolean isRoot ) throws XMLStreamException
61 {
62 getWriter( ).writeStartElement( Constants.NAMESPACE_ATOM, "feed" );
63
64 if ( isRoot )
65 {
66 writeNamespace( Constants.NAMESPACE_ATOM );
67 writeNamespace( Constants.NAMESPACE_CMIS );
68 writeNamespace( Constants.NAMESPACE_RESTATOM );
69 writeNamespace( Constants.NAMESPACE_APP );
70 }
71 }
72
73
74
75
76 public void startChildren( ) throws XMLStreamException
77 {
78 XMLStreamWriter writer = getWriter( );
79 writer.writeStartElement( Constants.NAMESPACE_RESTATOM, "children" );
80 }
81
82
83
84
85 public void endChildren( ) throws XMLStreamException
86 {
87 getWriter( ).writeEndElement( );
88 }
89
90
91
92
93 public void endFeed( ) throws XMLStreamException
94 {
95 getWriter( ).writeEndElement( );
96 }
97
98
99
100
101 public void writeFeedElements( String id, String author, String title, GregorianCalendar updated,
102 String pathSegment, BigInteger numItems ) throws XMLStreamException
103 {
104 writeAuthor( author );
105 writeId( generateAtomId( id ) );
106 writeTitle( title );
107 writeUpdated( updated );
108 writePathSegment( pathSegment );
109 writeNumItems( numItems );
110 }
111
112
113
114
115 public void writeNumItems( BigInteger numItems ) throws XMLStreamException
116 {
117 if ( numItems == null )
118 {
119 return;
120 }
121
122 writeSimpleTag( Constants.NAMESPACE_RESTATOM, "numItems", numItems.toString( ) );
123 }
124
125
126
127
128 public void writePagingLinks( UrlBuilder pagingUrl, BigInteger maxItems, BigInteger skipCount, BigInteger numItems,
129 Boolean hasMoreItems, BigInteger pageSize ) throws XMLStreamException
130 {
131 if ( ( skipCount == null ) || ( skipCount.compareTo( BigInteger.ZERO ) == -1 ) )
132 {
133 skipCount = BigInteger.ZERO;
134 }
135
136 if ( ( maxItems == null ) || ( maxItems.compareTo( BigInteger.ZERO ) == -1 ) )
137 {
138 if ( ( pageSize == null ) || ( pageSize.compareTo( BigInteger.ZERO ) == -1 ) )
139 {
140 maxItems = DEFAULT_PAGE_SIZE;
141 }
142 else
143 {
144 maxItems = pageSize;
145 }
146 }
147
148
149 if ( skipCount.compareTo( BigInteger.ZERO ) == 1 )
150 {
151
152 UrlBuilder firstLink = new UrlBuilder( pagingUrl );
153 firstLink.addParameter( Constants.PARAM_SKIP_COUNT, "0" );
154 firstLink.addParameter( Constants.PARAM_MAX_ITEMS, maxItems );
155 writeFirstLink( firstLink.toString( ) );
156
157
158 UrlBuilder previousLink = new UrlBuilder( pagingUrl );
159 previousLink.addParameter( Constants.PARAM_SKIP_COUNT, skipCount.subtract( maxItems ).max( BigInteger.ZERO ) );
160 previousLink.addParameter( Constants.PARAM_MAX_ITEMS, maxItems );
161 writePreviousLink( previousLink.toString( ) );
162 }
163
164
165 if ( ( hasMoreItems != null ) && hasMoreItems.booleanValue( ) )
166 {
167
168 UrlBuilder nextLink = new UrlBuilder( pagingUrl );
169 nextLink.addParameter( Constants.PARAM_SKIP_COUNT, skipCount.add( maxItems ) );
170 nextLink.addParameter( Constants.PARAM_MAX_ITEMS, maxItems );
171 writeNextLink( nextLink.toString( ) );
172 }
173
174
175 if ( ( numItems != null ) && ( numItems.compareTo( BigInteger.ZERO ) == 1 ) )
176 {
177 BigInteger lastSkip = numItems.subtract( maxItems ).max( BigInteger.ZERO );
178
179 if ( lastSkip.compareTo( BigInteger.ZERO ) == 1 )
180 {
181
182 UrlBuilder lastLink = new UrlBuilder( pagingUrl );
183 lastLink.addParameter( Constants.PARAM_SKIP_COUNT, lastSkip );
184 lastLink.addParameter( Constants.PARAM_MAX_ITEMS, maxItems );
185 writeLastLink( lastLink.toString( ) );
186 }
187 }
188 }
189 }