001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/business/entity/FileNamer.java $
003: * $Id: FileNamer.java 9273 2006-05-10 22:34:28Z daisyf@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 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.sakaiproject.tool.assessment.business.entity;
021:
022: import java.util.Date;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: import org.sakaiproject.tool.assessment.util.StringParseUtils;
028:
029: /**
030: * <p>
031: * Copyright: Copyright (c) 2003
032: * </p>
033: *
034: * <p>
035: * Organization: Stanford University
036: * </p>
037: *
038: * <p>
039: * This class implements common methods for describing data needed to
040: * record/save and retrieve audio recordings.
041: * </p>
042: *
043: * <p>
044: * Usage (hypothetical example): <code><br>
045: * <br>
046: * RecordingData rd = new RecordingData( agent_name, agent_id, course_name,
047: * course_id); log.debug("file" + rd.getFileName() + "." +
048: * rd.getFileExtension()); </code>
049: * </p>
050: *
051: * @author Ed Smiley
052: * @version $Id $
053: */
054: public class FileNamer {
055: private static Log log = LogFactory.getLog(RecordingData.class);
056:
057: // internals
058: private static final int maxAgentName = 20;
059: private static final int maxAgentId = 10;
060: private static final int maxcourseAssignmentContext = 15;
061: private static final int maxCourseId = 10;
062: private static final int maxFileName = 64;
063:
064: /**
065: * Makes a unique file name from the data supplied.
066: *
067: * @param agent_name The name of the person uploading the file
068: * @param agent_id The id code of the person uploading the file
069: * @param course_assignment_context The name of the course, assignment, part,
070: * quetion etc.
071: *
072: * @return a meaningful file name
073: */
074: public static String make(String agent_name, String agent_id,
075: String course_assignment_context) {
076: // we can still create a unique file if one or more values are null
077: agent_name = "" + agent_name;
078: agent_id = "" + agent_id;
079: course_assignment_context = "" + course_assignment_context;
080:
081: //timestamp this access
082: Date date = new Date();
083:
084: // hold in a StringBuffer
085: StringBuffer sb = new StringBuffer();
086:
087: // make a unique random signature
088: String rand = "" + Math.random() + "" + Math.random();
089:
090: sb.append(StringParseUtils.simplifyString("" + agent_name,
091: maxAgentName, agent_name.length()));
092: sb.append(StringParseUtils.simplifyString("" + agent_id,
093: maxAgentId, agent_id.length()));
094: sb.append(StringParseUtils.simplifyString(""
095: + course_assignment_context,
096: maxcourseAssignmentContext, course_assignment_context
097: .length()));
098: sb.append(new SortableDate(date));
099: sb.append("__");
100: sb.append(StringParseUtils.simplifyString(rand, 99, 99));
101:
102: // return as a string
103: if (sb.length() > maxFileName) {
104: return sb.substring(0, maxFileName - 1);
105: }
106:
107: return sb.toString();
108: }
109:
110: /**
111: * unit test for use with jUnit etc.
112: */
113: public static void unitTest() {
114: String s;
115: s = make("Ed Smiley", "esmiley", "Intro to Wombats 101");
116: log.debug("esmiley file: " + s);
117:
118: s = make("Rachel Gollub", "rgollub",
119: "Intro to Wolverines and Aardvarks 221B");
120: log.debug("rgollub file: " + s);
121:
122: s = make("Ed Smiley", "esmiley", "Intro to Wombats 101");
123: log.debug("esmiley file: " + s);
124:
125: s = make("Rachel Gollub", "rgollub",
126: "Intro to Wolverines and Aardvarks 221B");
127: log.debug("rgollub file: " + s);
128:
129: s = make(null, null, null);
130: log.debug("NULL file: " + s);
131: s = make(null, null, null);
132: log.debug("NULL file: " + s);
133: }
134:
135: /**
136: * DOCUMENTATION PENDING
137: *
138: * @param args DOCUMENTATION PENDING
139: */
140: public static void main(String[] args) {
141: unitTest();
142: }
143: }
|