Skip to content

Commit

Permalink
LA-36: Ignoring hidden n2n attributes. Updating n2n collections instead
Browse files Browse the repository at this point in the history
of setting new one to enable support for orphanRemoval=true. (#36)
  • Loading branch information
aliubunia committed Feb 16, 2013
1 parent 2481c58 commit dd430fb
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications;
import org.springframework.data.rest.repository.AttributeMetadata;
import org.springframework.data.rest.repository.RepositoryConstraintViolationException;
import org.springframework.data.rest.webmvc.PagingAndSorting;
import org.springframework.hateoas.Link;
Expand All @@ -40,6 +41,7 @@
import java.io.IOException;
import java.io.Serializable;
import java.net.URI;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -75,6 +77,24 @@ public ResponseEntity<?> createOrUpdate(ServletServerHttpRequest request, URI ba
return super.createOrUpdate( request, baseUri, repository, "" );
}

@Override
@SuppressWarnings("rawtypes")
protected void attrMetaSet(AttributeMetadata attrMeta, Object incomingVal, Object entity) {
if (attrMeta.isCollectionLike() || attrMeta.isSetLike()) {
// Trying to avoid collection-was-no-longer-referenced issue
// if the collection is modifiable
try {
Collection col = (Collection) attrMeta.get(entity);
col.clear();
col.addAll((Collection) incomingVal);
} catch (UnsupportedOperationException e) {
attrMeta.set(incomingVal, entity);
}
} else {
attrMeta.set(incomingVal, entity);
}
}

@ResponseBody
@RequestMapping( value = "/{repositoryName}/{id}/unit/{configurationUnit}", method = RequestMethod.GET )
public ResponseEntity<?> entity(ServletServerHttpRequest request, URI baseUri, @PathVariable String repositoryName, @PathVariable String id, @PathVariable String configurationUnit) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ public ResponseEntity<?> createOrUpdate(ServletServerHttpRequest request,
.values()) {
Object incomingVal;
if(null != (incomingVal = attrMeta.get(incoming))) {
attrMeta.set(incomingVal, entity);
attrMetaSet(attrMeta, incomingVal, entity);
}
}

Expand All @@ -838,7 +838,7 @@ public ResponseEntity<?> createOrUpdate(ServletServerHttpRequest request,
.values()) {
Object incomingVal;
if(null != (incomingVal = attrMeta.get(incoming))) {
attrMeta.set(incomingVal, entity);
attrMetaSet(attrMeta, incomingVal, entity);
}
}
} else {
Expand Down Expand Up @@ -873,6 +873,10 @@ public ResponseEntity<?> createOrUpdate(ServletServerHttpRequest request,

}

protected void attrMetaSet(AttributeMetadata attrMeta, Object incomingVal, Object entity) {
attrMeta.set(incomingVal, entity);
}

/**
* Delete an entity.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.lightadmin.core.view.tags.form;

import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.lightadmin.core.persistence.metamodel.DomainTypeAttributeType;
Expand All @@ -9,6 +13,7 @@

import org.lightadmin.core.config.domain.DomainTypeBasicConfiguration;
import org.lightadmin.core.config.domain.GlobalAdministrationConfiguration;
import org.lightadmin.core.config.domain.field.FieldMetadata;
import org.lightadmin.core.persistence.metamodel.DomainTypeAttributeMetadata;
import org.lightadmin.core.persistence.metamodel.DomainTypeEntityMetadata;
import org.lightadmin.core.rest.DomainTypeResourceSupport;
Expand All @@ -30,12 +35,17 @@ public class DomainTypeMetadataJsonTag extends AbstractAutowiredTag {

private DomainTypeEntityMetadata<DomainTypeAttributeMetadata> domainTypeMetadata;

private Set<String> includedAttributes;

@Override
public void doTag() throws IOException {
JsonGenerator json = JSON_FACTORY.createJsonGenerator(getJspContext().getOut());
json.writeStartObject();
try {
for (DomainTypeAttributeMetadata attribMetadata : domainTypeMetadata.getAttributes()) {
if (includedAttributes != null && !includedAttributes.contains(attribMetadata.getName())) {
continue;
}
try {
json.writeObjectFieldStart(attribMetadata.getName());
json.writeStringField("type", DomainTypeAttributeType.by( attribMetadata ).name());
Expand Down Expand Up @@ -67,4 +77,11 @@ public void setDomainTypeMetadata(DomainTypeEntityMetadata<DomainTypeAttributeMe
this.domainTypeMetadata = domainTypeMetadata;
}

public void setIncludeFields(Collection<FieldMetadata> fields) {
includedAttributes = new HashSet<String>();
for (FieldMetadata field : fields) {
includedAttributes.add(field.getUuid());
}
}

}
6 changes: 6 additions & 0 deletions lightadmin-core/src/main/resources/META-INF/lightadmin.tld
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@
<rtexprvalue>true</rtexprvalue>
<type>org.lightadmin.core.persistence.metamodel.DomainTypeAttributeMetadata</type>
</attribute>
<attribute>
<name>includeFields</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<type>java.util.Collection</type>
</attribute>
</tag>

<tag>
Expand Down
5 changes: 3 additions & 2 deletions lightadmin-demo/src/main/webapp/WEB-INF/views/pages/edit.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<c:set var="domainTypeName" value="${domainTypeAdministrationConfiguration.domainTypeName}"/>
<spring:url var="domainBaseUrl" value="${light:domainBaseUrl(domainTypeName)}" scope="page"/>
<c:set var="domainTypeEntityMetadata" value="${domainTypeAdministrationConfiguration.domainTypeEntityMetadata}"/>
<c:set var="formViewFields" value="${domainTypeAdministrationConfiguration.formViewFragment.fields}"/>
<jsp:useBean id="domainTypeEntityMetadata" type="org.lightadmin.core.persistence.metamodel.DomainTypeEntityMetadata"/>
<jsp:useBean id="entity" type="java.lang.Object" scope="request"/>
<c:set var="entityId" value="<%= domainTypeEntityMetadata.getIdAttribute().getValue( entity ) %>"/>
Expand All @@ -30,7 +31,7 @@
<div class="widget">
<div class="head"><h5 class="iCreate"><c:out value="${light:capitalize(domainTypeName)}"/> #<c:out value="${entityId}"/></h5></div>
<fieldset>
<c:forEach var="fieldEntry" items="${domainTypeAdministrationConfiguration.formViewFragment.fields}" varStatus="status">
<c:forEach var="fieldEntry" items="${formViewFields}" varStatus="status">
<c:if test="${!fieldEntry.primaryKey}">
<div id="${fieldEntry.uuid}-control-group" class="rowElem ${status.first ? 'noborder' : ''}">
<label><c:out value="${light:capitalize(fieldEntry.name)}"/>:</label>
Expand All @@ -55,7 +56,7 @@
$("select, input:checkbox, input:radio, input:file").uniform();
DOMAIN_TYPE_METADATA = <light:domain-type-metadata-json domainTypeMetadata="${domainTypeEntityMetadata}"/>;
DOMAIN_TYPE_METADATA = <light:domain-type-metadata-json domainTypeMetadata="${domainTypeEntityMetadata}" includeFields="${formViewFields}"/>;
loadDomainObjectForFormView($('#editForm'), '${domainObjectUrl}');
});
Expand Down
58 changes: 31 additions & 27 deletions lightadmin-demo/src/main/webapp/scripts/lightadmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ function dataTableRESTAdapter( sSource, aoData, fnCallback ) {
restParams.push( { "name": sortName + ".dir", "value": sortDir } );

jQuery.ajax( {
"dataType": 'json',
"type": "GET",
"url": sSource,
"data": restParams,
"success": function ( data ) {
data.iTotalRecords = data.page.totalElements;
data.iTotalDisplayRecords = data.page.totalElements;

getSearcher().onSearchCompleted();

fnCallback( data );
}
} );
"dataType": 'json',
"type": "GET",
"url": sSource,
"data": restParams,
"success": function ( data ) {
data.iTotalRecords = data.page.totalElements;
data.iTotalDisplayRecords = data.page.totalElements;

getSearcher().onSearchCompleted();

fnCallback( data );
}
} );
}

function getPrimaryKey( dataValue ) {
Expand Down Expand Up @@ -154,19 +154,19 @@ function bindInfoClickHandlers( tableElement, dataTable ) {
var aData = dataTable.fnGetData( nTr );
var restEntityUrl = aData.links[0].href;
jQuery.ajax( {
"dataType": 'json',
"type": "GET",
"url": restEntityUrl + '/unit/quickView',
"success": function ( data ) {
var nDetailsRow = dataTable.fnOpen( nTr, quickLook( data ), 'details' );
$( nDetailsRow ).addClass( $( nDetailsRow ).prev().attr( 'class' ) );
$( 'div.innerDetails', nDetailsRow ).hide();
$( 'div.innerDetails', nDetailsRow ).slideDown( 'slow', function () {
infoImg.attr( 'src', "../images/aInactive.png" );
infoImg.attr( 'title', "Click to close Quick View" );
} );
}
} );
"dataType": 'json',
"type": "GET",
"url": restEntityUrl + '/unit/quickView',
"success": function ( data ) {
var nDetailsRow = dataTable.fnOpen( nTr, quickLook( data ), 'details' );
$( nDetailsRow ).addClass( $( nDetailsRow ).prev().attr( 'class' ) );
$( 'div.innerDetails', nDetailsRow ).hide();
$( 'div.innerDetails', nDetailsRow ).slideDown( 'slow', function () {
infoImg.attr( 'src', "../images/aInactive.png" );
infoImg.attr( 'title', "Click to close Quick View" );
} );
}
} );
}
} );
}
Expand Down Expand Up @@ -323,6 +323,10 @@ function updateDomainObject( domForm ) {
if ( errorMessages.length > 0 ) {
showFailureMessageNote( errorMessages );
}
},
409: function ( jqXHR ) {
var data = $.parseJSON( jqXHR.responseText );
showFailureMessageNote( data.message );
}
}
} );
Expand Down Expand Up @@ -352,4 +356,4 @@ function showMessageNote( message, messageTypeClass ) {
} );
} );
} );
}
}

0 comments on commit dd430fb

Please sign in to comment.