Index: lams_build/lib/lams/lams.jar =================================================================== diff -u -r338e82cbe9bdfe1b5fbc1e2b73e91dcfadb68ec6 -r301bc5704aa25346bc457867daeb7cd8854cb452 Binary files differ Index: lams_common/src/java/org/lamsfoundation/lams/learningdesign/License.java =================================================================== diff -u -r338e82cbe9bdfe1b5fbc1e2b73e91dcfadb68ec6 -r301bc5704aa25346bc457867daeb7cd8854cb452 --- lams_common/src/java/org/lamsfoundation/lams/learningdesign/License.java (.../License.java) (revision 338e82cbe9bdfe1b5fbc1e2b73e91dcfadb68ec6) +++ lams_common/src/java/org/lamsfoundation/lams/learningdesign/License.java (.../License.java) (revision 301bc5704aa25346bc457867daeb7cd8854cb452) @@ -24,6 +24,7 @@ import java.io.Serializable; +import org.apache.commons.lang.StringUtils; import org.lamsfoundation.lams.learningdesign.dto.LicenseDTO; /** @@ -110,4 +111,27 @@ public void setCode(String code) { this.code = code; } + + /** Is this license and another license the same license? The licenseID and name fields + * are checked, with the name fields having any leading or trailing spaces stripped + * before comparison. If both licenseID fields are null, then they are assumed to be + * different licenses. + * + * This is to be used when importing designs, to see if licenses match. If the id and + * name fields match, then the imported design is linked to the license record. If + * they don't match, the design should be attached to the "Other" license. + * + * The user selects the license based on the name, hence we have chosen to check the name. + * + * @param otherLicense + * @return true if they are the same type of license + */ + public boolean isSameLicenseType(License otherLicense) { + if ( licenseID != null && licenseID.equals(otherLicense.getLicenseID()) ) { + String name1 = ( name != null ? StringUtils.strip(name) : ""); + String name2 = ( otherLicense.getName() != null ? StringUtils.strip(otherLicense.getName()) : ""); + return name1.equals(name2); + } + return false; + } } Index: lams_common/test/java/org/lamsfoundation/lams/learningdesign/TestLicense.java =================================================================== diff -u --- lams_common/test/java/org/lamsfoundation/lams/learningdesign/TestLicense.java (revision 0) +++ lams_common/test/java/org/lamsfoundation/lams/learningdesign/TestLicense.java (revision 301bc5704aa25346bc457867daeb7cd8854cb452) @@ -0,0 +1,72 @@ +package org.lamsfoundation.lams.learningdesign; + +import org.lamsfoundation.lams.learningdesign.dao.ILicenseDAO; +import org.lamsfoundation.lams.test.AbstractCommonTestCase; + +public class TestLicense extends AbstractCommonTestCase { + + protected ILicenseDAO licenseDAO; + + private static final Long DEFAULT_BYNCSA_LICENSE_ID=new Long(1); + private static final String BYNCSA_LICENSE_CODE="by-nc-sa"; + private static final String BYNCSA_LICENSE_NAME="Attribution-Noncommercial-ShareAlike 2.5"; + private static final String BYNCSA_URL ="http://creativecommons.org/licenses/by-nc-sa/2.5/"; + private static final String BYNCSA_PICTURE_URL ="/images/license/byncsa.jpg"; + + protected void setUp() throws Exception + { + super.setUp(); + licenseDAO =(ILicenseDAO) context.getBean("licenseDAO"); + + } + + protected void tearDown() throws Exception + { + super.tearDown(); + } + + /** + * Constructor for TestActivity. + * @param arg0 + */ + public TestLicense(String arg0) + { + super(arg0); + } + + public void testRetrieveLicense() + { + License byncsa = licenseDAO.getLicenseByID(DEFAULT_BYNCSA_LICENSE_ID); + assertNotNull("Found license with test id", byncsa); + assertEquals(BYNCSA_LICENSE_CODE, byncsa.getCode()); + assertEquals(BYNCSA_LICENSE_NAME, byncsa.getName()); + assertTrue("Found license is the default license", byncsa.getDefaultLicense().booleanValue()); + assertEquals(BYNCSA_URL, byncsa.getUrl()); + assertEquals(BYNCSA_PICTURE_URL, byncsa.getPictureURL()); + } + + public void testIsSameLicenseType() + { + License byncsa = licenseDAO.getLicenseByID(DEFAULT_BYNCSA_LICENSE_ID); + + // expect this to match exactly. Only the id and name matter, and the name should have leading and trailing spaces ignored + License matchingLicense = new License(DEFAULT_BYNCSA_LICENSE_ID,BYNCSA_LICENSE_NAME,null, null, Boolean.FALSE, null); + assertTrue("Manually created license matches as expected",matchingLicense.isSameLicenseType(byncsa)); + matchingLicense.setName(" "+BYNCSA_LICENSE_NAME+" "); + + // Should fail as matching license has different name id + License diffNameLicense = new License(DEFAULT_BYNCSA_LICENSE_ID,null,null, null, Boolean.FALSE, null); + assertFalse("Test License with no name does not match as expected",diffNameLicense.isSameLicenseType(matchingLicense)); + diffNameLicense.setName("Attribution-Noncommercial-ShareAlike"); + assertFalse("Test License with wrong name does not match as expected",diffNameLicense.isSameLicenseType(matchingLicense)); + + // Should fail as matching license has null id + License nullIDLicense = new License(null,BYNCSA_LICENSE_NAME,null, null, Boolean.FALSE, null); + assertFalse("Test License with null id does not match as expected",nullIDLicense.isSameLicenseType(matchingLicense)); + + // Should fail as both licenses have a null id + License nullIDLicense2 = new License(null,BYNCSA_LICENSE_NAME,null, null, Boolean.FALSE, null); + assertFalse("Licenses with null ids do not match as expected",nullIDLicense2.isSameLicenseType(nullIDLicense)); +} + +}