001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.webdav;
016    
017    import com.liferay.portal.kernel.util.ContentTypes;
018    import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
019    import com.liferay.portal.kernel.util.HttpUtil;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.TimeZoneUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Lock;
025    
026    import java.io.InputStream;
027    
028    import java.text.Format;
029    
030    import java.util.Date;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Alexander Chow
035     */
036    public class BaseResourceImpl implements Resource {
037    
038            public BaseResourceImpl(String parentPath, long name, long displayName) {
039                    this(parentPath, String.valueOf(name), String.valueOf(displayName));
040            }
041    
042            public BaseResourceImpl(String parentPath, long name, String displayName) {
043                    this(parentPath, String.valueOf(name), displayName);
044            }
045    
046            public BaseResourceImpl(
047                    String parentPath, String name, String displayName) {
048    
049                    this(parentPath, name, displayName, null, null);
050            }
051    
052            public BaseResourceImpl(
053                    String parentPath, String name, String displayName, Date createDate,
054                    Date modifiedDate) {
055    
056                    this(parentPath, name, displayName, createDate, modifiedDate, 0);
057            }
058    
059            public BaseResourceImpl(
060                    String parentPath, String name, String displayName, Date createDate,
061                    Date modifiedDate, long size) {
062    
063                    _href = parentPath;
064    
065                    if (Validator.isNotNull(name)) {
066                            _href += StringPool.SLASH + name;
067                    }
068    
069                    _href = HttpUtil.encodePath(_href);
070    
071                    _displayName = displayName;
072    
073                    if (createDate == null) {
074                            _createDate = new Date();
075                    }
076                    else {
077                            _createDate = createDate;
078                    }
079    
080                    if (modifiedDate == null) {
081                            _modifiedDate = new Date();
082                    }
083                    else {
084                            _modifiedDate = _createDate;
085                    }
086    
087                    _size = size;
088            }
089    
090            @Override
091            public String getClassName() {
092                    return _className;
093            }
094    
095            @Override
096            @SuppressWarnings("unused")
097            public InputStream getContentAsStream() throws WebDAVException {
098                    return null;
099            }
100    
101            @Override
102            public String getContentType() {
103                    return ContentTypes.HTTPD_UNIX_DIRECTORY;
104            }
105    
106            @Override
107            public String getCreateDate() {
108                    return _createDateFormatter.format(_createDate);
109            }
110    
111            @Override
112            public String getDisplayName() {
113                    return _displayName;
114            }
115    
116            @Override
117            public String getHREF() {
118                    return _href;
119            }
120    
121            @Override
122            public Lock getLock() {
123                    return null;
124            }
125    
126            @Override
127            public Object getModel() {
128                    return _model;
129            }
130    
131            @Override
132            public String getModifiedDate() {
133                    return _modifiedDateFormatter.format(_modifiedDate);
134            }
135    
136            @Override
137            public long getPrimaryKey() {
138                    return _primaryKey;
139            }
140    
141            @Override
142            public long getSize() {
143                    return _size;
144            }
145    
146            @Override
147            public boolean isCollection() {
148                    return true;
149            }
150    
151            @Override
152            public boolean isLocked() {
153                    return false;
154            }
155    
156            @Override
157            public void setClassName(String className) {
158                    _className = className;
159            }
160    
161            @Override
162            public void setModel(Object model) {
163                    _model = model;
164            }
165    
166            @Override
167            public void setPrimaryKey(long primaryKey) {
168                    _primaryKey = primaryKey;
169            }
170    
171            private static Format _createDateFormatter =
172                    FastDateFormatFactoryUtil.getSimpleDateFormat(
173                            "yyyy-MM-dd'T'HH:mm:ss'Z'", LocaleUtil.US, TimeZoneUtil.GMT);
174            private static Format _modifiedDateFormatter =
175                    FastDateFormatFactoryUtil.getSimpleDateFormat(
176                            "EEE, dd MMM yyyy HH:mm:ss zzz", LocaleUtil.US, TimeZoneUtil.GMT);
177    
178            private String _className;
179            private Date _createDate;
180            private String _displayName;
181            private String _href;
182            private Object _model;
183            private Date _modifiedDate;
184            private long _primaryKey = -1;
185            private long _size;
186    
187    }