Skip to content
Snippets Groups Projects
Commit f7da0475 authored by Félix  Sidokhine's avatar Félix Sidokhine
Browse files

Merge branch 'feature/contact-sync' into 'master'

Feature/contact sync

See merge request fsidokhine/jams-refactoring!2
parents 065e37c1 1e9786d8
Branches
No related tags found
No related merge requests found
Showing
with 149 additions and 38 deletions
...@@ -26,6 +26,7 @@ import lombok.Getter; ...@@ -26,6 +26,7 @@ import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.jami.datastore.main.DataStore; import net.jami.datastore.main.DataStore;
import net.jami.jams.common.dao.DeleteStatementBuilder;
import net.jami.jams.common.dao.SelectStatementBuilder; import net.jami.jams.common.dao.SelectStatementBuilder;
import net.jami.jams.common.dao.StatementList; import net.jami.jams.common.dao.StatementList;
import net.jami.jams.common.dao.UpdateStatementBuilder; import net.jami.jams.common.dao.UpdateStatementBuilder;
...@@ -46,7 +47,6 @@ public abstract class AbstractDao<T> { ...@@ -46,7 +47,6 @@ public abstract class AbstractDao<T> {
private Class<T> tClass; private Class<T> tClass;
public abstract boolean storeObject(T object); public abstract boolean storeObject(T object);
public abstract boolean deleteObject(LinkedHashMap<String,String> constraints);
public List<T> getObjects(StatementList constraints){ public List<T> getObjects(StatementList constraints){
List<T> result = new ArrayList<>(); List<T> result = new ArrayList<>();
...@@ -82,4 +82,20 @@ public abstract class AbstractDao<T> { ...@@ -82,4 +82,20 @@ public abstract class AbstractDao<T> {
DataStore.connectionPool.returnConnection(connection); DataStore.connectionPool.returnConnection(connection);
} }
} }
public boolean deleteObject(StatementList delete){
SQLConnection connection = DataStore.connectionPool.getConnection();
try{
PreparedStatement ps = DeleteStatementBuilder.buildStatement(tableName,delete,connection);
return ps.execute();
}
catch (Exception e){
log.error("An error has occurred while trying to fetch a device: " + e.toString());
return false;
}
finally {
DataStore.connectionPool.returnConnection(connection);
}
}
} }
...@@ -22,25 +22,43 @@ ...@@ -22,25 +22,43 @@
*/ */
package net.jami.datastore.dao; package net.jami.datastore.dao;
import lombok.extern.slf4j.Slf4j;
import net.jami.datastore.main.DataStore;
import net.jami.jams.common.dao.StatementList; import net.jami.jams.common.dao.StatementList;
import net.jami.jams.common.dao.connectivity.SQLConnection;
import net.jami.jams.common.objects.contacts.Contact; import net.jami.jams.common.objects.contacts.Contact;
import net.jami.jams.common.objects.devices.Device;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@Slf4j
public class ContactDao extends AbstractDao<Contact> { public class ContactDao extends AbstractDao<Contact> {
@Override public ContactDao() {
public boolean storeObject(Contact object) { SQLConnection connection = DataStore.connectionPool.getConnection();
return false; try {
this.setTableName("contacts");
this.setTClass(Contact.class);
String createTable = "CREATE TABLE contacts (" +
"owner varchar(255), " +
"contact varchar(255)," +
"displayName varchar(255),"+
"PRIMARY KEY (owner,contact))";
PreparedStatement ps = connection.getConnection().prepareStatement(createTable);
ps.execute();
}
catch (SQLException e){
log.error("Could not create the contacts table with error " + e.getMessage());
}
finally {
DataStore.connectionPool.returnConnection(connection);
} }
@Override
public boolean deleteObject(LinkedHashMap<String, String> constraints) {
return false;
} }
@Override @Override
public boolean updateObject(StatementList update, StatementList constraints) { public boolean storeObject(Contact object) {
return false; return false;
} }
} }
...@@ -78,11 +78,6 @@ public class DeviceDao extends AbstractDao<Device> { ...@@ -78,11 +78,6 @@ public class DeviceDao extends AbstractDao<Device> {
} }
} }
@Override
public boolean deleteObject(LinkedHashMap<String, String> constraints) {
return false;
}
@Override @Override
public boolean updateObject(StatementList update, StatementList constraints) { public boolean updateObject(StatementList update, StatementList constraints) {
return false; return false;
......
...@@ -76,11 +76,6 @@ public class SystemDao extends AbstractDao<SystemAccount> { ...@@ -76,11 +76,6 @@ public class SystemDao extends AbstractDao<SystemAccount> {
} }
} }
@Override
public boolean deleteObject(LinkedHashMap<String, String> constraints) {
return false;
}
@Override @Override
public boolean updateObject(StatementList update, StatementList constraints) { public boolean updateObject(StatementList update, StatementList constraints) {
return false; return false;
......
...@@ -83,10 +83,4 @@ public class UserDao extends AbstractDao<User> { ...@@ -83,10 +83,4 @@ public class UserDao extends AbstractDao<User> {
DataStore.connectionPool.returnConnection(connection); DataStore.connectionPool.returnConnection(connection);
} }
} }
@Override
public boolean deleteObject(LinkedHashMap<String, String> constraints) {
return false;
}
} }
...@@ -32,10 +32,10 @@ import java.sql.SQLException; ...@@ -32,10 +32,10 @@ import java.sql.SQLException;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@Slf4j @Slf4j
public class UserProfileDAO extends AbstractDao<UserProfile> { public class UserProfileDao extends AbstractDao<UserProfile> {
//Fis this to include the fields from AD/LDAP. //Fis this to include the fields from AD/LDAP.
public UserProfileDAO() { public UserProfileDao() {
SQLConnection connection = DataStore.connectionPool.getConnection(); SQLConnection connection = DataStore.connectionPool.getConnection();
try { try {
this.setTableName("local_directory"); this.setTableName("local_directory");
...@@ -60,9 +60,4 @@ public class UserProfileDAO extends AbstractDao<UserProfile> { ...@@ -60,9 +60,4 @@ public class UserProfileDAO extends AbstractDao<UserProfile> {
public boolean storeObject(UserProfile object) { public boolean storeObject(UserProfile object) {
return false; return false;
} }
@Override
public boolean deleteObject(LinkedHashMap<String, String> constraints) {
return false;
}
} }
...@@ -24,6 +24,7 @@ package net.jami.datastore.main; ...@@ -24,6 +24,7 @@ package net.jami.datastore.main;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import net.jami.datastore.dao.ContactDao;
import net.jami.datastore.dao.DeviceDao; import net.jami.datastore.dao.DeviceDao;
import net.jami.datastore.dao.SystemDao; import net.jami.datastore.dao.SystemDao;
import net.jami.datastore.dao.UserDao; import net.jami.datastore.dao.UserDao;
...@@ -46,6 +47,7 @@ public class DataStore implements AuthenticationSource { ...@@ -46,6 +47,7 @@ public class DataStore implements AuthenticationSource {
private UserDao userDao; private UserDao userDao;
private DeviceDao deviceDao; private DeviceDao deviceDao;
private SystemDao systemDao; private SystemDao systemDao;
private ContactDao contactDao;
//Implicitly connect to debry. //Implicitly connect to debry.
public DataStore(String connectionString) { public DataStore(String connectionString) {
...@@ -53,6 +55,7 @@ public class DataStore implements AuthenticationSource { ...@@ -53,6 +55,7 @@ public class DataStore implements AuthenticationSource {
userDao = new UserDao(); userDao = new UserDao();
deviceDao = new DeviceDao(); deviceDao = new DeviceDao();
systemDao = new SystemDao(); systemDao = new SystemDao();
contactDao = new ContactDao();
} }
public boolean userExists(String username){ public boolean userExists(String username){
......
package net.jami.jams.common.dao;
import net.jami.jams.common.dao.connectivity.SQLConnection;
import java.sql.PreparedStatement;
public class DeleteStatementBuilder {
public static PreparedStatement buildStatement(String table, StatementList statementElements,
SQLConnection connection) throws Exception {
PreparedStatement ps = null;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("DELETE FROM ").append(table);
if(statementElements != null) {
stringBuilder.append(" WHERE ");
for (StatementElement statementElement : statementElements.getStatements()) {
stringBuilder
.append(statementElement.getColumn())
.append(" ")
.append(statementElement.getOperator())
.append(" ")
.append("?")
.append(" ")
.append(statementElement.getNextStatementRelation());
}
ps = connection.getConnection().prepareStatement(stringBuilder.toString());
int i = 1;
for (StatementElement statementElement : statementElements.getStatements()) {
ps.setString(i, statementElement.getValue());
i++;
}
}
else{
ps = connection.getConnection().prepareStatement(stringBuilder.toString());
}
return ps;
}
}
...@@ -22,10 +22,46 @@ ...@@ -22,10 +22,46 @@
*/ */
package net.jami.jams.common.objects.contacts; package net.jami.jams.common.objects.contacts;
import com.jsoniter.annotation.JsonIgnore;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import net.jami.jams.common.serialization.database.DatabaseObject;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@Getter @Getter
@Setter @Setter
public class Contact { @NoArgsConstructor
public class Contact implements DatabaseObject {
@JsonIgnore //Ignore the owner because he is irrelevant.
private String owner;
private String uri;
private String displayName;
public Contact(ResultSet rs) throws Exception {
this.owner = rs.getString("owner");
this.uri = rs.getString("uri");
this.displayName = rs.getString("displayName");
}
@Override
public PreparedStatement getInsert(PreparedStatement ps) throws Exception {
ps.setString(1,owner);
ps.setString(2,uri);
ps.setString(3,displayName);
return ps;
}
@Override
public PreparedStatement getDelete(PreparedStatement ps) throws Exception {
return null;
}
@Override
public PreparedStatement getUpdate(PreparedStatement ps) throws Exception {
return null;
}
} }
...@@ -23,9 +23,9 @@ ...@@ -23,9 +23,9 @@
package net.jami.jams.common.serialization.database; package net.jami.jams.common.serialization.database;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet;
public interface DatabaseObject { public interface DatabaseObject {
PreparedStatement getInsert(PreparedStatement ps) throws Exception; PreparedStatement getInsert(PreparedStatement ps) throws Exception;
PreparedStatement getDelete(PreparedStatement ps) throws Exception; PreparedStatement getDelete(PreparedStatement ps) throws Exception;
PreparedStatement getUpdate(PreparedStatement ps) throws Exception; PreparedStatement getUpdate(PreparedStatement ps) throws Exception;
......
...@@ -22,28 +22,49 @@ ...@@ -22,28 +22,49 @@
*/ */
package net.jami.jams.server.servlets.api.auth.contacts; package net.jami.jams.server.servlets.api.auth.contacts;
import com.jsoniter.JsonIterator;
import com.jsoniter.output.JsonStream;
import net.jami.datastore.dao.ContactDao;
import net.jami.jams.common.dao.StatementElement;
import net.jami.jams.common.dao.StatementList;
import net.jami.jams.common.objects.contacts.Contact;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import static net.jami.jams.server.Server.dataStore;
@WebServlet("/api/auth/contacts") @WebServlet("/api/auth/contacts")
public class ContactServlet extends HttpServlet { public class ContactServlet extends HttpServlet {
@Override @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp); StatementList statementList = new StatementList();
statementList.addStatement(new StatementElement("owner","=",req.getAttribute("username").toString(),""));
List<Contact> contactList = dataStore.getContactDao().getObjects(statementList);
resp.getOutputStream().write(JsonStream.serialize(contactList).getBytes());
} }
@Override @Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPut(req, resp); Contact contact = JsonIterator.deserialize(req.getInputStream().readAllBytes(),Contact.class);
contact.setOwner(req.getAttribute("username").toString());
if(dataStore.getContactDao().storeObject(contact)) resp.setStatus(200);
else resp.sendError(500,"Could not store device!");
} }
//TODO: Because deleting requires sending the URI in the body, didn't want to do this now.
@Override @Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doDelete(req, resp); StatementList statementList = new StatementList();
statementList.addStatement(new StatementElement("owner","=",req.getAttribute("username").toString(),"AND"));
statementList.addStatement(new StatementElement("uri","=",req.getParameter("uri"),""));
if(dataStore.getContactDao().deleteObject(statementList)) resp.setStatus(200);
else resp.sendError(500,"Could not delete a contact!");
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment