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.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.Lock;
023    
024    import java.io.InputStream;
025    
026    import java.text.Format;
027    
028    import java.util.Date;
029    import java.util.Locale;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     * @author Alexander Chow
034     */
035    public class BaseResourceImpl implements Resource {
036    
037            public BaseResourceImpl(String parentPath, long name, long displayName) {
038                    this(parentPath, String.valueOf(name), String.valueOf(displayName));
039            }
040    
041            public BaseResourceImpl(String parentPath, long name, String displayName) {
042                    this(parentPath, String.valueOf(name), displayName);
043            }
044    
045            public BaseResourceImpl(
046                    String parentPath, String name, String displayName) {
047    
048                    this(parentPath, name, displayName, null, null);
049            }
050    
051            public BaseResourceImpl(
052                    String parentPath, String name, String displayName, Date createDate,
053                    Date modifiedDate) {
054    
055                    this(parentPath, name, displayName, createDate, modifiedDate, 0);
056            }
057    
058            public BaseResourceImpl(
059                    String parentPath, String name, String displayName, Date createDate,
060                    Date modifiedDate, long size) {
061    
062                    _href = parentPath;
063    
064                    if (Validator.isNotNull(name)) {
065                            _href += StringPool.SLASH + name;
066                    }
067    
068                    _href = HttpUtil.encodePath(_href);
069    
070                    _displayName = displayName;
071    
072                    if (createDate == null) {
073                            _createDate = new Date();
074                    }
075                    else {
076                            _createDate = createDate;
077                    }
078    
079                    if (modifiedDate == null) {
080                            _modifiedDate = new Date();
081                    }
082                    else {
083                            _modifiedDate = _createDate;
084                    }
085    
086                    _size = size;
087            }
088    
089            @Override
090            public String getClassName() {
091                    return _className;
092            }
093    
094            @Override
095            @SuppressWarnings("unused")
096            public InputStream getContentAsStream() throws WebDAVException {
097                    return null;
098            }
099    
100            @Override
101            public String getContentType() {
102                    return ContentTypes.HTTPD_UNIX_DIRECTORY;
103            }
104    
105            @Override
106            public String getCreateDate() {
107                    return _createDateFormatter.format(_createDate);
108            }
109    
110            @Override
111            public String getDisplayName() {
112                    return _displayName;
113            }
114    
115            @Override
116            public String getHREF() {
117                    return _href;
118            }
119    
120            @Override
121            public Lock getLock() {
122                    return null;
123            }
124    
125            @Override
126            public Object getModel() {
127                    return _model;
128            }
129    
130            @Override
131            public String getModifiedDate() {
132                    return _modifiedDateFormatter.format(_modifiedDate);
133            }
134    
135            @Override
136            public long getPrimaryKey() {
137                    return _primaryKey;
138            }
139    
140            @Override
141            public long getSize() {
142                    return _size;
143            }
144    
145            @Override
146            public boolean isCollection() {
147                    return true;
148            }
149    
150            @Override
151            public boolean isLocked() {
152                    return false;
153            }
154    
155            @Override
156            public void setClassName(String className) {
157                    _className = className;
158            }
159    
160            @Override
161            public void setModel(Object model) {
162                    _model = model;
163            }
164    
165            @Override
166            public void setPrimaryKey(long primaryKey) {
167                    _primaryKey = primaryKey;
168            }
169    
170            private static Format _createDateFormatter =
171                    FastDateFormatFactoryUtil.getSimpleDateFormat(
172                            "yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
173            private static Format _modifiedDateFormatter =
174                    FastDateFormatFactoryUtil.getSimpleDateFormat(
175                            "EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
176    
177            private String _className;
178            private Date _createDate;
179            private String _displayName;
180            private String _href;
181            private Object _model;
182            private Date _modifiedDate;
183            private long _primaryKey = -1;
184            private long _size;
185    
186    }