I am trying to retrieve and process code from JIRA, unfortunately the pieces of information (which are in the Metadata-Plugin) are saved in a column, not a row.
Picture of JIRA-MySQL-Database
The goal is to save this in an object with following attributes:
public class DesiredObject {
private String Object_Key;
private String Aze.kunde.name;
private Long Aze.kunde.schluessel;
private String Aze.projekt.name;
private Long Aze.projekt.schluessel
//getters and setters here
}
My workbench is STS and it's a Spring-Boot-Application.
I can fetch a List of Object-Keys with the JRJC using:
JiraController jiraconnect = new JiraController();
List<JiraProject> jiraprojects = new ArrayList<JiraProject>();
jiraprojects = jiraconnect.findJiraProjects();
This is perfectly working, also the USER_KEY and USER_VALUE are easily retrievable, but I hope there is a better way than to perform
three SQL-Searches for each project and then somehow build an object from all those lists.
I was starting with
for (JiraProject jp : jiraprojects) {
String SQL = "select * from jira_metadata where ENRICHED_OBJECT_KEY = ?";
List<DesiredObject> do = jdbcTemplateObject.query(SQL, new Object[] { "com.atlassian.jira.project.Project:" + jp.getProjectkey() }, XXX);
}
to get a list with every object, but I'm stuck as i can't figure out a ObjectMapper (XXX) who is able to write this into an object.
Usually I go with
object.setter(rs.getString("SQL-Column"));
But that isn't working, as all my columns are called the same. (USER_KEY & USER_VALUE)
The Database is automatically created by JIRA, so I can't "fix" it.
The Object_Keys are unique which is why I tried to use those to collect all the data from my SQL-Table.
I hope all you need to enlighten me is in this post, if not feel free to ask for more!
Edit: Don't worry if there are some 'project' and 'projekt', that's because I gave most of my classes german names and descriptions..
I created a Hashmap with the Objectkey and an unique token in brackets, e.g.: "(1)JIRA".
String SQL = "select * from ao_cc6aeb_jira_metadata";
List<JiraImportObjekt> jioList = jdbcTemplateObject.query(SQL, new JiraImportObjektMapper());
HashMap<String, String> hmap = new HashMap<String, String>();
Integer unique = 1;
for (JiraImportObjekt jio : jioList) {
hmap.put("(" + unique.toString() + ")" + jio.getEnriched_Object_Key(),
jio.getUser_Key() + "(" + jio.getUser_Value() + ")");
unique++;
}
I changed this into a TreeMap
Map<String, String> tmap = new TreeMap<String, String>(hmap);
And then i iterated through that treemap via
String aktuProj = new String();
for (String s : tmap.keySet()) {
if (aktuProj.equals(s.replaceAll("\\([^\\(]*\\)", ""))) {
} else { //Add Element to list and start new Element }
//a lot of other stuff
}
What I did was to put all the data in the right order, iterate through and process everything like I wanted it.
Object hinfo = hmap.get(s);
if (hinfo.toString().replaceAll("\\([^\\(]*\\)", "").equals("aze.kunde.schluessel")) {
Matcher m = Pattern.compile("\\(([^)]+)\\)").matcher(hinfo.toString());
while (m.find()) {
jmo[obj].setAzeKundeSchluessel(Long.parseLong(m.group(1), 10));
// logger.info("AzeKundeSchluessel: " +
// jmo[obj].getAzeKundeSchluessel());
}
} else ...
After the loop I needed to add the last Element.
Now I have a List with the Elements which is easy to use and ready for further steps.
I cut out a lot of code because most of it is customized for my problem.. the roadmap should be enough to solve it though.
Good luck!
Related
I have a Set of data. For example:
car accord
car civic
suv landcruzer
suv landrover
muv innova
I want store it in a scanner or hash map and retrieve the values based on the input.
If "car" is the input I want to pass URL+/accord and URL+/civic as its output
If "muv" is the input, I want to pass URL+/innova as its output
String URL = "www.abc.com";
String Vehicletype = "";
#DataProvider(name = "irLanguage")
public Object[][] lang() {
#SuppressWarnings("resource")
Scanner s = new Scanner(
"Car /accord/\n" +
"Car /civic/\n" +
"suv /landcruzer/\n" +
"suv /rangerover/\n" +
"muv /innova/\n");
Map<String, List<String>> map = new LinkedHashMap<String, List<String>>();
while (s.hasNext()) {
String key = s.next();
if (!map.containsKey(key))
map.put(key, new LinkedList<String>());
map.get(key).add(s.next());
}
urlArray = map.get(vehicletype);
String[][] shades = new String[urlArray.size()][2];
for (int i = 0; i < urlArray.size(); i++) {
shades[i][0] = urlArray.get(i).toString();
shades[i][1] = URL + urlArray.get(i).toString();
lang = shades[i][0];
System.out.println(shades[i][0]);
}
return shades;
}
Here, the code is working fine. That is , if the input vehicle type is car then the output url is www.abc.com/accord/ and www.abc.com/civic/
and if the vehicle type is muv, it only returns www.abc.com/innova/ . This setup works fine for me. But, I wonder if there is any simpler method to do this.
Can anybody with good knowledge in java can help?
You have the right idea, I would build a HashMap that contains one Key (e.g. "car") and all the desired Values for that Key (e.g. "accord", "civic")
HashMap<String, ArrayList<String>> vehicles = new HashMap<String, ArrayList<String>>();
ArrayList<String> makes = new ArrayList<String>();
makes.add("accord");
makes.add("civic");
vehicles.put("car", makes);
makes.clear();
makes.add("landcruzer");
makes.add("rangerover");
vehicles.put("suv", makes);
makes.clear();
makes.add("innova");
vehicles.put("muv", makes);
makes.clear();
Now that you've got the vehicles HashMap built, you can fetch a Key and get all Values and build your URLs.
makes = vehicles.get("car");
for (String make : makes)
{
System.out.println("www.abc.com/" + make);
}
I have a Four different enum UserOneStatus, UserTwoStatus, UserThreeType and UserFourType which has ID and Name
I have to filter the User based on the those Enum ID's so, I end up with the following code
if (ChoosenUserFilterValue.equals(String.valueOf(UserOneStatus.REQUESTED.getId()))) {
selecedUserFilter = new UserFilter(UserOneStatus.class,
String.valueOf(UserOneStatus.REQUESTED.getId()),
UserOneStatus.REQUESTED.getUserStatus(), String.valueOf(UUID.randomUUID()));
} else if (ChoosenUserFilterValue.equals(UserOneStatus.ACTIVE.getId() + "")) {
selecedUserFilter = new UserFilter(UserOneStatus.class,
String.valueOf(UserOneStatus.ACTIVE.getId()),
UserOneStatus.ACTIVE.getUserStatus(), String.valueOf(UUID.randomUUID()));
} else if (ChoosenUserFilterValue.equals(UserOneStatus.INACTIVE.getId() + "")) {
selecedUserFilter = new UserFilter(UserOneStatus.class,
String.valueOf(UserOneStatus.INACTIVE.getId()),
UserOneStatus.INACTIVE.getUserStatus(), String.valueOf(UUID.randomUUID()));
} else if (ChoosenUserFilterValue.equals(UserTwoStatus.REVOKED.getId() + "")) {
selecedUserFilter = new UserFilter(UserTwoStatus.class,
String.valueOf(UserTwoStatus.REVOKED.getId()),
UserTwoStatus.REVOKED.getLoginStatus(), String.valueOf(UUID.randomUUID()));
} else if (ChoosenUserFilterValue.equals(UserThreeType .ADMIN.getId() + "")) {
selecedUserFilter = new UserFilter(Some.class,
String.valueOf(UserThreeType.ADMIN.getId()),
UserThreeType.ADMIN.getName(), String.valueOf(UUID.randomUUID()));
} else if (ChoosenUserFilterValue.equals(UserFourType .SSO_TEMPLATE.getId() + "")) {
selecedUserFilter = new UserFilter(Some.class,
String.valueOf(UserFourType.SSO_TEMPLATE.getId()),
UserFourType.TEMPLATE.getName(), String.valueOf(UUID.randomUUID()));
}
I refactored It little bit but I could't able to get the results as same as the upper code
The refactored code is as follows
Map<String,Class<?>> map = new HashMap<String, Class<?>>();
map.put(UserOneStatus.REQUESTED.getId()+"", UserOneStatus.class);
map.put(UserOneStatus.ACTIVE.getId()+"", UserOneStatus.class);
map.put(UserOneStatus.INACTIVE.getId()+"", UserOneStatus.class);
map.put(UserTwoStatus.REVOKED.getId()+"", UserTwoStatus.class);
map.put(UserThreeType.ADMIN.getId()+"", Some.class);
map.put(UserFourType.TEMPLATE.getId()+"", Some.class);
String key ="";
if(map.containsKey(ChoosenUserFilterValue)){
Class<?> getSelectedUserFilterValueClass = map.get(selectedUserFilterValue);
for (Entry<String, Class<?>> entry : map.entrySet()) {
key = entry.getKey();
}
selecedUserFilter = new UserFilter(getSelectedUserFilterValueClass, key , getSelectedUserFilterValueClass.toString(), String.valueOf(UUID.randomUUID());
}
any suggestions are appreciated
Thanks
Problems of creating objects of one interface/abstract class, based on some kind of input data (like ids, numbers, etc.) are supposed to be solved using factory method design pattern, or abstract factory pattern. Your code is a kind of factory method, and using if/else statements are perfectly fine in such situations. Here you can check an example of factory method, and abstract factory.
This is actually a re-do of an older question of mine that I have completely redone because my old question seemed to confuse people.
I have written a Java program that Queries a database and is intended to retrieve several rows of data. I have previously written the program in Informix-4GL and I am using a sql cursor to loop through the database and store each row into a "dynamic row of record". I understand there are no row of records in Java so I have ended up with the following code.
public class Main {
// DB CONNECT VARIABLE ===========================
static Connection gv_conn = null;
// PREPARED STATEMENT VARIABLES ==================
static PreparedStatement users_sel = null;
static ResultSet users_curs = null;
static PreparedStatement uinfo_sel = null;
static ResultSet uinfo_curs = null;
// MAIN PROGRAM START ============================
public static void main(String[] args) {
try {
// CONNECT TO DATABASE CODE
} catch(Exception log) {
// YOU FAILED CODE
}
f_prepare(); // PREPARE THE STATEMENTS
ArrayList<Integer> list_id = new ArrayList<Integer>();
ArrayList<String> list_name = new ArrayList<String>();
ArrayList<Integer> list_info = new ArrayList<String>();
ArrayList<String> list_extra = new ArrayList<String>();
try {
users_sel.setInt(1, 1);
users_curs = users_sel.executeQuery();
// RETRIEVE ROWS FROM USERS
while (users_curs.next()) {
int lv_u_id = users_curs.getInt("u_id");
String lv_u_name = users_curs.getString("u_name");
uinfo_sel.setInt(1, lv_u_id);
uinfo_curs = uinfo_sel.executeQuery();
// RETRIEVE DATA FROM UINFO RELATIVE TO USER
String lv_ui_info = uinfo_curs.getString("ui_info");
String lv_ui_extra = uinfo_curs.getString("ui_extra");
// STORE DATA I WANT IN THESE ARRAYS
list_id.add(lv_u_id);
list_name.add(lv_u_name);
list_info.add(lv_ui_info);
list_extra.add(lv_ui_extra);
}
} catch(SQLException log) {
// EVERYTHING BROKE
}
// MAKING SURE IT WORKED
System.out.println(
list_id.get(0) +
list_name.get(0) +
list_info.get(0) +
list_extra.get(0)
);
// TESTING WITH ARBITRARY ROWS
System.out.println(
list_id.get(2) +
list_name.get(5) +
list_info.get(9) +
list_extra.get(14)
);
}
// PREPARE STATEMENTS SEPARATELY =================
public static void f_prepare() {
String lv_sql = null;
try {
lv_sql = "select * from users where u_id >= ?"
users_sel = gv_conn.prepareStatement(lv_sql);
lv_sql = "select * from uinfo where ui_u_id = ?"
uinfo_sel = gv_conn.prepareStatement(lv_sql)
} catch(SQLException log) {
// IT WON'T FAIL COZ I BELIEEEVE
}
}
}
class DBConn {
// connect to SQLite3 code
}
All in all this code works, I can hit the database once, get all the data I need, store it in variables and work with them as I please however this does not feel right and I think it's far from the most suited way to do this in Java considering I can do it with only 15 lines of code in Informix-4GL.
Can anyone give me advice on a better way to achieve a similar result?
In order to use Java effectively you need to use custom objects. What you have here is a lot of static methods inside a class. It seems that you are coming from a procedural background and if you try to use Java as a procedural language, you will not much value from using it. So first off create a type, you can plop it right inside your class or create it as a separate file:
class User
{
final int id;
final String name;
final String info;
final String extra;
User(int id, String name, String info, String extra)
{
this.id = id;
this.name = name;
this.info = info;
this.name = name;
}
void print()
{
System.out.println(id + name + info + extra);
}
}
Then the loop becomes:
List<User> list = new ArrayList<User>();
try {
users_sel.setInt(1, 1);
users_curs = users_sel.executeQuery();
// RETRIEVE ROWS FROM USERS
while (users_curs.next()) {
int lv_u_id = users_curs.getInt("u_id");
String lv_u_name = users_curs.getString("u_name");
uinfo_sel.setInt(1, lv_u_id);
uinfo_curs = uinfo_sel.executeQuery();
// RETRIEVE DATA FROM UINFO RELATIVE TO USER
String lv_ui_info = uinfo_curs.getString("ui_info");
String lv_ui_extra = uinfo_curs.getString("ui_extra");
User user = new User(lv_u_id, lv_u_name, lv_ui_info, lv_ui_extra);
// STORE DATA
list.add(user);
}
} catch(SQLException log) {
// EVERYTHING BROKE
}
// MAKING SURE IT WORKED
list.get(0).print();
This doesn't necessarily address the number of lines. Most people who use Java don't interact with databases with this low-level API but in general, if you are looking to get down to the fewest number of lines (a questionable goal) Java isn't going to be your best choice.
Your code is actually quite close to box stock JDBC.
The distinction is that in Java, rather than having a discrete collection of arrays per field, we'd have a simple Java Bean, and a collection of that.
Some examples:
public class ListItem {
Integer id;
String name;
Integer info;
String extra;
… constructors and setters/getters ellided …
}
List<ListItems> items = new ArrayList<>();
…
while(curs.next()) {
ListItem item = new ListItem();
item.setId(curs.getInt(1));
item.setName(curs.getString(2));
item.setInfo(curs.getInfo(3));
item.setExtra(curs.getString(4));
items.add(item);
}
This is more idiomatic, and of course does not touch on the several frameworks and libraries available to make DB access a bit easier.
Below is my current code:
private void processData(StringBuffer reportBuffer) {
String tableName = "db2_table_name";
ArrayList<HashMap<String, String>> tableValue = FileData.get(tableName);
String No = "No";
String versionNumber = "VersionNumber";
for (HashMap<String, String> fieldValue : tableValue) {
String No = fieldValue.get(No);
String Version = fieldValue.get(versionNumber);
reportBuffer.append("No is: " + No + " and Version is: " +Version + NL);
}
}
The current output of this is:
No is: 1. and Version is: 1.
No is: 1. and Version is: 2.
No is: 3. and Version is: 1.
No is: 3. and Version is: 2.
No is: 3. and Version is: 3.
What I am looking to do is only keep the latest version of each No while removing the element of the older versions. So within my new ArrayList I would ideally want to only have:
No is: 1. and Version is: 2.
No is: 3. and Version is: 3
Let me know if you need any clarifications!
Thanks!
.
You could create a new HashMap and save the latest version for each No inside that hashmap.
You should be able to do that by saving each No and Version in the hashmap while you go through the for loop you have there. However, every time before you save a value, you should check if the hashmap contains the current No, and if yes don't save it but check if the stored version is smaller than the current one and replace it.
At the end, create a loop to go around the hashmap and use it along with the reportBuffer.append()
Heres one way
Keep a Map<String,Integer> lowestAmongMaps = new HashMap<String,Integer>();
In your for loop, add the lowest value for each key to this map..
basically, something like this
Integer currentLowest = Integer.valueOf(lowestAmongMaps.get(versionNumber));
if(currentLowest == null){
currentLowest = Integer.MAX_VALUE;
}
Integer currentVal = Integer.valueOf(Version).intValue();
if( currentVal.intValue() < currentLowest.intValue()){
lowestAmongMaps.put(versionNumber,currentVal);
}
Now in the next iteration remove all keys which are not lowest.
So basically you have two iterations over
for (HashMap<String, String> fieldValue : tableValue)
If its a database the best way would be to modify the query to return only max version from FileData.get(). But I suppose its a file. So a simple solution would be to keep a third map to store the report values. Eg
private void processData(StringBuffer reportBuffer) {
String tableName = "db2_table_name";
ArrayList<HashMap<String, String>> tableValue = FileData.get(tableName);
String no = "No";
String versionNumber = "VersionNumber";
Map<String, String> mergedMap = new HashMap<String, String>();
for (HashMap<String, String> fieldValue : tableValue) {
String No = fieldValue.get(no);
String Version = fieldValue.get(versionNumber);
if(mergedMap.containsKey(No)){
Integer previousVersion = Integer.valueOf(mergedMap.get(No));
Integer currentVersion = Integer.valueOf(Version);
if(currentVersion > previousVersion){
mergedMap.put(No, Version);
}
} else {
mergedMap.put(No, Version);
}
}
for(Entry<String, String> entry : mergedMap.entrySet()){
reportBuffer.append("No is: " + entry.getKey() + " and Version is: " + entry.getValue() + NL);
}
}
I have a string in the format nm=Alan&hei=72&hair=brown
I would like to split this information up, add a conversion to the first value and print the results in the format
nm Name Alan
hei Height 72
hair Hair Color brown
I've looked at various methods using the split function and hashmaps but have had no luck piecing it all together.
Any advice would be very useful to me.
Map<String, String> aliases = new HashMap<String, String>();
aliases.put("nm", "Name");
aliases.put("hei", "Height");
aliases.put("hair", "Hair Color");
String[] params = str.split("&"); // gives you string array: nm=Alan, hei=72, hair=brown
for (String p : params) {
String[] nv = p.split("=");
String name = nv[0];
String value = nv[1];
System.out.println(nv[0] + " " + aliases.get(nv[0]) + " " + nv[1]);
}
I really do not understand what you problem was...
Try something like this:
static final String DELIMETER = "&"
Map<String,String> map = ...
map.put("nm","Name");
map.put("hei","Height");
map.put("hair","Hair color");
StringBuilder builder = new StringBuilder();
String input = "nm=Alan&hei=72&hair=brown"
String[] splitted = input.split(DELIMETER);
for(Stirng str : splitted){
int index = str.indexOf("=");
String key = str.substring(0,index);
builder.append(key);
builder.append(map.get(key));
builder.append(str.substring(index));
builder.append("\n");
}
A HashMap consists of many key, value pairs. So when you use split, devise an appropriate regex (&). Once you have your string array, you can use one of the elements as the key (think about which element will make the best key). However, you may now be wondering- "how do I place the rest of elements as the values?". Perhaps you can create a new class which stores the rest of the elements and use objects of this class as values for the hashmap.
Then printing becomes easy- merely search for the value of the corresponding key. This value will be an object; use the appropriate method on this object to retrieve the elements and you should be able to print everything.
Also, remember to handle exceptions in your code. e.g. check for nulls, etc.
Another thing: your qn mentions the word "sort". I don't fully get what that means in this context...
Map<String, String> propsMap = new HashMap<String, String>();
Map<String, String> propAlias = new HashMap<String, String>();
propAlias.put("nm", "Name");
propAlias.put("hei", "Height");
propAlias.put("hair", "Hair Color");
String[] props = input.split("&");
if (props != null && props.length > 0) {
for (String prop : props) {
String[] propVal = prop.split("=");
if (propVal != null && propVal.length == 2) {
propsMap.put(propVal[0], propVal[1]);
}
}
}
for (Map.Entry tuple : propsMap.getEntrySet()) {
if (propAlias.containsKey(tuple.getKey())) {
System.out.println(tuple.getKey() + " " + propAlias.get(tuple.getKey()) + " " + tuple.getValue());
}
}