Skip to content

Instantly share code, notes, and snippets.

@CedricL46
Last active August 4, 2022 11:27
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save CedricL46/c581bd0563ea24c437e97ffb787fd43f to your computer and use it in GitHub Desktop.
//Oracle ADF ActionEvent to duplicate a row
public void duplicateRowLine(ActionEvent e) {
ViewObject vo = this.getViewObjectFromIterator("YOUR_ITERATOR_NAME");
Row currentRow = vo.getCurrentRow();
Row duplicatedRow = vo.createRow();
AttributeDef[] voKeyAttributes = vo.getKeyAttributeDefs(); //List of the primary keys that need to be unique
String[] currentRowAttributes = currentRow.getAttributeNames();
for (String attributeName : currentRowAttributes) {
int attributeIndex = duplicatedRow.getAttributeIndexOf(attributeName);
//Check if the attribute is updatable
if (duplicatedRow.isAttributeUpdateable(attributeIndex)){
//Check if the attribute is a Primary Key
if (!this.attributeIsPrimaryKey(voKeyAttributes, attributeName)) {
//If it's not, copy the attribute value to the duplicated row
duplicatedRow.setAttribute(attributeName, currentRow.getAttribute(attributeName));
}
//Mandatory: Add an else here to set the primary key with a unique value in java
}
}
//Add the duplicated row to your View Object
vo.insertRow(duplicatedRow);
//Optionnal : Commit the VO to add row in database
//Optionnal : Refresh the table AdfFacesContext.getCurrentInstance().addPartialTarget("YOUR_TABLE_ID");
}
private boolean attributeIsPrimaryKey(AttributeDef[] voKeyAttributes, String currentAttributeName) {
for (AttributeDef voKeyAttribute : voKeyAttributes) {
if (voKeyAttribute.isPrimaryKey() && currentAttributeName.equals(voKeyAttribute.getName())) {
//If one of the primaryKey attribute is the current attribute
return true;
}
}
return false;
}
/*** This function is usually a public available in your ADFUtils package and should be reusable
Get the full library here : https://github.com/CedricL46/Oracle-ADF-and-JSF-Utils-libraries ***/
private static ViewObjectImpl getViewObjectFromIterator(String nomIterator) {
_Logger.log(Level.INFO, "" + nomIterator);
ViewObjectImpl returnVO = null;
DCBindingContainer dcb = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
if (dcb != null) {
DCIteratorBinding iter = dcb.findIteratorBinding(nomIterator);
if (iter != null) {
returnVO = (ViewObjectImpl)iter.getViewObject();
}
}
return returnVO;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment