Java tutorial
/* EduMsg is made available under the OSI-approved MIT license. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package edumsg.core.commands.dm; import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.node.ValueNode; import edumsg.core.*; import edumsg.redis.Cache; import edumsg.redis.DMCache; import org.json.JSONObject; import org.postgresql.util.PSQLException; import java.io.IOException; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.sql.Types; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; public class GetConversationCommand extends Command implements Runnable { private final Logger LOGGER = Logger.getLogger(GetConversationCommand.class.getName()); @Override public void execute() { try { dbConn = PostgresConnection.getDataSource().getConnection(); dbConn.setAutoCommit(false); proc = dbConn.prepareCall("{? = call get_conversation(?)}"); proc.setPoolable(true); proc.registerOutParameter(1, Types.OTHER); proc.setInt(2, Integer.parseInt(map.get("conv_id"))); proc.execute(); set = (ResultSet) proc.getObject(1); root.put("app", map.get("app")); root.put("method", map.get("method")); root.put("status", "ok"); root.put("code", "200"); Conversation conv = new Conversation(); ArrayList<DirectMessage> dms = new ArrayList<>(); while (set.next()) { int sender_id = set.getInt(1); String sender_name = set.getString(2); String sender_username = set.getString(3); int reciever_id = set.getInt(4); String reciever_name = set.getString(5); String receiver_username = set.getString(6); String dm_text = set.getString(7); String image_url = set.getString(8); Timestamp created_at = set.getTimestamp(9); String sender_avatar = set.getString(10); String receiver_avatar = set.getString(11); User sender = new User(); sender.setId(sender_id); sender.setName(sender_name); sender.setAvatarUrl(sender_avatar); sender.setUsername(sender_username); User reciever = new User(); reciever.setId(reciever_id); reciever.setName(reciever_name); reciever.setAvatarUrl(receiver_avatar); reciever.setUsername(receiver_username); DirectMessage dm = new DirectMessage(); dm.setDmText(dm_text); dm.setSender(sender); dm.setReciever(reciever); dm.setCreatedAt(created_at); dm.setImageUrl(image_url); dms.add(dm); } conv.setDms(dms); ValueNode child = nf.pojoNode(conv); root.set("conv", child); try { CommandsHelp.submit(map.get("app"), mapper.writeValueAsString(root), map.get("correlation_id"), LOGGER); JSONObject cacheEntry = new JSONObject(mapper.writeValueAsString(root)); cacheEntry.put("cacheStatus", "valid"); DMCache.dmCache.set("get_conv:" + map.getOrDefault("session_id", ""), cacheEntry.toString()); } catch (JsonGenerationException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); } catch (JsonMappingException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); } catch (IOException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); } dbConn.commit(); } catch (PSQLException e) { CommandsHelp.handleError(map.get("app"), map.get("method"), e.getMessage(), map.get("correlation_id"), LOGGER); LOGGER.log(Level.SEVERE, e.getMessage(), e); } catch (SQLException e) { CommandsHelp.handleError(map.get("app"), map.get("method"), e.getMessage(), map.get("correlation_id"), LOGGER); LOGGER.log(Level.SEVERE, e.getMessage(), e); } finally { PostgresConnection.disconnect(set, proc, dbConn); } } }