Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please help with providing the following functions to this source code: deleteMeme( ) insertMeme( ) insertLike( ) insertDislike( ) package memeagram.data.objects; import com.dropbox.core.DbxException; import com.dropbox.core.v2.DbxClientV2;
Please help with providing the following functions to this source code:
deleteMeme( )
insertMeme( )
insertLike( )
insertDislike( )
package memeagram.data.objects; |
import com.dropbox.core.DbxException; |
import com.dropbox.core.v2.DbxClientV2; |
import com.dropbox.core.v2.sharing.SharedLinkMetadata; |
import memeagram.Context; |
import memeagram.data.DatabaseAccessController; |
import memeagram.data.DropBoxController; |
import com.mysql.jdbc.StringUtils; |
import javax.imageio.ImageIO; |
import java.awt.image.BufferedImage; |
import java.io.File; |
import java.io.FileInputStream; |
import java.io.IOException; |
import java.io.InputStream; |
import java.net.URL; |
import java.sql.PreparedStatement; |
import java.sql.ResultSet; |
import java.sql.SQLException; |
import java.sql.Statement; |
import java.util.ArrayList; |
import java.util.UUID; |
public class Meme { |
public Integer id; |
public BufferedImage originalImage; |
public BufferedImage memeImage; |
public ArrayList |
public int userId; |
public String url; |
public String captionText; |
private DatabaseAccessController Dac; |
private DropBoxController Dbc; |
public Meme(Context context){ |
tags = new ArrayList<>(); |
Dac = context.dac; |
Dbc = context.dbc; |
} |
public boolean getImage() { |
if (StringUtils.isNullOrEmpty(url)) { |
return false; |
} |
try { |
URL url = new URL(this.url); |
memeImage = ImageIO.read(url); |
} catch (IOException e) { |
e.printStackTrace(); |
return false; |
} |
if (memeImage != null) { |
return true; |
} |
return false; |
} |
public static Meme getMemeById(Context context, int id) throws SQLException { |
DatabaseAccessController Dac = context.dac; |
String stmt = "SELECT Id, UserId, ImageUrl, CaptionText FROM Memes WHERE Id = ?;"; |
PreparedStatement preparedStatement = Dac.conn.prepareStatement(stmt); |
preparedStatement.setInt(1, id); |
ResultSet rs = preparedStatement.executeQuery(); |
Meme meme = new Meme(context); |
if (rs.next()) { |
meme.id = rs.getInt("Id"); |
meme.userId = rs.getInt("UserId"); |
meme.url = rs.getString("ImageUrl"); |
meme.captionText = rs.getString("CaptionText"); |
} |
else return null; |
meme.tags = getMemeTags(context, meme.id); |
return meme; |
} |
public boolean saveMeme() { |
DbxClientV2 dropbox = Dbc.client; |
// convert meme to temp file |
File file; |
try { |
file = File.createTempFile(UUID.randomUUID().toString(), ".jpg"); |
ImageIO.write(memeImage, "jpg", file); |
} catch (IOException e) { |
e.printStackTrace(); |
return false; |
} |
// store temp file to dropbox and get direct url |
String dropboxPath = String.format("/memes/%s", file.getName()); |
try(InputStream in = new FileInputStream(file.getAbsolutePath())) { |
dropbox.files() |
.uploadBuilder(dropboxPath) |
.uploadAndFinish(in); |
SharedLinkMetadata shareMeta = dropbox.sharing() |
.createSharedLinkWithSettings(dropboxPath); |
url = shareMeta.getUrl(); |
// alter query param to notify that we want to download the image |
url = url.substring(0,url.lastIndexOf("?")); |
url += "?dl=1"; |
} catch (IOException | DbxException e) { |
e.printStackTrace(); |
return false; |
} |
file.delete(); |
// store meme meta to DB |
if (!StringUtils.isNullOrEmpty(url)) { |
try { |
id = insertMeme(userId, url, captionText); |
if (id == null){ |
return false; |
} |
} catch (SQLException e) { |
e.printStackTrace(); |
return false; |
} |
} |
// store meme tags to DB |
try { |
insertMemeTags(id, tags); |
} catch (SQLException e) { |
e.printStackTrace(); |
} |
// success |
return true; |
} |
private static ArrayList |
DatabaseAccessController Dac = context.dac; |
String stmt = "SELECT TagText FROM MemeTags WHERE MemeId = ?;"; |
PreparedStatement preparedStatement = Dac.conn.prepareStatement(stmt); |
preparedStatement.setInt(1, memeId); |
ResultSet rs = preparedStatement.executeQuery(); |
ArrayList |
while (rs.next()) { |
tags.add(rs.getString("TagText")); |
} |
return tags; |
} |
private Integer insertMeme(int userId, String url, String captionText) throws SQLException |
{ |
String stmt = "INSERT INTO Memes(UserId, ImageUrl, CaptionText) VALUES(?,?,?);"; |
PreparedStatement preparedStmt = Dac.conn.prepareStatement(stmt, Statement.RETURN_GENERATED_KEYS); |
preparedStmt.setInt(1,userId); |
preparedStmt.setString(2,url); |
preparedStmt.setString(3,captionText); |
preparedStmt.execute(); |
try (ResultSet generatedKeys = preparedStmt.getGeneratedKeys()) { |
if (generatedKeys.next()) { |
return generatedKeys.getInt(1); |
} |
} |
return null; |
} |
private void insertMemeTags(int memeId, ArrayList |
{ |
for (String tag: tags) { |
String stmt = "INSERT INTO MemeTags(MemeId, TagText) VALUES(?,?)"; |
PreparedStatement preparedStmt = Dac.conn.prepareStatement(stmt); |
preparedStmt.setInt(1,memeId); |
preparedStmt.setString(2,tag); |
preparedStmt.execute(); |
} |
} |
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started