001/**
002 * Copyright 2015 Tampere University of Technology, Pori Department
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package service.tut.pori.fileservice.reference;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.apache.commons.lang3.ArrayUtils;
022import org.apache.commons.lang3.RandomStringUtils;
023
024import service.tut.pori.contentanalysis.reference.CAXMLObjectCreator;
025import service.tut.pori.fileservice.File;
026import service.tut.pori.fileservice.FileHandler;
027import service.tut.pori.fileservice.FileList;
028import service.tut.pori.fileservice.FileProperties;
029import twitter4j.Logger;
030import core.tut.pori.context.ServiceInitializer;
031import core.tut.pori.users.UserIdentity;
032
033/**
034 * 
035 * class that can be used to created example objects/object lists
036 *
037 * Note: this class is NOT guaranteed to be thread-safe in any way.
038 *
039 */
040public class FSXMLObjectCreator {
041  private static final int FILENAME_LENGTH = 32;
042  private static final Logger LOGGER = Logger.getLogger(FSXMLObjectCreator.class);
043  private CAXMLObjectCreator _CACreator = null;
044  private FileHandler _fileHander = new FileHandler();
045
046  /**
047   * 
048   * @param seed for random generator, or null to use default (system time in nanoseconds)
049   */
050  public FSXMLObjectCreator(Long seed){
051    if(seed == null){
052      seed = System.nanoTime();
053    }
054    _CACreator = new CAXMLObjectCreator(seed);
055  }
056  
057  /**
058   * @see service.tut.pori.fileservice.FileHandler#generateFilename(String)
059   * 
060   * @param filename
061   * @return random filename
062   */
063  public String generateFilename(String filename) {
064    return _fileHander.generateFilename(filename);
065  }
066
067  /**
068   * 
069   * @return random file id
070   */
071  public Long generateFileId() {
072    return Math.abs(_CACreator.getRandom().nextLong());
073  }
074
075  /**
076   * @see service.tut.pori.contentanalysis.reference.CAXMLObjectCreator#createUserIdentity()
077   * 
078   * @return random user identity
079   */
080  public UserIdentity createUserIdentity() {
081    return _CACreator.createUserIdentity();
082  }
083
084  /**
085   * 
086   * @param userIdentity if null or invalid, new one will be randomly generated
087   * @param fileCount if file ids are given and the count is lower than the file id count, only the count amount of files will be returned. If count is more than file id count, only file id count of files will be returned.
088   * @param fileIds optional file ids
089   * @return list of files or null if 1 or less files was requested
090   */
091  public FileList generateFileList(UserIdentity userIdentity, int fileCount, long[] fileIds) {
092    if(fileCount < 1){
093      LOGGER.debug("Count < 1.");
094      return null;
095    }
096    
097    if(!UserIdentity.isValid(userIdentity)){
098      userIdentity = createUserIdentity();
099    }
100    
101    String filePath = ServiceInitializer.getPropertyHandler().getSystemProperties(FileProperties.class).getUriPath();
102    List<File> files = new ArrayList<>();
103    if(ArrayUtils.isEmpty(fileIds)){
104      long fileId = 0;
105      for(int i=0;i<fileCount;++i){
106        files.add(generateFile(userIdentity, ++fileId, filePath));
107      }
108    }else{
109      for(int i=0;i<fileCount && i<fileIds.length;++i){
110        files.add(generateFile(userIdentity, fileIds[i], filePath));
111      }
112    }
113    
114    return FileList.getFileList(files);
115  }
116  
117  /**
118   * 
119   * @return random file
120   */
121  public File generateFile(){
122    return generateFile(createUserIdentity(), Math.abs(_CACreator.getRandom().nextLong()), ServiceInitializer.getPropertyHandler().getSystemProperties(FileProperties.class).getUriPath());
123  }
124  
125  /**
126   * 
127   * @param userId
128   * @param fileId
129   * @param filePath 
130   * @return random file
131   */
132  public File generateFile(UserIdentity userId, long fileId, String filePath){
133    File file = new File();
134    file.setFileId(fileId);
135    String filename = RandomStringUtils.randomAlphanumeric(FILENAME_LENGTH);
136    String savedName = _fileHander.generateFilename(filename);
137    file.setName(filename);
138    file.setSavedName(savedName);
139    file.setUrl(filePath+savedName);
140    return file;
141  }
142}