001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/common/api/src/java/org/theospi/portfolio/guidance/model/GuidanceItemAttachment.java $
003: * $Id:GuidanceItemAttachment.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.theospi.portfolio.guidance.model;
021:
022: import org.sakaiproject.content.api.ContentResource;
023: import org.sakaiproject.entity.api.Reference;
024: import org.sakaiproject.metaobj.shared.mgt.ReferenceHolder;
025: import org.sakaiproject.metaobj.shared.model.IdentifiableObject;
026: import org.sakaiproject.metaobj.shared.model.MimeType;
027:
028: /**
029: * Created by IntelliJ IDEA.
030: * User: John Ellis
031: * Date: Nov 11, 2005
032: * Time: 12:38:19 PM
033: * To change this template use File | Settings | File Templates.
034: */
035: public class GuidanceItemAttachment extends IdentifiableObject {
036:
037: private GuidanceItem item;
038: private ReferenceHolder baseReference;
039: private ReferenceHolder fullReference;
040:
041: public GuidanceItemAttachment() {
042: }
043:
044: public GuidanceItemAttachment(GuidanceItem item,
045: Reference baseReference, Reference fullReference) {
046: this .item = item;
047: this .baseReference = new ReferenceHolder(baseReference);
048: this .fullReference = new ReferenceHolder(fullReference);
049: }
050:
051: public GuidanceItem getItem() {
052: return item;
053: }
054:
055: public void setItem(GuidanceItem item) {
056: this .item = item;
057: }
058:
059: public ReferenceHolder getBaseReference() {
060: return baseReference;
061: }
062:
063: public void setBaseReference(ReferenceHolder baseReference) {
064: this .baseReference = baseReference;
065: }
066:
067: public void setBaseReference(Reference baseReference) {
068: this .baseReference = new ReferenceHolder(baseReference);
069: }
070:
071: public ReferenceHolder getFullReference() {
072: return fullReference;
073: }
074:
075: public void setFullReference(Reference fullReference) {
076: this .fullReference = new ReferenceHolder(fullReference);
077: }
078:
079: public void setFullReference(ReferenceHolder fullReference) {
080: this .fullReference = fullReference;
081: }
082:
083: public String getDisplayName() {
084: ContentResource resource = (ContentResource) baseReference
085: .getBase().getEntity();
086:
087: if (resource == null)
088: throw new NullPointerException(
089: "the content resource is null for "
090: + baseReference.toString());
091: if (resource.getProperties() == null)
092: throw new NullPointerException(
093: "the content resource properties are null for "
094: + baseReference.toString());
095:
096: String displayNameProp = resource.getProperties()
097: .getNamePropDisplayName();
098: return resource.getProperties().getProperty(displayNameProp);
099: }
100:
101: public MimeType getMimeType() {
102: ContentResource resource = (ContentResource) baseReference
103: .getBase().getEntity();
104:
105: if (resource == null)
106: throw new NullPointerException(
107: "the content resource is null for "
108: + baseReference.toString());
109: if (resource.getProperties() == null)
110: throw new NullPointerException(
111: "the content resource properties are null for "
112: + baseReference.toString());
113:
114: String contentTypeProp = resource.getProperties()
115: .getNamePropContentType();
116: return new MimeType(resource.getProperties().getProperty(
117: contentTypeProp));
118: }
119:
120: /**
121: * This function gets the content length of the resource.
122: * It also formats it into kilobytes, megabytes and gigabytes
123: *@returns String
124: */
125: public String getContentLength() {
126: ContentResource resource = (ContentResource) baseReference
127: .getBase().getEntity();
128:
129: String displayNameProp = resource.getProperties()
130: .getNamePropContentLength();
131: String size = resource.getProperties().getProperty(
132: displayNameProp);
133:
134: int length = Integer.parseInt(size);
135:
136: if (length < 1024 * 100)
137: return (length / 1024) + "." + ((length * 10 / 1024) % 10)
138: + " KB";
139: else if (length < 1024 * 1024)
140: return (length / 1024) + " KB";
141: else if (length < 1024 * 1024 * 100)
142: return (length / (1024 * 1024)) + "."
143: + ((length * 10 / (1024 * 1024)) % 10) + " MB";
144: else if (length < 1024 * 1024 * 1024)
145: return (length / (1024 * 1024)) + " MB";
146: else if (length < 1024 * 1024 * 1024 * 100)
147: return (length / (1024 * 1024 * 1024)) + "."
148: + ((length * 10 / (1024 * 1024 * 1024)) % 10)
149: + " GB";
150: else
151: return (length / (1024 * 1024 * 1024)) + " GB";
152: }
153:
154: public boolean equals(Object o) {
155: if (this == o) {
156: return true;
157: }
158: if (!(o instanceof GuidanceItemAttachment)) {
159: return false;
160: }
161:
162: final GuidanceItemAttachment guidanceItemAttachment = (GuidanceItemAttachment) o;
163:
164: if (fullReference != null ? !fullReference
165: .equals(guidanceItemAttachment.fullReference)
166: : guidanceItemAttachment.fullReference != null) {
167: return false;
168: }
169: if (item != null ? !item.equals(guidanceItemAttachment.item)
170: : guidanceItemAttachment.item != null) {
171: return false;
172: }
173:
174: return true;
175: }
176:
177: public int hashCode() {
178: int result = 0;
179: result = 29 * result + (item != null ? item.hashCode() : 0);
180: result = 29
181: * result
182: + (fullReference != null ? fullReference.hashCode() : 0);
183: return result;
184: }
185:
186: }
|