Author: bleny
Date: 2011-02-16 10:52:59 +0000 (Wed, 16 Feb 2011)
New Revision: 1016
Log:
remove some contact state motifs UI parts ; manage merge in import terrestrial locations
Modified:
trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/Import.java
trunk/wao-business/src/main/java/fr/ifremer/wao/service/ServiceReferentialImpl.java
trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/pages/Administration.java
trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/pages/Contacts.java
trunk/wao-ui/src/main/resources/i18n/wao-ui_en_GB.properties
trunk/wao-ui/src/main/resources/i18n/wao-ui_fr_FR.properties
trunk/wao-ui/src/main/webapp/Administration.tml
trunk/wao-ui/src/main/webapp/Contacts.tml
Modified: trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/Import.java
===================================================================
--- trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/Import.java 2011-02-15 17:30:54 UTC (rev 1015)
+++ trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/Import.java 2011-02-16 10:52:59 UTC (rev 1016)
@@ -56,9 +56,6 @@
nonIgnoredHeaders.add(field);
}
}
- if (log.isTraceEnabled()) {
- log.trace("all non-ignored headers are " + nonIgnoredHeaders);
- }
return nonIgnoredHeaders;
}
@@ -69,9 +66,6 @@
allMandatoryHeaders.add(field);
}
}
- if (log.isTraceEnabled()) {
- log.trace("all mandatory headers are " + allMandatoryHeaders);
- }
return allMandatoryHeaders;
}
@@ -80,6 +74,11 @@
validateModel();
this.reader = new CsvReader(inputStream, ';', Charset.forName("UTF-8"));
reader.setTrimWhitespace(true);
+ if (log.isTraceEnabled()) {
+ log.trace("all headers are " + model.getAllColumns());
+ log.trace("all non-ignored headers are " + getNonIgnoredHeaders());
+ log.trace("all mandatory headers are " + getAllMandatoryHeaders());
+ }
}
protected boolean validateModel() {
Modified: trunk/wao-business/src/main/java/fr/ifremer/wao/service/ServiceReferentialImpl.java
===================================================================
--- trunk/wao-business/src/main/java/fr/ifremer/wao/service/ServiceReferentialImpl.java 2011-02-15 17:30:54 UTC (rev 1015)
+++ trunk/wao-business/src/main/java/fr/ifremer/wao/service/ServiceReferentialImpl.java 2011-02-16 10:52:59 UTC (rev 1016)
@@ -278,75 +278,123 @@
@Override
protected void executeImportTerrestrialLocations(TopiaContext transaction, InputStream input) throws Exception {
+
TerrestrialLocationDAO dao = WaoDAOHelper.getTerrestrialLocationDAO(transaction);
- int count = dao.count();
+ // first, we will read the CSV file, line by line
+ // at, each line, we will look if the location is already in database
+ // if yes, some attribute may have change, so we use a binder to copy all field
+ // if no, add the new location in database
- if (count == 0) {
+ ImportModel<TerrestrialLocation> model = new TerrestrialLocationImportModel();
+ Import<TerrestrialLocation> terrestrialLocationImport = new Import<TerrestrialLocation>(model, input);
- ImportModel<TerrestrialLocation> model = new TerrestrialLocationImportModel();
- Import<TerrestrialLocation> terrestrialLocationImport = new Import<TerrestrialLocation>(model, input);
+ // we will need a binder, to copy location for an update
+ Binder<TerrestrialLocation, TerrestrialLocation> locationBinder =
+ BinderFactory.newBinder(TerrestrialLocation.class);
+ // some counts for logging purpose
+ int locationAdded = 0, locationUpdated = 0;
- Iterator<TerrestrialLocation> locationIterator = terrestrialLocationImport.startImport();
- while (locationIterator.hasNext()) {
- TerrestrialLocation location = locationIterator.next();
+ // for each line of the CSV
+ Iterator<TerrestrialLocation> locationIterator = terrestrialLocationImport.startImport();
+ while (locationIterator.hasNext()) {
+ // location is a line of the CSV
+ TerrestrialLocation location = locationIterator.next();
+
+ // look if already exists and update existing or add new
+ TerrestrialLocation existingLocation = dao.findByProperties(TerrestrialLocation.PROPERTY_CODE, location.getCode(),
+ TerrestrialLocation.PROPERTY_NAME, location.getName(),
+ TerrestrialLocation.PROPERTY_LOCATION_TYPE_ORDINAL, location.getLocationTypeOrdinal());
+ if (existingLocation == null) {
dao.create(location);
+ locationAdded += 1;
+ } else {
+ locationBinder.copyExcluding(location, existingLocation, TerrestrialLocation.TOPIA_ID,
+ TerrestrialLocation.TOPIA_CREATE_DATE, TerrestrialLocation.TOPIA_VERSION);
+ dao.update(existingLocation);
+ locationUpdated += 1;
}
+ }
- if (log.isInfoEnabled()) {
- count = dao.count();
- log.info(count + " terrestrial locations imported");
- }
+ if (log.isInfoEnabled()) {
+ log.info(locationAdded + " terrestrial locations added, " + locationUpdated + " updated");
+ }
- // add districts
- WaoQueryHelper.TerrestrialLocationProperty terrestrialLocationProperty =
- WaoQueryHelper.newTerrestrialLocationProperty();
- TopiaQuery query = dao.createQuery(terrestrialLocationProperty.$alias());
- query.addDistinct()
- .setSelect(terrestrialLocationProperty.districtCode());
- List<String> districtCodes = transaction.findByQuery(query);
- for (String districtCode : districtCodes) {
- TerrestrialLocation location = dao.findByDistrictCode(districtCode);
- TerrestrialLocation district = new TerrestrialLocationImpl();
+ // now, all the CSV file has been read and dumped into database,
+ // Now, we need to update database by adding any missing district
+ // Each location has a district (identified by a district code)
+ // We must find all the distinct district ids and check that
+ // we have an entity representing it. In database a disctrict can
+ // be distinguished from a location by having it's
+ // locationTypeOrdinal null.
+ WaoQueryHelper.TerrestrialLocationProperty terrestrialLocationProperty =
+ WaoQueryHelper.newTerrestrialLocationProperty();
- Binder<TerrestrialLocation, TerrestrialLocation> locationBinder =
- BinderFactory.newBinder(TerrestrialLocation.class);
- locationBinder.copyExcluding(location, district, TerrestrialLocation.PROPERTY_NAME,
- TerrestrialLocation.PROPERTY_CODE, TerrestrialLocation.PROPERTY_PORT_CODE,
- TerrestrialLocation.PROPERTY_PORT_NAME, TerrestrialLocation.PROPERTY_LOCATION_TYPE_ORDINAL,
- TerrestrialLocation.TOPIA_ID, TerrestrialLocation.TOPIA_CREATE_DATE,
- TerrestrialLocation.TOPIA_VERSION);
- district.setLocationType(null);
+ // let's find all distinct district codes.
+ TopiaQuery query = dao.createQuery(terrestrialLocationProperty.$alias());
+ query.addDistinct()
+ .setSelect(terrestrialLocationProperty.districtCode())
+ .addNotNull(terrestrialLocationProperty.locationTypeOrdinal());
+ List<String> districtCodes = transaction.findByQuery(query);
- if (StringUtils.isEmpty(district.getDistrictCode())) {
- // distinct will select a random place with no district code
- // it can be in England, Sweden or whatever. It's bad for us
- // because we don't want some data not to be filtered when looking
- // for England while we want to filter on all country except France
- district.setCountryCode(null);
- district.setCountryName("Hors France");
- district.setSeaboardCode(null);
- district.setSeaboardName(null);
- district.setCoastFAOCode(null);
- district.setCoastFAOName(null);
- }
+ // a binder, needed for update
+ Binder<TerrestrialLocation, TerrestrialLocation> districtBinder =
+ BinderFactory.newBinder(TerrestrialLocation.class);
+ // counts for logging
+ int districtAdded = 0, districtUpdated = 0;
- dao.create(district);
- }
+ for (String districtCode : districtCodes) { // for each district code found
- if (log.isInfoEnabled()) {
- count = dao.count() - count;
- log.info(count + " terrestrial district imported");
- }
+ // let's find a location which is in this particular district
+ TerrestrialLocation location = dao.findByDistrictCode(districtCode);
- transaction.commitTransaction();
+ // create the district by copying region, country etc. from location
+ TerrestrialLocation district = new TerrestrialLocationImpl();
+ districtBinder.copyExcluding(location, district, TerrestrialLocation.PROPERTY_NAME,
+ TerrestrialLocation.PROPERTY_CODE, TerrestrialLocation.PROPERTY_PORT_CODE,
+ TerrestrialLocation.PROPERTY_PORT_NAME, TerrestrialLocation.PROPERTY_LOCATION_TYPE_ORDINAL,
+ TerrestrialLocation.TOPIA_ID, TerrestrialLocation.TOPIA_CREATE_DATE,
+ TerrestrialLocation.TOPIA_VERSION);
+ district.setLocationType(null);
- } else {
- if (log.isInfoEnabled()) {
- log.info("database already contains " + count + " terrestrial locations, not importing");
+ // A particular case, some location (not in France) has no
+ // district (district code null), so we must add a district
+ // for them
+ if (StringUtils.isEmpty(district.getDistrictCode())) {
+ // distinct will select a random place with no district code
+ // it can be in England, Sweden or whatever. It's bad for us
+ // because we don't want some data not to be filtered when looking
+ // for England while we want to filter on all country except France
+ district.setCountryCode(null);
+ district.setCountryName("Hors France");
+ district.setSeaboardCode(null);
+ district.setSeaboardName(null);
+ district.setCoastFAOCode(null);
+ district.setCoastFAOName(null);
}
+
+ // now, we have the 'district' object we want to have in the
+ // database. Now let's add it if not already in or update
+ // it already in
+ TerrestrialLocation existingDistrict = dao.findByProperties(TerrestrialLocation.PROPERTY_DISTRICT_CODE, district.getDistrictCode(),
+ TerrestrialLocation.PROPERTY_LOCATION_TYPE_ORDINAL, null);
+ if (existingDistrict == null) {
+ dao.create(district);
+ districtAdded += 1;
+ } else {
+ locationBinder.copyExcluding(district, existingDistrict, TerrestrialLocation.TOPIA_ID,
+ TerrestrialLocation.TOPIA_CREATE_DATE, TerrestrialLocation.TOPIA_VERSION);
+ dao.update(existingDistrict);
+ districtUpdated += 1;
+ }
}
+ if (log.isInfoEnabled()) {
+ log.info(districtAdded + " terrestrial district added, " + districtUpdated + " updated");
+ }
+
+ transaction.commitTransaction();
+
}
@Override
Modified: trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/pages/Administration.java
===================================================================
--- trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/pages/Administration.java 2011-02-15 17:30:54 UTC (rev 1015)
+++ trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/pages/Administration.java 2011-02-16 10:52:59 UTC (rev 1016)
@@ -246,6 +246,18 @@
};
}
+ public ImportEngine getTerrestrialLocationsImportEngine() {
+ return new ImportEngine() {
+
+ @Override
+ public ImportResults execute(InputStream input)
+ throws WaoException, WaoBusinessException {
+ serviceReferential.importTerrestrialLocations(input);
+ return null;
+ }
+ };
+ }
+
public InputStream getActivityCalendarLogFile() {
return getActivityCalendarLogFile(WaoProperty.FILENAME_LOG_ACTIVITY_IMPORT);
}
@@ -746,58 +758,6 @@
return GlobalIndicatorValue.valueOf(indicatorLevel.getLevel());
}
- /************************ Contact states motifs **************************/
-
- @InjectComponent
- private Form contactStateMotifsForm;
-
- @Property
- @Persist
- private String contactStateMotifId;
-
- @Property
- private String translation;
-
- private GenericSelectModel<ContactStateMotif> contactStateMotifSelectModel;
-
- @Log
- public GenericSelectModel<ContactStateMotif> getContactStateMotifSelectModel() {
- if (contactStateMotifSelectModel == null) {
- List<ContactStateMotif> motifs = serviceReferential.getAllContactStateMotifs(null);
- if (motifs.size() != 0) {
- contactStateMotifSelectModel = new GenericSelectModel<ContactStateMotif>(
- motifs, ContactStateMotif.class, "translation", ContactStateMotif.TOPIA_ID, propertyAccess);
- }
- }
- return contactStateMotifSelectModel;
- }
-//
-// @Log
-// public Object onValueChangedFromContactStateMotif(String contactStateMotifId) {
-// this.contactStateMotifId = contactStateMotifId;
-// ContactStateMotif motif = getContactStateMotifSelectModel().findObject(contactStateMotifId);
-// translation = motif.getTranslation();
-// return contactStateMotifsForm;
-// }
-
- @Log
- public Form onSuccessFromContactStateMotifsForm() {
- ContactStateMotif motif;
- if (StringUtils.isEmpty(contactStateMotifId)) {
- motif = new ContactStateMotifImpl(ContactState.CONTACT_REFUSED);
- } else {
- motif = getContactStateMotifSelectModel().findObject(contactStateMotifId);
- }
- motif.setTranslation(manager.getCurrentLocale(), translation);
- List<ContactStateMotif> motifs = new ArrayList<ContactStateMotif>();
- motifs.add(motif);
- serviceReferential.updateAllContactStatesMotifs(motifs);
- // refresh
- translation = null;
- contactStateMotifSelectModel = null;
- return contactStateMotifsForm;
- }
-
/*************************************** I18N ****************************/
@Inject
Modified: trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/pages/Contacts.java
===================================================================
--- trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/pages/Contacts.java 2011-02-15 17:30:54 UTC (rev 1015)
+++ trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/pages/Contacts.java 2011-02-16 10:52:59 UTC (rev 1016)
@@ -307,7 +307,7 @@
* @return true if import/export of contacts can be done
*/
public boolean canImportExport() {
- return user.isAdmin() && ! user.isReadOnly();
+ return (user.isAdmin() || user.isCoordinator()) && ! user.isReadOnly();
}
@Log
Modified: trunk/wao-ui/src/main/resources/i18n/wao-ui_en_GB.properties
===================================================================
--- trunk/wao-ui/src/main/resources/i18n/wao-ui_en_GB.properties 2011-02-15 17:30:54 UTC (rev 1015)
+++ trunk/wao-ui/src/main/resources/i18n/wao-ui_en_GB.properties 2011-02-16 10:52:59 UTC (rev 1016)
@@ -116,7 +116,7 @@
wao.ui.field.SampleRow.size=Boat size
wao.ui.field.SampleRow.species=Species
wao.ui.field.SampleRow.terrestrialLocation=Maritime district
-wao.ui.field.SampleRow.terrestrialLocationInfos=
+wao.ui.field.SampleRow.terrestrialLocationInfos=Terrestrial Location Infos
wao.ui.field.User.firstName=First name
wao.ui.field.User.lastName=Last name
wao.ui.field.User.login=Login
@@ -165,6 +165,7 @@
wao.ui.import.fullDescription=Import %s (%s format with UTF-8 characters set)
wao.ui.import.longTitle=CSV Import/Export (UTF-8)
wao.ui.import.samplingPlanLabel=of the sampling plan
+wao.ui.import.terrestrialLocationsLabel=of the terrestrial locations
wao.ui.import.title=Import %s
wao.ui.indicator.andMore=%s and more
wao.ui.indicator.bounds=Bounds
Modified: trunk/wao-ui/src/main/resources/i18n/wao-ui_fr_FR.properties
===================================================================
--- trunk/wao-ui/src/main/resources/i18n/wao-ui_fr_FR.properties 2011-02-15 17:30:54 UTC (rev 1015)
+++ trunk/wao-ui/src/main/resources/i18n/wao-ui_fr_FR.properties 2011-02-16 10:52:59 UTC (rev 1016)
@@ -165,6 +165,7 @@
wao.ui.import.fullDescription=Import %s (format %s avec encodage UTF-8)
wao.ui.import.longTitle=Import/Export CSV (UTF-8)
wao.ui.import.samplingPlanLabel=du plan d'\u00E9chantillonnage
+wao.ui.import.terrestrialLocationsLabel=des lieux terrestres
wao.ui.import.title=Import %s
wao.ui.indicator.andMore=%s et plus
wao.ui.indicator.bounds=Bornes
Modified: trunk/wao-ui/src/main/webapp/Administration.tml
===================================================================
--- trunk/wao-ui/src/main/webapp/Administration.tml 2011-02-15 17:30:54 UTC (rev 1015)
+++ trunk/wao-ui/src/main/webapp/Administration.tml 2011-02-16 10:52:59 UTC (rev 1016)
@@ -177,7 +177,7 @@
</p:else>
</t:unless>
- <!-- t:importFieldSet t:label="${message:wao.ui.import.boatDistrictLabel}" t:engine="boatDistrictImportEngine" t:format="KML" / -->
+ <t:importFieldSet t:label="${message:wao.ui.import.terrestrialLocationsLabel}" t:engine="terrestrialLocationsImportEngine" />
</t:if>
<div class="mtop10" />
@@ -259,21 +259,4 @@
</t:if>
- <t:if t:test="currentUser.admin">
- <t:if t:test="currentUser.obsMer">
- <h2>${message:wao.ui.field.Contact.contactStateMotifs}</h2>
-
- <t:zone t:id="contactStateMotifsFormZone" t:update="show" class="acenter">
- <form t:type="form" t:zone="contactStateMotifsFormZone" t:id="contactStateMotifsForm">
- <input t:type="select"
- t:id="contactStateMotif"
- t:value="contactStateMotifId"
- t:model="contactStateMotifSelectModel" />
- <input t:type="textfield" t:value="translation" />
- <input t:type="submit" class="ico save" value="${message:wao.ui.action.save}" />
- </form>
- </t:zone>
- </t:if>
- </t:if>
-
</t:layout>
Modified: trunk/wao-ui/src/main/webapp/Contacts.tml
===================================================================
--- trunk/wao-ui/src/main/webapp/Contacts.tml 2011-02-15 17:30:54 UTC (rev 1015)
+++ trunk/wao-ui/src/main/webapp/Contacts.tml 2011-02-16 10:52:59 UTC (rev 1016)
@@ -224,12 +224,19 @@
</legend>
<form t:type="form" t:id="importContacts">
<t:errors />
- <label for="contactsCsvFile">${message:wao.ui.form.contactsFile} :</label>
- <input t:type="upload" t:id="contactsCsvFile" t:validate="required" />
- <input t:type="submit" class="ico import" value="${message:wao.ui.action.runImport}" title="${message:wao.ui.action.runImport}" />
+ <t:if test="user.admin">
+ <label for="contactsCsvFile">${message:wao.ui.form.contactsFile} :</label>
+ <input t:type="upload" t:id="contactsCsvFile" t:validate="required" />
+ <input t:type="submit" class="ico import" value="${message:wao.ui.action.runImport}" title="${message:wao.ui.action.runImport}" />
+ </t:if>
<a t:type="actionlink" t:id="exportShowContacts">
- <img src="${asset:context:img/file-export-22px.png}" alt="${message:wao.ui.action.runExport}"
- title="${message:wao.ui.action.runExport}" />
+ <t:if test="user.admin">
+ <img src="${asset:context:img/file-export-22px.png}" alt="${message:wao.ui.action.runExport}"
+ title="${message:wao.ui.action.runExport}" />
+ <p:else>
+ ${message:wao.ui.action.runExport}
+ </p:else>
+ </t:if>
</a>
</form>
</fieldset>