Android Open Source - innovativeproject-meetingdataexchange Files Management






From Project

Back to project page innovativeproject-meetingdataexchange.

License

The source code is released under:

MIT License

If you think the Android project innovativeproject-meetingdataexchange listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package controllers;
import java.io.File;
import java.sql.Timestamp;
/*from w w w .  j  a  v a2s.c o  m*/
import org.jooq.Record;
import org.jooq.Record1;
import org.jooq.Record4;
import org.jooq.Record6;
import org.jooq.exception.DataAccessException;

import models.DbSingleton;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.io.Files;

import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import tools.MD5Checksum;
import static models.Tables.*;

public class FilesManagement extends Controller {
  
  public static Result upload(String login, String sid, String meetingid, String filename) throws DataAccessException, Exception {
    File file = request().body().asRaw().asFile();
    return ok(web_upload(login, sid, meetingid, file, filename));
  }
  
  public static ObjectNode web_upload(String login, String sid, String meetingid, File file, String filename) throws DataAccessException, Exception{
    if(login==null)
      return errorObject("Incorrect login");
    if(sid==null)
      return errorObject("Incorrect sid");
    if(meetingid==null)
      return errorObject("Incorrect meetingid");
    if(filename==null)
      return errorObject("Incorrect file name");
    
    
    if(!Meetings.userIsA_MemberOfMeeting(login, meetingid))
      return errorObject("Not a member of meeting");
    
    org.jooq.Result<Record1<Integer>> record = DbSingleton.getInstance().getDsl().select(MEETINGUSER.ID)
        .from(MEETING)
        .join(MEETINGUSER).on(MEETINGUSER.MEETINGID.equal(Integer.parseInt(meetingid)))
          .and(MEETINGUSER.USERLOGIN.equal(login))
        .where(MEETING.ID.equal(Integer.parseInt(meetingid)))
        .and(MEETING.ABILITYTOSENDFILES.isTrue().or(MEETING.AUTHORLOGIN.equal(login))).fetch();
    
    int meetinguserid;
    
    if(record.isEmpty())
      return errorObject("Upload forbidden");
    else
      meetinguserid = record.getValue(0, MEETINGUSER.ID);
        
    File newFile = new File(System.getProperty("user.dir")+"/upload/"+meetingid+"/"+filename);
    
    if(newFile.exists())
      return errorObject("File with this name already exists");
      
    newFile.createNewFile();
    Files.copy(file, newFile);
    
    java.util.Date date= new java.util.Date();
    
    DbSingleton.getInstance().getDsl()
        .insertInto(FILE,
            FILE.MEETINGUSERID, FILE.NAME, FILE.SIZEKB, FILE.ADDEDTIME, FILE.HASHMD5)
        .values(meetinguserid, filename, 
            new Integer((int) Math.ceil(newFile.length()/1024.0)), new Timestamp(date.getTime()),
            MD5Checksum.getMD5Checksum(System.getProperty("user.dir")+"/upload/"+meetingid+"/"+filename))
        .execute();
    
    ObjectNode result = Json.newObject();
    result.put("status", "ok");
    return result;
  }
  
  public static Result remove(){
    JsonNode json = request().body().asJson();
    if(json == null)
      return errorResult("json excepted");
    
    String login = json.findPath("login").textValue();
    String sid = json.findPath("sid").textValue();
    String fileid = json.findPath("fileid").textValue();
    
    return ok(web_remove(login, sid, fileid));
  }


  public static ObjectNode web_remove(String login, String sid, String fileid) {
    if(!checkIsSidCorrect(login, sid))
      return errorObject("incorrect sid");
    
    org.jooq.Result<Record1<Integer>> record = DbSingleton.getInstance().getDsl()
        .select(MEETING.ID)
        .from(MEETING).join(MEETINGUSER).on(MEETING.ID.equal(MEETINGUSER.MEETINGID))
        .join(FILE).on(FILE.MEETINGUSERID.equal(MEETINGUSER.ID))
        .where(FILE.ID.equal(Integer.parseInt(fileid))).fetch();
    
    String meetingid = String.valueOf(record.getValue(0, MEETING.ID));
    
    if(!userIsA_Host(login, meetingid) && !userIsA_FileAuthor(login, fileid))
      return errorObject("access denied");
    
    if(meetingIsAlreadyFinish(meetingid))
      return errorObject("already finished");
    
    org.jooq.Result<Record1<String>> record2 = DbSingleton.getInstance().getDsl()
        .select(FILE.NAME)
        .from(FILE)
        .where(FILE.ID.equal(Integer.parseInt(fileid))).fetch();
    
    String filename = record2.getValue(0, FILE.NAME);
    
    File fileToDelete = new File(System.getProperty("user.dir")+"/upload/"+meetingid+"/"+filename);
    fileToDelete.delete();
    
    DbSingleton.getInstance().getDsl().delete(FILE).where(FILE.ID.equal(Integer.parseInt(fileid))).execute();
    
    ObjectNode result = Json.newObject();
    result.put("status", "ok");
    return result;
  }
  
  public static Result download(String login, String sid, String fileid){
    Object obj = web_download(login, sid, fileid);
    if(obj instanceof File)
      return ok((File) obj);
    else
      return ok((ObjectNode) obj);
  }
  
  public static Object web_download(String login, String sid, String fileid){
    if(!checkIsSidCorrect(login, sid))
      return errorObject("incorrect sid");
    
    org.jooq.Result<Record1<Integer>> record = DbSingleton.getInstance().getDsl()
        .select(MEETING.ID)
        .from(MEETING).join(MEETINGUSER).on(MEETING.ID.equal(MEETINGUSER.MEETINGID))
        .join(FILE).on(FILE.MEETINGUSERID.equal(MEETINGUSER.ID))
        .where(FILE.ID.equal(Integer.parseInt(fileid))).fetch();
    
    String meetingid = String.valueOf(record.getValue(0, MEETING.ID));
    
    if(!userIsA_MemberOfMeeting(login, meetingid))
      return errorObject("you are not a member");
    
    org.jooq.Result<Record1<String>> record2 = DbSingleton.getInstance().getDsl()
        .select(FILE.NAME)
        .from(FILE)
        .where(FILE.ID.equal(Integer.parseInt(fileid))).fetch();
    
    if(record2.isEmpty())
      return errorObject("file not found");
    
    String filename = record2.getValue(0, FILE.NAME);
    
    File fileToSend = new File(System.getProperty("user.dir")+"/upload/"+meetingid+"/"+filename);
    
    response().setContentType("application/x-download");  
    response().setHeader("Content-disposition","attachment; filename="+filename);
    return fileToSend;
  }
  
  public static Result downloadDir(String meetingid, String code, String filename){
    org.jooq.Result<Record1<Integer>> record = DbSingleton.getInstance().getDsl()
        .select(MEETING.ID)
        .from(FILE).join(MEETINGUSER).on(FILE.MEETINGUSERID.equal(MEETINGUSER.ID))
        .join(MEETING).on(MEETING.ID.equal(MEETINGUSER.MEETINGID))
        .where(MEETING.ID.equal(Integer.parseInt(meetingid)))
        .and(MEETING.ACCESSCODE.equal(code))
        .and(FILE.NAME.equal(filename)).fetch();
    if(record.size()>0){
      File fileToSend = new File(System.getProperty("user.dir")+"/upload/"+meetingid+"/"+filename);
      
      response().setContentType("application/x-download");  
      response().setHeader("Content-disposition","attachment; filename="+filename);
      return ok(fileToSend);
    }
    else
      return Controller.notFound();
  }
  
  
  
  public static Result comment(){
    JsonNode json = request().body().asJson();
    if(json == null)
      return errorResult("json excepted");
    
    String login = json.findPath("login").textValue();
    String sid = json.findPath("sid").textValue();
    String fileid = json.findPath("fileid").textValue();
    String content = json.findPath("content").textValue();
    
    return ok(web_comment(login, sid, fileid, content));
  }
  
  
  public static ObjectNode web_comment(String login, String sid, String fileid, String content) {
    if(!checkIsSidCorrect(login, sid))
      return errorObject("incorrect sid");
    
    org.jooq.Result<Record1<Integer>> record = DbSingleton.getInstance().getDsl()
        .select(MEETING.ID)
        .from(MEETING).join(MEETINGUSER).on(MEETING.ID.equal(MEETINGUSER.MEETINGID))
        .join(FILE).on(FILE.MEETINGUSERID.equal(MEETINGUSER.ID))
        .where(FILE.ID.equal(Integer.parseInt(fileid))).fetch();
    
    String meetingid = String.valueOf(record.getValue(0, MEETING.ID));
    if(!userIsA_MemberOfMeeting(login, meetingid))
      return errorObject("you are not a member");
    if(meetingIsAlreadyFinish(meetingid))
      return errorObject("meeting finished");
    
    java.util.Date date= new java.util.Date();
    
    Record record2 = DbSingleton.getInstance().getDsl()
        .insertInto(COMMENT,
            COMMENT.FILEID, COMMENT.USERLOGIN, COMMENT.DATE, COMMENT.CONTENT)
        .values(Integer.parseInt(fileid), login, new Timestamp(date.getTime()), content)
        .returning(COMMENT.ID)
        .fetchOne();
    
    ObjectNode result = Json.newObject();
    result.put("status", "ok");
    return result;
  }
  
  public static Result getList(String meetingid, String login, String sid){
    return ok(web_getList(meetingid, login, sid));
  }
  
  public static ObjectNode web_getList(String meetingid, String login, String sid){
    if(login==null || sid==null)
      return errorObject("incorrect data");
    if(!checkIsSidCorrect(login, sid))
      return errorObject("incorrect sid");
    if(!Meetings.userIsA_MemberOfMeeting(login, meetingid))
      return errorObject("access denied");
    
    ObjectNode result = Json.newObject();
    ArrayNode arrayFile = Json.newObject().arrayNode();
    
    org.jooq.Result<Record6<Integer, String, Integer, Timestamp, String, String>> record = 
        DbSingleton.getInstance().getDsl()
        .select(FILE.ID, FILE.NAME, FILE.SIZEKB, FILE.ADDEDTIME, FILE.HASHMD5, USER.NAME)
        .from(FILE)
        .join(MEETINGUSER)
        .on(FILE.MEETINGUSERID.equal(MEETINGUSER.ID))
        .join(MEETING)
        .on(MEETING.ID.equal(MEETINGUSER.MEETINGID))
        .join(USER)
        .on(USER.LOGIN.equal(MEETINGUSER.USERLOGIN))
        .where(MEETING.ID.equal(Integer.parseInt(meetingid)))
        .orderBy(FILE.ADDEDTIME.desc()).fetch();
    
    int count = record.size();
    
    for(int i=0; i<count; i++){
      ObjectNode file = Json.newObject();
      
      file.put("fileid", record.getValue(i, FILE.ID)).asInt();
      file.put("filename", record.getValue(i, FILE.NAME));
      file.put("author", record.getValue(i, USER.NAME));
      file.put("addtime", record.getValue(i, FILE.ADDEDTIME).toString());
      file.put("hash", record.getValue(i, FILE.HASHMD5));
      file.put("sizeKB", record.getValue(i, FILE.SIZEKB)).asInt();
      
      ArrayNode arrayComment = Json.newObject().arrayNode();
      
      org.jooq.Result<Record4<Integer, String, Timestamp, String>> record2 = 
          DbSingleton.getInstance().getDsl()
          .select(COMMENT.ID, USER.NAME, COMMENT.DATE, COMMENT.CONTENT)
          .from(COMMENT)
          .join(USER)
          .on(COMMENT.USERLOGIN.equal(USER.LOGIN))
          .where(COMMENT.FILEID.equal(record.getValue(i, FILE.ID)))
          .orderBy(COMMENT.DATE.desc()).fetch();
      
      int count_comment = record2.size();
      
      for(int j=0; j<count_comment; j++){
        ObjectNode comment = Json.newObject();
        
        comment.put("commentid", record2.getValue(j, COMMENT.ID));
        comment.put("author", record2.getValue(j, USER.NAME));
        comment.put("addtime", record2.getValue(j, COMMENT.DATE).toString());
        comment.put("content", record2.getValue(j, COMMENT.CONTENT));
        
        arrayComment.add(comment);
      }
      file.put("comments", arrayComment);
      arrayFile.add(file);
    }
    result.put("status", "ok");
    result.put("files", arrayFile);
    return result;
  }
  
  private static boolean userIsA_MemberOfMeeting(String login,
      String meetingId) {
    org.jooq.Result<Record> record = DbSingleton.getInstance().getDsl()
        .select()
        .from(MEETINGUSER)
        .where(MEETINGUSER.MEETINGID.equal(Integer.parseInt(meetingId)))
        .and(MEETINGUSER.USERLOGIN.equal(login)).fetch();
        if(record.isNotEmpty())
          return true;  
        else
          return false;
  }
  
  private static boolean checkIsSidCorrect(String login, String sid) {
    org.jooq.Result<Record> record = DbSingleton.getInstance().getDsl().select().from(SESSION)
        .where(SESSION.USERLOGIN.equal(login)).and(SESSION.SID.equal(sid)).fetch();
        if(record.isNotEmpty())
          return true;  
        else
          return false;
  }
  
  private static boolean userIsA_Host(String login, String meetingId) {
    org.jooq.Result<Record> record = DbSingleton.getInstance().getDsl()
          .select()
          .from(MEETING)
          .where(MEETING.ID.equal(Integer.parseInt(meetingId)))
          .and(MEETING.AUTHORLOGIN.equal(login))
            .fetch();
        if(record.isNotEmpty())
          return true;  
        else
          return false;
  }
  
  private static boolean meetingIsAlreadyFinish(String meetingId) {
    org.jooq.Result<Record> record = DbSingleton.getInstance().getDsl()
          .select()
          .from(MEETING)
          .where(MEETING.ID.equal(Integer.parseInt(meetingId)))
          .and(MEETING.ENDTIME.isNotNull())
            .fetch();
        if(record.isNotEmpty())
          return true;  
        else
          return false;
  }
  
  private static boolean userIsA_FileAuthor(String login, String fileid) {
    org.jooq.Result<Record> record = DbSingleton.getInstance().getDsl()
          .select()
          .from(FILE).join(MEETINGUSER).on(FILE.MEETINGUSERID.equal(MEETINGUSER.ID))
          .where(FILE.ID.equal(Integer.parseInt(fileid)))
          .and(MEETINGUSER.USERLOGIN.equal(login))
            .fetch();
        if(record.isNotEmpty())
          return true;  
        else
          return false;
  }

  private static Result errorResult(String msg) {
    return ok(errorObject(msg));
  }
  
  
  private static ObjectNode errorObject(String msg){
    ObjectNode result = Json.newObject();
    result.put("status", "failed");
    result.put("reason", msg);
    return result;
  }
}




Java Source Code List

Dialogs.ServerOptionsLoggedIn.java
Dialogs.ServerOptionsLoggedOut.java
asynctasks.HttpGetListMeetings.java
asynctasks.HttpGetPersonalData.java
asynctasks.HttpGetServerName.java
asynctasks.HttpPostNewMeeting.java
asynctasks.HttpPostRequestLogInAlreadyExistsUser.java
asynctasks.HttpPostRequestLogInFirstTime.java
asynctasks.HttpPostRequestLogIn.java
asynctasks.HttpPostSignUp.java
asynctasks.HttpPutSendFile.java
asynctasks.RefreshMeetingProgress.java
com.TrololoCompany.meetingdataexchangeAdapters.FileAdapter.java
com.TrololoCompany.meetingdataexchangeAdapters.MeetingAdapter.java
com.TrololoCompany.meetingdataexchangeAdapters.ServerAdapter.java
com.TrololoCompany.meetingdataexchange.AddNewMeetingActivity.java
com.TrololoCompany.meetingdataexchange.AddServerActivityFail.java
com.TrololoCompany.meetingdataexchange.AddServerActivitySuccess.java
com.TrololoCompany.meetingdataexchange.AddServerActivity.java
com.TrololoCompany.meetingdataexchange.DisplayQR.java
com.TrololoCompany.meetingdataexchange.LogInActivity.java
com.TrololoCompany.meetingdataexchange.MainActivity.java
com.TrololoCompany.meetingdataexchange.MeetingDetails.java
com.TrololoCompany.meetingdataexchange.ServerList.java
com.TrololoCompany.meetingdataexchange.ServerManageActivity.java
com.TrololoCompany.meetingdataexchangeServices.MeetingDetailsRefresh.java
com.TrololoCompany.meetingdataexchangeServices.MeetingServerCommunication.java
com.TrololoCompany.meetingdataexchangeServices.ServiceHandlers.java
com.TrololoCompany.meetingdataexchange.SignUpActivity.java
com.TrololoCompany.meetingdataexchange.util.SystemUiHiderBase.java
com.TrololoCompany.meetingdataexchange.util.SystemUiHiderHoneycomb.java
com.TrololoCompany.meetingdataexchange.util.SystemUiHider.java
com.TrololoCompany.meetingdataexchangedataBase.CommentEntity.java
com.TrololoCompany.meetingdataexchangedataBase.DataBaseHelper.java
com.TrololoCompany.meetingdataexchangedataBase.FileEntity.java
com.TrololoCompany.meetingdataexchangedataBase.MeetingEntity.java
com.TrololoCompany.meetingdataexchangedataBase.ServerEntity.java
controllers.Accounts.java
controllers.FilesManagement.java
controllers.GeneralStuff.java
controllers.Mailing.java
controllers.Meetings.java
dataBase.CommentEntity.java
dataBase.DataBaseHelper.java
dataBase.FileEntity.java
dataBase.MeetingEntity.java
dataBase.MettingEntity.java
dataBase.ServerEntity.java
dataBase.Server.java
fileMaintenance.FileMaintenance.java
lists.MeetingList.java
lists.ServerList.java
meeting_options.FireMissilesDialogFragment.java
meeting_options.MeetDetOnPageListener.java
meeting_options.MeetDetTabListener.java
meeting_options.MeetingAddItems.java
meeting_options.MeetingDescription.java
meeting_options.MeetingProgressUIRefresh.java
meeting_options.MeetingProgress.java
meeting_options.MeetingTabPagerAdapter.java
meeting_options.MyOnPageChangeListener.java
meeting_options.MyTabListener.java
meeting_options.RefreshMeetingProgressListView.java
meeting_options.TabsPagerAdapter.java
models.DbSingleton.java
models.DefaultSchema.java
models.Keys.java
models.Tables.java
models.tables.Comment.java
models.tables.File.java
models.tables.Meeting.java
models.tables.Meetinguser.java
models.tables.Session.java
models.tables.SqliteSequence.java
models.tables.User.java
models.tables.records.CommentRecord.java
models.tables.records.FileRecord.java
models.tables.records.MeetingRecord.java
models.tables.records.MeetinguserRecord.java
models.tables.records.SessionRecord.java
models.tables.records.SqliteSequenceRecord.java
models.tables.records.UserRecord.java
serverCommunicator.CheckIsServerExists.java
serverCommunicator.CommentsHelper.java
serverCommunicator.CommunicationHelper.java
serverCommunicator.Communication.java
serverCommunicator.FileHelper.java
serverCommunicator.FileListRefreshService.java
serverCommunicator.GetMeetingListHelper.java
serverCommunicator.GetPersonalDataHelper.java
serverCommunicator.HttpGetRequest.java
serverCommunicator.HttpPostRequest.java
serverCommunicator.LogInHelper.java
serverCommunicator.MeetingHelper.java
serverCommunicator.NewMeetingHelper.java
serverCommunicator.RegistrationHelper.java
tools.MD5Checksum.java