Hi,
How to get multi selected entity values in a table component one by one and insert these values into another database table? I want to iterate all selected rows and get the value of it. After this, I would add these values into multi entities then insert into table. Could you share the full example code? I’m new to Java and Cuba. I have many questions. Thank you very much.
Briefly, you need to 1) get selected rows (Table’s getSelected() method), 2) create a set of entities that duplicates the selected rows (metadata.create()), 3) save the set of new entites in DB (dataManager.commit()).
Now let’s see these steps in details.
For example, you want to save the selected Customer entity instances to another database table. To do this, you should create a new entity that duplicates Customer but is bound to another table in DB. By default, the entity inheritance strategy is SINGLE_TABLE, so you should change it to TABLE_PER_CLASS:
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@NamePattern("%s|name")
@Table(name = "SALES_CUSTOMER")
@Entity(name = "sales$Customer")
public class Customer extends StandardEntity {
private static final long serialVersionUID = -6018518150505269719L;
@Column(name = "NAME", nullable = false, length = 100)
protected String name;
@Column(name = "EMAIL", length = 100)
protected String email;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
}
The new CopiedCustomer entity extends Customer to avoid duplicating the code:
@Table(name = "SALES_COPIED_CUSTOMER")
@Entity(name = "sales$CopiedCustomer")
public class CopiedCustomer extends Customer {
private static final long serialVersionUID = 6675598050120952818L;
}
In the customer-browse XML descriptor let’s create a button that invokes the copySelectedItemsToDb() method:
<button id="copySelectedBtn"
caption="Copy selected to DB"
invoke="copySelectedItemsToDb"/>
In the CustomerBrowse Java controller we create this method and implement the logic I described above:
public class CustomerBrowse extends AbstractLookup {
@Inject
private GroupTable<Customer> customersTable;
@Inject
private Metadata metadata;
@Inject
private DataManager dataManager;
public void copySelectedItemsToDb() {
if (customersTable.getSelected() != null) {
Set<CopiedCustomer> customersToCopy = new HashSet<>();
for (Customer customer : customersTable.getSelected()) {
CopiedCustomer copiedCustomer = metadata.create(CopiedCustomer.class);
copiedCustomer.setName(customer.getName());
copiedCustomer.setEmail(customer.getEmail());
customersToCopy.add(copiedCustomer);
}
CommitContext context = new CommitContext(customersToCopy);
dataManager.commit(context);
showNotification(customersToCopy.size() + " customers have been copied to DB");
}
}
}
It is OK if the ccsWoesDs is populated with the items you need.
To track the rows selected in the table, you need the table.getSelected() method anyway.
P.S. if (JdeYN.equals("N")) is enough, == true is redundant here.