Created
February 14, 2020 03:10
-
-
Save Pchelolo/cc9e7e0ba999a82c58ef7b1056e8ece6 to your computer and use it in GitHub Desktop.
Horrible java code for interview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.*; | |
import java.util.*; | |
import java.util.stream.Collectors; | |
public class Main { | |
public static void main(String[] args) { | |
System.out.println("Starting the application"); | |
String database = args[0]; | |
System.out.println("Getting data from database at " + database); | |
Main main = new Main(); | |
Connection connection = databaseConnect(database); | |
Statement selectDataStatement = null; | |
try { | |
selectDataStatement = connection.createStatement(); | |
} catch (SQLException e) { | |
System.out.println("Statement create unsuccessfull"); | |
e.printStackTrace(); | |
} | |
int page_id = Integer.parseInt(args[1]); | |
System.out.println("Getting data for page id " + page_id); | |
ResultSet results = null; | |
try { | |
results = selectDataStatement.executeQuery("SELECT * FROM \"main\".\"revision\" WHERE page_id = " + page_id + ";"); | |
} catch (SQLException e) { | |
System.out.println("Can not select data"); | |
e.printStackTrace(); | |
} | |
try { | |
Object[] data = getRevisionData(results); | |
int[] textIds = new int[data.length]; | |
for (int i = 1; i < data.length; i++) { | |
textIds[i] = ((int[]) data[i])[2]; | |
} | |
System.out.println("Extracted text IDs: " + Arrays.toString(textIds)); | |
Map<Integer, List<String>> textByPageId = fetchCitationNeededText(data); | |
System.out.println("Fetched revision text by Revision ID: \n" + textByPageId); | |
} catch (SQLException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public static Connection databaseConnect(String database) { | |
// db parameters | |
String url = "jdbc:sqlite:/Users/ppchelko/wiki/CitationNeededGrabber/" + database; | |
// create a connection to the database | |
try { | |
return DriverManager.getConnection(url); | |
} catch (SQLException e) { | |
System.out.println("Connection unsuccessfull"); | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
public static Object[] getRevisionData(ResultSet resultSet) throws SQLException { | |
LinkedList<ArrayList> list = new LinkedList(); | |
while (resultSet.next()) { // TODO: change ro do-while | |
ArrayList result = new ArrayList(); | |
result.add(resultSet.getInt(1)); | |
result.add(resultSet.getInt(2)); | |
result.add(resultSet.getInt(3)); | |
result.add(resultSet.getInt(4)); | |
list.add(result); | |
} | |
; | |
return list.stream().map((ArrayList item) -> new int[]{(int) item.get(0), (int) item.get(2), (int) item.get(3)}).toArray(); | |
} | |
static Map<Integer, List<String>> fetchCitationNeededText(Object[] revisions) { | |
return Arrays.stream(revisions) | |
.parallel() | |
.collect(Collectors.groupingBy( | |
(revision) -> ((int[])revision)[0], // Revision ID, | |
Collectors.mapping( | |
(revision) -> { | |
int textId = ((int[])revision)[2]; | |
StringBuffer res = new StringBuffer(); | |
res.append("Revision "); | |
res.append(textId); | |
res.append(" "); | |
if (Math.random() < 0.5) { | |
res.append("{{Citation needed}}"); | |
} | |
return res.toString(); | |
}, | |
Collectors.toList() | |
)) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment