Answered step by step
Verified Expert Solution
Link Copied!

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 tags;
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 getMemeTags(Context context, int memeId) throws SQLException {
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 tags = new 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 tags) throws SQLException
{
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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sams Teach Yourself Beginning Databases In 24 Hours

Authors: Ryan Stephens, Ron Plew

1st Edition

067232492X, 978-0672324925

More Books

Students also viewed these Databases questions

Question

Please can you convert these into Java? Help really needed please!

Answered: 1 week ago

Question

What is meant by 'Wealth Maximization ' ?

Answered: 1 week ago

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago