change filtration criteriain smartGWT - java

i am developing a smartGWT app, and i want to filter the listGrid fields , in a way other than the default one , (ie. the default filtration is based on dearching on a contains matching )
to be more spastic ill give an example :
if a filed have 2 values one of them is the word "valid" and the other is the word "invalid" then the filtration works correctly for searching the word invalid but when i want to see "valid" , it will give me all the "valid" and "invalid" words since "invalid" consists of "in"+"valid"
registeredDate = new DataSourceDateField("registrationDate", voc.registeredDate());
registeredDate.setRequired(true);
verificationDate = new DataSourceDateField("lastVerificationDate", voc.verificationDate());
verificationDate.setRequired(true);
the same as every other field
this is how i fill records :
registeredUsersRecords = new ListGridRecord[registeredUsers.length()];
ListGridRecord record = new ListGridRecord();
record.setAttribute(ID, user.getId());
record.setAttribute("firstName", user.getFirstName());
record.setAttribute("lastName", user.getLastName());
record.setAttribute("email", user.getEmail());
record.setAttribute("userMainType", type);
record.setAttribute("isActivated", (user.isActivated())? voc.active(): voc.inActive());
record.setAttribute("country", user.getSelectedCountry().getValue());
record.setAttribute("companyName", user.getCompanyName());
record.setAttribute("registrationDate", user.getRegistrationDate());
record.setAttribute("lastVerificationDate", user.getVerificationDate());
registeredUsersRecords[i] = record;
and then i put them into datasource :
DataSource ds = new DataSource();
ds.setClientOnly(true);
ds.setFields(fName, lName, email, type,typeDetails, status, country, companyName, registeredDate,verificationDate);
for(int i = 0; i< registeredUsersRecords.length; i++){
ds.addData(registeredUsersRecords[i]);
}
registeredUsersListGrid.setDataSource(ds);
registeredUsersListGrid.fetchData();

I think FilterEditorSubmitHandler will solve your problem.
I have already posted a sample code at your last question here date filtering not working in smart gwt.
You have to do some modification in it as shown below:
Note: In the below sample code I have replaced the filter criteria operator from ICONTAINS to STARTS_WITH. Modify it as per your requirement.
--EDIT--
Complete code:
class User {
private int id;
private String firstName;
private Date registrationDate;
public User(int id, String firstName, Date registrationDate) {
this.id = id;
this.firstName = firstName;
this.registrationDate = registrationDate;
}
public int getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public Date getRegistrationDate() {
return registrationDate;
}
}
DateTimeFormat format = DateTimeFormat.getFormat("MM/dd/yyyy");
User[] registeredUsers = new User[] { new User(1, "valid", format.parse("01/20/2014")),
new User(2, "invalid", format.parse("05/20/2013")),
new User(3, "valid", format.parse("02/20/2014")) };
ListGridRecord[] registeredUsersRecords = new ListGridRecord[registeredUsers.length];
for (int i = 0; i < registeredUsers.length; i++) {
User user = registeredUsers[i];
ListGridRecord record = new ListGridRecord();
record.setAttribute("id", user.getId());
record.setAttribute("firstName", user.getFirstName());
record.setAttribute("registrationDate", user.getRegistrationDate());
registeredUsersRecords[i] = record;
}
DataSourceDateField registeredDate = new DataSourceDateField("registrationDate", "Date");
DataSourceTextField firstName = new DataSourceTextField("firstName", "Name");
DataSourceIntegerField id = new DataSourceIntegerField("id", "ID");
id.setRequired(true);
id.setPrimaryKey(true);
id.setHidden(true);
DataSource ds = new DataSource();
ds.setClientOnly(true);
ds.setFields(id, firstName, registeredDate);
for (int i = 0; i < registeredUsersRecords.length; i++) {
ds.addData(registeredUsersRecords[i]);
}
final ListGrid registeredUsersListGrid = new ListGrid();
registeredUsersListGrid.setDataSource(ds);
registeredUsersListGrid.fetchData();
registeredUsersListGrid.setShowFilterEditor(true);
registeredUsersListGrid.addFilterEditorSubmitHandler(new FilterEditorSubmitHandler() {
#Override
public void onFilterEditorSubmit(FilterEditorSubmitEvent event) {
event.cancel();
if (event.getCriteria() != null) {
AdvancedCriteria advancedCriteria = event.getCriteria().asAdvancedCriteria();
// store only single criteria for each field(column)
Map<String, Criterion> criterions = new HashMap<String, Criterion>();
for (final Criterion criterion : advancedCriteria.getCriteria()) {
System.out.println(criterion.getFieldName());
System.out.println(criterion.getValueAsString());
System.out.println(criterion.getOperator());
if (criterion.getOperator() == OperatorId.ICONTAINS) {
Criterion newCritearia = new Criterion(criterion.getFieldName(),
OperatorId.STARTS_WITH, criterion.getValueAsString());
criterions.put(criterion.getFieldName(), newCritearia);
} else {
criterions.put(criterion.getFieldName(), criterion);
}
}
if (criterions.size() > 0) {
AdvancedCriteria filterCriteria = new AdvancedCriteria(OperatorId.AND,
criterions.values().toArray(new Criterion[] {}));
registeredUsersListGrid.fetchData(filterCriteria);
}
}
}
});
Button button = new Button("Clear all filters");
button.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
registeredUsersListGrid.fetchData();
}
});
VLayout layout = new VLayout();
layout.setWidth("200px");
layout.setHeight("200px");
layout.addMember(button);
layout.addMember(registeredUsersListGrid);
layout.draw();

Related

How to export huge data(near 1 million data ) using a csv file using Java

I am trying to export data from a PostgreSQL DB where the volume of data i have to export is near about 1 million. I have tried various approach but didn't get a solution for it.Even if i am using the postman for the calling the API , which i have written to export the csv, the postman is shutting down.I am using react.js to download the but there it is loading for hours. I am posting the code for the export
public String populateCsvReport(SearchDto searchDto){
List<DetailRecord> myDetailRecord = itsCustomRepo.getDetail(searchDto);
StringWriter sw = new StringWriter();
try(CSVPrinter csvPrinter = new CSVPrinter(sw,CSVFormat.DEFAULT.withHeader("Supplier Number"
"Supplier name"........
)){
myDetailRecord.forEach(mydetail->{
csvPrinter.printRecord(
mydetail.getSuplNum(),
mydetail.getSuplName(),
......................
)
});
return myDetailRecord;
Now Here i have also tried to change my code as
myDetailRecord.forEach(mydetail->{
mydetail.getSuplNum(),
mydetail.getSuplName(),
......................
});
csvPrinter.printRecord(
myDetailRecord
);
But it's didn't create an impact on my code.
And in my controller i am doing like
#Getmapping(path="/get-export-detail/csv"){
public RespnseEntity<String> generateMydetailExport(SearchDto searchDto){
return ResponseEntity.ok()
.header("Content-Disposition","attachment;fileName="+"myDetails.csv")
.contentType(MediaType.parseMediaType("text/csv"))
.body(callingService.populateCSVForDetail(searchDto));
And here i am using the react.js code to export the file
const exportOnClick=()=>{
callingDetailsService.export(param)
.then(response)=>{
let mime = "test/csv";
let fileName = "myDetail.csv";
util.downloadFile(response.data,fileName,mime);
Here is my custom repository Code
#Repository
public class ItsCustomRepo{
#PersistanceContext
private EntityManager entityManager;
public List<DetailRecord> getDetail(List<SearchCriteria> params){
List<DetailRecord> listOfDetail = new ArrayList<>();
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<DetailEntity> cQuery = cb.createQuery(DetailEntity.class);
Root<DetailEntity> rootE = cQuery.from(DetailEntity.class);
String sqlQuery = "select ............."
if(params.size()>0){
for(SearchCriteria param:params){
if(param.getValue()!=null && param.getValue()!=""){
if(param.getOperation().equalIgnoreCase(CriteriaOperationEnum.GREATER_THAN_OR_EQUAL.getOperation()){
if(rootE.get(param.getKey()).getJavaType()==LocalDate.class){
}else if(param.getOperation().equalIgnoreCase(CriteriaOperationEnum.LESS_THAN_OR_EQUAL.getOperation()
//some op
}else{ if(param.getOPeration().equalsIgnoreCase(CriteriaOperationEnum.LIKE.getOperation())){
//some op
}
}
Query query = entityManager.createNativeQuery(sqlQuery);
List<Object[]> objectList = query.getResultList();
int count = objectList.size();
objectList.forEach(glObject->{
DetailRecord detailRecord = DetailRecord.builder()
.supl_num(glObject[0])
...................
listOfDetail .add(detailRecord);
});
return listOfDetail;
My code is simple by i don't understand where it is getting failed, i am checking the count of the DB while running the Query and it is fast , and also i can see while debugging the code is smoothly coming to the controller but after that it is hanging for hours and hours.I have tried using opencsv,apache- poi etc. Can't understand where it is failing, someone please help me.
Here is some sample code that generates some CSV with two methods. The first one is similar to yours -- it gets all rows in a list and then creates csv. The second method is more "streaming" in that it writes out the rows as soon as it can get them from the database. With 1M rows, that makes a big difference.
import org.h2.jdbcx.JdbcDataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import java.util.function.Consumer;
public class CsvSample {
static class Player {
int id;
String name;
int teamId;
Player(int id, String name, int temId) {
this.id = id;
this.name = name;
this.teamId = temId;
}
}
interface PlayerRepo {
void save(Player player);
List<Player> findPlayers(int teamId);
int processPlayers(int teamId, Consumer<Player> callback);
}
static class SimplePlayerRepo implements PlayerRepo {
JdbcTemplate jdbc;
SimplePlayerRepo(JdbcTemplate jdbc) {
this.jdbc = jdbc;
this.jdbc.execute("create table if not exists Player(id int primary key, name varchar(30), team int)");
}
#Override
public void save(Player player) {
int n = jdbc.update(
"update Player set name=?, team=? where id=?",
player.name, player.teamId, player.id);
if (n == 0) {
jdbc.update(
"insert into Player(name, team, id) values (?, ?, ?)",
player.name, player.teamId, player.id);
}
}
#Override
public List<Player> findPlayers(int teamId) {
return jdbc.query(
"select id, name, team from Player where team=?",
(rs, n) -> new Player(rs.getInt(1), rs.getString(2), rs.getInt(3)),
teamId);
}
#Override
public int processPlayers(int teamId, Consumer<Player> callback) {
return jdbc.query(
"select id, name, team from Player where team=?",
rs -> {
int n = 0;
while (rs.next()) {
Player p = new Player(rs.getInt(1), rs.getString(2), rs.getInt(3));
callback.accept(p);
}
return n;
},
teamId);
}
}
public static void main(String[] args) throws Exception {
JdbcDataSource dataSource = new JdbcDataSource();
dataSource.setUrl("jdbc:h2:mem:csvsample;DB_CLOSE_DELAY=-1");
PlayerRepo repo = new SimplePlayerRepo(new JdbcTemplate(dataSource));
// add some players
repo.save(new Player(1, "Kobe", 1));
repo.save(new Player(2, "LeBron", 1));
repo.save(new Player(3, "Shaq", 1));
repo.save(new Player(4, "Kareem", 1));
repo.save(new Player(5, "Magic", 1));
repo.save(new Player(6, "Larry", 2));
repo.save(new Player(7, "Jason", 2));
// generate CSV from List
repo.findPlayers(1).forEach(player -> {
System.out.println(player.id + "," + player.name);
});
System.out.println("----");
// generate CSV with callback
repo.processPlayers(1, player -> {
System.out.println(player.id + "," + player.name);
});
}
}
So, in you case I would add a method to your repository class. It should contain all the logic from your getDetail method until you get to the line that says Query query = entityManager.createNativeQuery(sqlQuery);:
public int processSearchResults(List<SearchCriteria> params, Consumer<DetailRecord> callback){
// instead of this:
// Query query = entityManager.createNativeQuery(sqlQuery);
Session session = entityManager.unwrap(Session.class);
return session.doWork(new Work() {
#Override
public void execute(Connection connection) throws SQLException {
Statement stmt = connection.createQuery();
ResultSet rs = stmt.executeQuery(sqlQuery);
int n = 0;
while (rs.next()) {
DetailRecord detailRecord = DetailRecord.builder()
.supl_num(rs.getObject(1))
// .....
.build();
callback.accept(detailRecord);
n++;
}
rs.close();
stmt.close();
return n;
}
});
}
Then, your generateMydetailExport may look something like this:
#Getmapping(path="/get-export-detail/csv", produces="text/csv") {
public void generateMydetailExport(SearchDto searchDto, PrintWriter out) {
CSVPrinter csvPrinter = new CSVPrinter(out, CSVFormat.DEFAULT.withHeader("Supplier Number", /* ... */));
itsCustomRepo.processSearchResults(searchDto, detail -> {
csvPrinter.printRecord(
mydetail.getSuplNum(),
mydetail.getSuplName(),
// .....
);
});
}

Is that possible to save String arraylist inside class through internal storage?

I am writing an note app and trying to save String arraylist in internalstorage and it doesn't seems to work
The user is creating and editing a table and i'm trying to save the content in arraylist, which just doesn't work.
Someone know
I created an class which i save as a ".bin" file
the function that suppose to store the arraylists:
ArrayList<String> tableLayoutBcontent=new ArrayList<>();
ArrayList<String> tableLayoutCcontent=new ArrayList<>();
ArrayList<String> tableLayoutDcontent=new ArrayList<>();
TableRow tableRow1=(TableRow)tableLayoutB.getChildAt(0);
EditText editText, editText1;
int tableCellsCountD=0;
for(int i=0;i<tableColumnCountB;i++){
editText1= (EditText) tableRow1.getChildAt(i).getTag();
tableLayoutBcontent.add(editText1.getText().toString());
for(int j=0;j<tableRowCountC;j++) {
tableRow= (TableRow)tableLayoutD.getChildAt(j);
Log.d("In create table", "number of rows and columns"+tableColumnCountB + i + j);
Log.d("In create table", "number of child counts"+tableRow.getChildCount()+tableRow1.getChildCount());
editText=(EditText)tableRow.getChildAt(i).getTag();
Log.d("In create table", "Content"+editText.getText().toString());
tableLayoutDcontent.add(editText.getText().toString());
tableCellsCountD++;
}
}
for(int i=0;i<tableRowCountC;i++){
tableRow=(TableRow)tableLayoutC.getChildAt(i);
editText=(EditText)tableRow.getChildAt(0).getTag();
tableLayoutCcontent.add(editText.getText().toString());
}
Table table= new Table(TableTitle, tableLayoutAcontent, tableLayoutBcontent, tableLayoutCcontent, tableLayoutDcontent, tableRowCountC, tableColumnCountB);
Log.d("In get table layout", "Check");
return table;
}
the function that supposed to restore the arraylists:
public void setTableLayoutData(Table table){
Title.setText(TableTitle);
TableLayoutBcontent= table.getTableLayoutBcontent();
TableLayoutCcontent= table.getTableLayoutCcontent();
TableLayoutDcontent= table.getTableLayoutDcontent();
InitialColumnsNumber = table.getColumnsNumber();
InitialRowsNumber = table.getRowsNumber();
for(int i = 0; i< InitialColumnsNumber; i++){
addColumnsToTableB(TableLayoutBcontent.get(i),i);
Log.d("Check content", "TablelayoutB content:" + TableLayoutBcontent.get(i));
}
for(int i = 0; i< InitialRowsNumber; i++){
initializeRowForTableD(i);
addRowToTableC(TableLayoutCcontent.get(i));
Log.d("Check content", "TablelayoutC content:" + TableLayoutCcontent.get(i));
for (int j = 0; j< InitialColumnsNumber; j++){
addColumnToTableD(i, TableLayoutDcontent.get(i));
}
}
}```
and the function that save the table:
public void SaveTable(){
Table table=getTableLayoutData();
Idea idea=Utilities.getIdea(IdeaTitle);
if(table!=null) {
Log.d("Idea'S title", IdeaTitle);
idea.AddTable(table);
}
if(Utilities.SaveIdea(this, idea)){
Toast.makeText(this, "Your table is saved in your idea", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this, "your table couldn't be saved for some reason", Toast.LENGTH_SHORT).show();
}
}
I expect it store the content and it just doesn't work,
anybody can try to help me with it?
edit:
Idea class code:
public class Idea implements Serializable{
private long Date;
private String Title;
private String Content;
private ArrayList<Table> Tables;
public Idea(long date, String title, String content) {
Date = date;
Title = title;
Content = content;
Tables=new ArrayList<>();
}
public Idea(long date, String title, String content, ArrayList<Table> tables) {
Date = date;
Title = title;
Content = content;
Tables = tables;
}
public long getDate() {
return Date;
}
public String getTitle() {
return Title;
}
public String getContent() {
return Content;
}
public void setDate(long date) {
Date = date;
}
public void setTitle(String title) {
Title = title;
}
public void setContent(String content) {
Content = content;
}
public ArrayList<Table> getTables() {
return Tables;
}
public void setTables(ArrayList<Table> tables) {
Tables = tables;
}
public void AddTable(Table table){
Tables.add(table);
Log.d("TablesCount", "tables count is "+Tables.size());
}
public Table getTable(String title){
for (int i=0;i<Tables.size();i++){
Log.d("Table name", Tables.get(i).getTitle());
if(title.equals(Tables.get(i).getTitle())){
return Tables.get(i);
}
}
return null;
}
public String getTableTitle(int i){
if(Tables.size()>i){
return Tables.get(i).getTitle();
}
return null;
}
public boolean hasTable(){
Log.d("hasTables", "TablesCount is "+Tables.size());
return Tables.size() != 0;
}
public int getTablesCount(){
return Tables.size();
}
}
Utillities.saveIdea code:
public static boolean SaveIdea(final Context context, final Idea idea){
final String FileName=String.valueOf(idea.getTitle()+".bin");
FileOutputStream fos;
ObjectOutputStream oos;
try {
fos = context.openFileOutput(FileName, Context.MODE_PRIVATE);
oos = new ObjectOutputStream(fos);
oos.writeObject(idea);
Log.d("in save idea", "in try");
oos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
Log.d("in save idea", "in catch");
Toast.makeText(context, "file wasn't saved, please check if you have enough storage space left on your device", Toast.LENGTH_SHORT).show();
return false;
}
return true;
}

TableView content not showing [duplicate]

This question already has answers here:
Javafx tableview not showing data in all columns
(3 answers)
Closed 4 years ago.
I have a table view which I have populated with information, however while I can select the rows which hold data, the text is not visible. Using table.getSelectionModel().getSelectedItem(); I can confirm that the table has indeed elements inside but the text does not show up.
I have a Socket communication in my programm where the server send the client an ArrayList of elements that the Client needs to display in the TableView.
Server side:
private void acceptedConnection(Socket client, Connection con){
try(ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream())){
System.out.println("receiving request");
String table = ois.readUTF();
System.out.println("Preparing statement");
PreparedStatement getTableContent = con.prepareStatement("SELECT * from " + table);
System.out.println("Fetching results");
ResultSet tableContentRs = getTableContent.executeQuery();
Builder builder = null;
switch (table){
case "blog_user": builder = new UserBuilder();break;
}
assert builder != null;
ArrayList tableContent = builder.buildArrayList(tableContentRs);
System.out.println("Sending results");
oos.writeObject(tableContent);
oos.flush();
System.out.println("Results sent");
}catch (IOException | SQLException e){
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client side:
public void loadTableContent(){
try (Socket client = new Socket(InetAddress.getLocalHost(), 667);
ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(client.getInputStream())) {
System.out.println("Sending request");
String selectedTable = databaseTableComboBox.getSelectionModel().getSelectedItem();
oos.writeUTF(selectedTable);
oos.flush();
table.getColumns().clear();
table.getItems().clear();
ArrayList data = null;
Builder builder = null;
switch (selectedTable){
case "blog_user": builder = new UserBuilder();data = (ArrayList<User>)ois.readObject();break;
}
assert builder != null;
builder.setColumns(table);
table.getItems().addAll(data);
table.refresh();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
The UserBuilder:
public class UserBuilder implements Builder {
#Override
public ArrayList buildArrayList(ResultSet set) throws SQLException {
ArrayList<User> users = new ArrayList<>();
while (set.next()) users.add(new User(set.getInt(1), set.getString(2), set.getString(3)));
return users;
}
#Override
public void setColumns(TableView table) {
TableColumn<User, String> idCol = new TableColumn<>("ID");
TableColumn<User, String> nameCol = new TableColumn<>("Name");
TableColumn<User, String> pwdCol = new TableColumn<>("Passwort");
idCol.setCellValueFactory(new PropertyValueFactory<>("id"));
nameCol.setCellValueFactory(new PropertyValueFactory<>("username"));
pwdCol.setCellValueFactory(new PropertyValueFactory<>("pwd"));
table.getColumns().addAll(idCol, nameCol, pwdCol);
}
}
And the User:
public class User implements Serializable{
private int id;
private String username;
private String pwd;
public User(int id, String username, String pwd) {
this.id = id;
this.username = username;
this.pwd = pwd;
}
#Override
public String toString() {
return id + " " + username + " " + pwd;
}
}
The interesting thing is that it already worked once but then I added the same mechanism I had for User and UserBuilder for other classes (so e.g. Admin and AdminBuilder) and suddenly the text did not show up in the TableView anymore.
Any and all help is greatly appreciated.
Your User class needs getter methods. The PropertyValueFactory needs to access these fields.
public class User {
private final int id;
private final String username;
private final String pwd;
public User(int id, String username, String pwd) {
this.id = id;
this.username = username;
this.pwd = pwd;
}
#Override
public String toString() {
return id + " " + username + " " + pwd;
}
public int getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPwd() {
return pwd;
}
}

Only last row is being inserted in the table in hibernate

I am working on a java project using hibernate. I have a csv file that contains more than 200 data. I've successfully retrieved data from csv file. Now I have to insert those data to the table.
The problem is only the last row is being added to the table. Other rows are not being inserted.
The schema of the table is given below:
INSERT INTO `attendence_table`
(`serial_no` int auto-increment,
`employee_id` varchar2,
`in_time` varchar2,
`out_time` varchar2,
`attend_date` date)
The Attendence class is given below:
#Entity
#Table(name = "attendence_table")
public class Attendence {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "serial_no")
private int id;
#Column(name = "employee_id")
private String employee_id;
#Column(name = "in_time")
private String inTime;
#Column(name = "out_time")
private String outTime;
#Column(name = "attend_date")
private String date;
public String getEmployee_id() {
return employee_id;
}
public void setEmployee_id(String employee_id) {
this.employee_id = employee_id;
}
public String getInTime() {
return inTime;
}
public void setInTime(String inTime) {
this.inTime = inTime;
}
public String getOutTime() {
return outTime;
}
public void setOutTime(String outTime) {
this.outTime = outTime;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
The insert function is given below:
private static SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
public static void hibernateInsertAttendenceSession(List<Attendence> collection) {
Session session = sessionFactory.openSession();
session.beginTransaction();
for (Attendence obj : collection) {
session.save(obj);
System.out.println("Object Added");
}
session.getTransaction().commit();
session.close();
}
For your convenience, I'm also adding the glimpse of the csv file:
Test_company,TestId001,Test Name,2018/03/22,08:53:15,17:50:40
Test_company,TestId001,Test Name,2018/03/25,08:51:02,17:55:18
Test_company,TestId001,Test Name,2018/03/27,08:50:16,18:03:47
Test_company,TestId001,Test Name,2018/03/28,08:48:07,18:46:42
Test_company,TestId001,Test Name,2018/03/29,08:56:16,20:14:16
Thanks in advance for giving your valuable time to help me with this issue.
You are saving the reference of the Attendence object, while you're modifying it's content everytime.
You should probably instantiate an Attendence Object every time you attempt saving it.
for (Attendence obj : collection) {
Attendence newRef = new Attendence(obj);
session.save(newRef);
System.out.println("Object Added");
}
Sorry, My issue was in different place. Thank you all for your help. While retrieving data from csv file, there was a little error which created the issue. Thank you all for your time :)
In the readfromcsv function, previously I did the following:
public static void readFromExcel(String path) {
ArrayList<Attendence> attendences = new ArrayList<Attendence>();
String csvFile = path;
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
Attendence attendenceLine=new Attendence();
try {
br = new BufferedReader(new FileReader(csvFile));
//Attendence attendenceLine = new Attendence();
line = br.readLine();
while ((line = br.readLine()) != null) {
String[] data = line.split(cvsSplitBy);
if (data.length == 6) {
attendenceLine.setEmployee_id(data[1]);
attendenceLine.setDate(data[3]);
attendenceLine.setInTime(data[4]);
attendenceLine.setOutTime(data[5]);
}
else{
attendenceLine.setEmployee_id(data[1]);
attendenceLine.setDate(data[3]);
attendenceLine.setInTime("no punch");
attendenceLine.setOutTime("no punch");
}
attendences.add(attendenceLine);
}
for(Attendence attendence: attendences){
HibernateOperation.hibernateInsertOneAttendenceSession(attendence);
}
//HibernateOperation.hibernateInsertAttendenceSession(attendences);
} catch (FileNotFoundException ex) {
Logger.getLogger(AddToDatabaseOperation.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(AddToDatabaseOperation.class.getName()).log(Level.SEVERE, null, ex);
}
}
Here the attendenceLine String Variable was only having the last row as a reference value. Thats why for every iteration, I need to create the object again. I did the following to solve the issue.
public static void readFromExcel(String path) {
ArrayList<Attendence> attendences = new ArrayList<Attendence>();
String csvFile = path;
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
try {
br = new BufferedReader(new FileReader(csvFile));
//Attendence attendenceLine = new Attendence();
line = br.readLine();
while ((line = br.readLine()) != null) {
String[] data = line.split(cvsSplitBy);
Attendence attendenceLine=new Attendence();
if (data.length == 6) {
attendenceLine.setEmployee_id(data[1]);
attendenceLine.setDate(data[3]);
attendenceLine.setInTime(data[4]);
attendenceLine.setOutTime(data[5]);
}
else{
attendenceLine.setEmployee_id(data[1]);
attendenceLine.setDate(data[3]);
attendenceLine.setInTime("no punch");
attendenceLine.setOutTime("no punch");
}
attendences.add(attendenceLine);
}
for(Attendence attendence: attendences){
HibernateOperation.hibernateInsertOneAttendenceSession(attendence);
}
//HibernateOperation.hibernateInsertAttendenceSession(attendences);
} catch (FileNotFoundException ex) {
Logger.getLogger(AddToDatabaseOperation.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(AddToDatabaseOperation.class.getName()).log(Level.SEVERE, null, ex);
}
}
Please call new class and add all filed to this new class and save new class.
It will work.
for (Attendence obj : collection) {
Attendence newRef = new Attendence();
newRef.setSerialNo(obj.getSerialNo())
// set newRef to obj of all column......
session.save(newRef);
System.out.println("Object Added");
}

Passing values in runnable class constructor in Java

I hope somebody could please help me out in identifying to where I am going wrong with the codes below.
public class CCFileImpl implements CCFileAbs {
private LogMe logMe = null;
private ExecutorService ccfileimpl_exsc = null;
private CCProcessorImpl cProc = null;
private DataUtil dUtil = null;
public CCFileImpl() {
this.logMe = LogMe.getLogger();
if (dUtil == null) {
dUtil = new DataUtil();
}
}
#Override
public void getFilesForProcess() {
CCHeader cHead = null;
Future future = null;
String sPath = PropReader.getPropValue(PropReader.FILEDIR); //D:\samples\
int iCtr = 0;
ccfileimpl_exsc = Executors.newFixedThreadPool(Integer.parseInt(PropReader.getPropValue(PropReader.TPool_File)));
Date dToday = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Iterator iter = dUtil.getFilesForProcess(sdf.format(dToday)).iterator();
String sFileGroupName = "", sFileName = "";
String sId = null; //"testFiles";
while (iter.hasNext()) {
cHead = (CCHeader) iter.next();
sFileName = cHead.getsFileName(); //(String) iter.next();
sId = cHead.getsId();
sFileGroupName = sFileName + "_" + iCtr++;
dUtil.updTPDHDRStatusById(sId); //Interface utility class // <=== And also here, when trying to update the db
// nothing happened.
cProc = new CCProcessorImpl(sId, sFileGroupName, sPath, sFileName); // <=== Problem is here?
future = ccfileimpl_exsc.submit(cProc);
}
ccfileimpl_exsc.shutdown();
}
}
The above code retrieves the files for processing then assigning it to a runnable class (below) then submitting it to an executorService class.
Now i can't understand to why the passed values of the constructor (below) is set to null/space and only the sPath variable has a definite value.
public class CCProcessorImpl implements Runnable{
private CCFileParser rpsCCParser;
private ExecutorService ccprocimpl_exsc;
private static LogMe logMe;
private final String sGroupName;
private final String sId;
private final String sFileName;
#Override
public void run() {
this.parseFiles(sId, sFileName);
}
public CCProcessorImpl(String sId, String sGroupName, String sPath, String sFileName) {
this.logMe = LogMe.getLogger();
this.sId = sId;
this.sGroupName = sGroupName;
this.sFileName = sPath + sFileName;
}
public void parseFiles(String sId, String sFileName) {
try {
Future future = null;
rpsCCParser = new CCFileParser(sId, sFileName);
ArrayList aList = rpsCCParser.getFileContent();
String sGroupName = sId + "_";
ccprocimpl_exsc = Executors.newFixedThreadPool(Integer.parseInt(PropReader.getPropValue(PropReader.TPool_Content)));
int iStart = 0, iSize = 9, iEnd = iSize;
for (int iCtr = 0; iCtr <= ((aList.size() / 10) - 1); iCtr++, iStart += iSize, iEnd += iSize) {
future = ccprocimpl_exsc.submit(new CCUpdater(aList.subList(iStart, iEnd), sGroupName + iCtr));
}
future.get();
ccprocimpl_exsc.shutdown();
} catch (ExecutionException e) {
throw new RuntimeException(e);
} catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
}
}
Also as a supplementary question, why is it when i tried to update the db table no updates were performed? Would this be related to being in a thread environment?
Why don't you use the futures returned by ccfileimpl_exsc.submit()?
Instead, you call ccfileimpl_exsc.shutdown() right after submitting the jobs, killing them before they finish.

Categories

Resources