- Association between GenericContact and GenericAddress is now modeled like other associations (necessary to work around limitations in the publication process and of PDL). Solves Ticket #2039.

- Is was not possible to remove a address from a contact. See #2017.

Attention: Database Upgrade ccm-cms-6.6.10-6.6.11 required for existing databases!



git-svn-id: https://svn.libreccm.org/ccm/trunk@2547 8810af33-2d31-482b-a856-94f89814c4df
master
jensp 2014-03-01 14:03:23 +00:00
parent 4aed32729b
commit 1b23384b47
19 changed files with 1205 additions and 304 deletions

View File

@ -1,6 +1,6 @@
model com.arsdigita.cms.contenttypes;
import com.arsdigita.cms.ContentPage;
import com.arsdigita.cms.*;
// Address object
object type GenericAddress extends ContentPage {
@ -14,3 +14,7 @@ object type GenericAddress extends ContentPage {
reference key ( cms_addresses.address_id );
}
object type GenericAddressBundle extends ContentBundle {
reference key (cms_address_bundles.bundle_id);
}

View File

@ -25,9 +25,6 @@ import com.arsdigita.cms.*;
// Contact Object
object type GenericContact extends ContentPage {
GenericAddress [0..1] address = join cms_contacts.address_id
to cms_addresses.address_id;
component GenericContactEntry [0..n] contactentries = join cms_contacts.contact_id
to cms_contactEntries.contact_id;
@ -65,3 +62,21 @@ association {
String[0..1] linkKey = cms_person_contact_map.link_key VARCHAR(100);
}
association {
GenericAddressBundle[0..n] address = join cms_contact_bundles.bundle_id
to cms_contact_address_map.contact_id,
join cms_contact_address_map.address_id
to cms_address_bundles.bundle_id;
GenericContactBundle[0..n] contacts = join cms_address_bundles.bundle_id
to cms_contact_address_map.address_id,
join cms_contact_address_map.contact_id
to cms_contact_bundles.bundle_id;
//We don't need for UI purposes but other the ContentItem#add method does not return a link
//object if an association has no attributes...
BigDecimal[0..1] linkOrder = cms_contact_address_map.link_order INTEGER;
}

View File

@ -12,7 +12,7 @@
mode="hidden">
<ctd:authoring-kit
createComponent="com.arsdigita.cms.ui.authoring.PageCreate">
createComponent="com.arsdigita.cms.contenttypes.ui.GenericAddressCreate">
<ctd:authoring-step
labelKey="cms.contenttypes.shared.basic_properties.title"

View File

@ -80,5 +80,8 @@
<version from="6.6.10" to="6.6.11">
<!-- Add stacktrace column for cms_publish_lock -->
<script sql="ccm-cms/upgrade/::database::-6.6.10-6.6.11.sql"/>
<!-- Refactor the association between GenericContact and GenericAddress to the model used by
similar assocs -->
<script class="com.arsdigita.cms.contenttypes.upgrades.Upgrade6610to6611ContactAddressAssoc"/>
</version>
</upgrade>

View File

@ -172,3 +172,4 @@ cms.contenttypes.ui.genericorgaunit.persons.status=Status
cms.ui.edit_assoc=Edit
cms.contenttypes.ui.person.contact.type=Contact Type
cms.contenttypes.ui.person.contact.title=Title
cms.contenttypes.ui.contact.person.confirm_remove=Remove address

View File

@ -182,3 +182,4 @@ cms.contenttypes.ui.genericorgaunit.persons.status=Status
cms.ui.edit_assoc=Bearbeiten
cms.contenttypes.ui.person.contact.type=Art des Kontaktes
cms.contenttypes.ui.person.contact.title=Titel
cms.contenttypes.ui.contact.person.confirm_remove=Addresse l\u00f6schen

View File

@ -175,6 +175,10 @@ public class GenericAddress extends ContentPage {
set(STATE, state);
}
public GenericAddressBundle getGenericAddressBundle() {
return (GenericAddressBundle) getContentBundle();
}
// Convert the iso country code to country names using the current locale
public static String getCountryNameFromIsoCode(String isoCountryCode) {

View File

@ -0,0 +1,107 @@
/*
* Copyright (C) 2014 Jens Pelzetter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
package com.arsdigita.cms.contenttypes;
import com.arsdigita.cms.ContentBundle;
import com.arsdigita.cms.ContentItem;
import com.arsdigita.cms.CustomCopy;
import com.arsdigita.cms.ItemCopier;
import com.arsdigita.domain.DataObjectNotFoundException;
import com.arsdigita.domain.DomainObjectFactory;
import com.arsdigita.persistence.DataCollection;
import com.arsdigita.persistence.DataObject;
import com.arsdigita.persistence.OID;
import com.arsdigita.persistence.metadata.Property;
import com.arsdigita.util.Assert;
import java.math.BigDecimal;
/**
*
* @author Jens Pelzetter <jens@jp-digital.de>
*/
public class GenericAddressBundle extends ContentBundle {
public static final String BASE_DATA_OBJECT_TYPE
= "com.arsdigita.cms.contenttypes.GenericAddressBundle";
private static final String CONTACTS = "contacts";
public GenericAddressBundle(final ContentItem primary) {
super(BASE_DATA_OBJECT_TYPE);
Assert.exists(primary, ContentItem.class);
setDefaultLanguage(primary.getLanguage());
setContentType(primary.getContentType());
addInstance(primary);
super.setName(primary.getName());
}
public GenericAddressBundle(final OID oid) throws DataObjectNotFoundException {
super(oid);
}
public GenericAddressBundle(final BigDecimal id) throws DataObjectNotFoundException {
super(new OID(BASE_DATA_OBJECT_TYPE, id));
}
public GenericAddressBundle(final DataObject dobj) {
super(dobj);
}
public GenericAddressBundle(final String type) {
super(type);
}
@Override
public boolean copyProperty(final CustomCopy source,
final Property property,
final ItemCopier copier) {
final String attribute = property.getName();
final GenericAddressBundle addressBundle = (GenericAddressBundle) source;
if (copier.getCopyType() == ItemCopier.VERSION_COPY) {
if (CONTACTS.equals(attribute)) {
final DataCollection contacts = (DataCollection) addressBundle.get(CONTACTS);
while (contacts.next()) {
createContactAssoc(contacts);
}
return true;
} else {
return super.copyProperty(source, property, copier);
}
} else {
return super.copyProperty(source, property, copier);
}
}
private void createContactAssoc(final DataCollection contacts) {
final GenericContactBundle draftContact = (GenericContactBundle) DomainObjectFactory.
newInstance(contacts.getDataObject());
final GenericContactBundle liveContact = (GenericContactBundle) draftContact.getLiveVersion();
if (liveContact != null) {
final DataObject link = add(CONTACTS, liveContact);
link.save();
}
}
}

View File

@ -28,7 +28,6 @@ import com.arsdigita.cms.ContentType;
import com.arsdigita.cms.ContentPage;
import com.arsdigita.cms.ExtraXMLGenerator;
import com.arsdigita.cms.RelationAttributeInterface;
import com.arsdigita.domain.DomainObjectFactory;
import com.arsdigita.persistence.DataCollection;
import com.arsdigita.util.Assert;
import com.arsdigita.xml.Element;
@ -44,7 +43,7 @@ import org.apache.log4j.Logger;
public class GenericContact extends ContentPage implements
RelationAttributeInterface, ExtraXMLGenerator {
private static final Logger logger = Logger.getLogger(GenericContact.class);
private static final Logger LOGGER = Logger.getLogger(GenericContact.class);
/**
* PDL property names
*/
@ -52,47 +51,44 @@ public class GenericContact extends ContentPage implements
// public static final String CONTACT_TYPE = "";
public static final String ADDRESS = "address";
public static final String CONTACT_ENTRIES = "contactentries";
public static final String CONTACTS_KEY =
GenericPersonContactCollection.CONTACTS_KEY;
private static final String RELATION_ATTRIBUTES =
"person.link_key:GenericContactTypes;contactentries.key:GenericContactEntryKeys";
public static final String CONTACTS_KEY = GenericPersonContactCollection.CONTACTS_KEY;
private static final String RELATION_ATTRIBUTES
= "person.link_key:GenericContactTypes;contactentries.key:GenericContactEntryKeys";
// Config
private static final GenericContactConfig s_config =
new GenericContactConfig();
private static final GenericContactConfig s_config = new GenericContactConfig();
static {
logger.debug("Static initializer is starting...");
LOGGER.debug("Static initializer is starting...");
s_config.load();
logger.debug("Static initializer finished");
LOGGER.debug("Static initializer finished");
}
/**
* Data object type for this domain object
*/
public static final String BASE_DATA_OBJECT_TYPE =
"com.arsdigita.cms.contenttypes.GenericContact";
public static final String BASE_DATA_OBJECT_TYPE
= "com.arsdigita.cms.contenttypes.GenericContact";
public GenericContact() {
super(BASE_DATA_OBJECT_TYPE);
}
public GenericContact(BigDecimal id)
public GenericContact(final BigDecimal id)
throws DataObjectNotFoundException {
this(new OID(BASE_DATA_OBJECT_TYPE, id));
}
public GenericContact(OID id)
public GenericContact(final OID id)
throws DataObjectNotFoundException {
super(id);
}
public GenericContact(DataObject obj) {
public GenericContact(final DataObject obj) {
super(obj);
}
public GenericContact(String type) {
public GenericContact(final String type) {
super(type);
//unsetPerson();
}
@Override
@ -104,6 +100,8 @@ public class GenericContact extends ContentPage implements
/**
* Retrieves the current configuration
*
* @return The Config for GenericContact
*/
public static final GenericContactConfig getConfig() {
return s_config;
@ -117,76 +115,36 @@ public class GenericContact extends ContentPage implements
// accessors
// Get the person for this contact
public GenericPerson getPerson() {
/*
* DataCollection collection;
*
* collection = (DataCollection) get(PERSON);
*
* if (collection.size() == 0) { return null; } else { DataObject dobj;
*
* collection.next(); dobj = collection.getDataObject();
*
* // Close Collection to prevent an open ResultSet collection.close();
*
* return (GenericPerson) DomainObjectFactory.newInstance(dobj);
}
*/
return getGenericContactBundle().getPerson();
}
// Set the person for this contact
public void setPerson(GenericPerson person, String contactType) {
//set(PERSON, person);
/*
* if (getPerson() != null) { unsetPerson(); }
*
* if (person != null) { Assert.exists(person, GenericPerson.class);
* DataObject link = add(PERSON, person);
* link.set(GenericPerson.CONTACTS_KEY, contactType);
* link.set(GenericPerson.CONTACTS_ORDER, new
* BigDecimal(person.getContacts().size())); link.save();
}
*/
getGenericContactBundle().setPerson(person, contactType);
}
// // Get the type for this contact
// public String getContactType() {
// return get(CONTACT_TYPE));
// }
//
// // Set the type for this contact
// public void setContactType(String type) {
// set(CONTACT_TYPE, type);
// }
// Unset the address for this contact
public void unsetPerson() {
//set(PERSON, null);
/*
* GenericPerson oldPerson; oldPerson = getPerson(); if (oldPerson !=
* null) { remove(PERSON, oldPerson);
}
*/
getGenericContactBundle().unsetPerson();
}
// Get the address for this contact
public GenericAddress getAddress() {
return (GenericAddress) DomainObjectFactory.newInstance((DataObject) get(
ADDRESS));
// return (GenericAddress) DomainObjectFactory.newInstance((DataObject) get(
// ADDRESS));
return getGenericContactBundle().getAddress();
}
// Set the address for this contact
public void setAddress(GenericAddress address) {
set(ADDRESS, address);
public void setAddress(final GenericAddress address) {
//set(ADDRESS, address);
getGenericContactBundle().setAddress(address);
}
// Unset the address for this contact
public void unsetAddress() {
set(ADDRESS, null);
//set(ADDRESS, null);
getGenericContactBundle().unsetAddress();
}
// Get all contact entries for this contact, p. ex. phone number, type of contact etc.
@ -196,42 +154,41 @@ public class GenericContact extends ContentPage implements
}
// Add a contact entry for this contact
public void addContactEntry(GenericContactEntry contactEntry) {
public void addContactEntry(final GenericContactEntry contactEntry) {
Assert.exists(contactEntry, GenericContactEntry.class);
add(CONTACT_ENTRIES, contactEntry);
}
// Remove a contect entry for this contact
public void removeContactEntry(GenericContactEntry contactEntry) {
public void removeContactEntry(final GenericContactEntry contactEntry) {
Assert.exists(contactEntry, GenericContactEntry.class);
remove(CONTACT_ENTRIES, contactEntry);
}
public String getContactType() {
final GenericPerson person = getPerson();
GenericPerson person = getPerson();
if (person != null) {
GenericPersonContactCollection collection = person.getContacts();
if (person == null) {
return null;
} else {
final GenericPersonContactCollection collection = person.getContacts();
collection.next();
String contactType = (String) collection.getContactType();
final String contactType = collection.getContactType();
// Close Collection to prevent open ResultSet
collection.close();
return contactType;
} else {
return null;
}
}
public void setContactType(String contactType) {
public void setContactType(final String contactType) {
GenericPerson person = getPerson();
final GenericPerson person = getPerson();
if (person != null) {
GenericPersonContactCollection collection = person.getContacts();
final GenericPersonContactCollection collection = person.getContacts();
collection.next();
DataObject link = (DataObject) collection.get("link");
final DataObject link = (DataObject) collection.get("link");
link.set(CONTACTS_KEY, contactType);
}
}
@ -254,10 +211,10 @@ public class GenericContact extends ContentPage implements
}
@Override
public boolean hasRelationAttributeProperty(String propertyName) {
StringTokenizer strTok = new StringTokenizer(RELATION_ATTRIBUTES, ";");
public boolean hasRelationAttributeProperty(final String propertyName) {
final StringTokenizer strTok = new StringTokenizer(RELATION_ATTRIBUTES, ";");
while (strTok.hasMoreTokens()) {
String token = strTok.nextToken();
final String token = strTok.nextToken();
if (token.startsWith(propertyName + ".")) {
return true;
}
@ -271,71 +228,108 @@ public class GenericContact extends ContentPage implements
}
@Override
public String getRelationAttributeKeyName(String propertyName) {
StringTokenizer strTok = new StringTokenizer(RELATION_ATTRIBUTES, ";");
public String getRelationAttributeKeyName(final String propertyName) {
final StringTokenizer strTok = new StringTokenizer(RELATION_ATTRIBUTES, ";");
while (strTok.hasMoreTokens()) {
String token = strTok.nextToken();
final String token = strTok.nextToken();
if (token.startsWith(propertyName + ".")) {
return token.substring(token.indexOf(".") + 1,
token.indexOf(":"));
return token.substring(token.indexOf('.') + 1,
token.indexOf(':'));
}
}
return null;
}
@Override
public String getRelationAttributeName(String propertyName) {
StringTokenizer strTok = new StringTokenizer(RELATION_ATTRIBUTES, ";");
public String getRelationAttributeName(final String propertyName) {
final StringTokenizer strTok = new StringTokenizer(RELATION_ATTRIBUTES, ";");
while (strTok.hasMoreTokens()) {
String token = strTok.nextToken();
final String token = strTok.nextToken();
if (token.startsWith(propertyName + ".")) {
return token.substring(token.indexOf(":") + 1);
return token.substring(token.indexOf(':') + 1);
}
}
return null;
}
@Override
public String getRelationAttributeKey(String propertyName) {
public String getRelationAttributeKey(final String propertyName) {
return null;
}
@Override
public void generateXML(ContentItem item, Element element, PageState state) {
public void generateXML(final ContentItem item, final Element element, final PageState state) {
if (getPerson() != null) {
Element personElem = element.newChildElement("person");
final Element personElem = element.newChildElement("person");
GenericPerson person = getPerson();
if ((person.getSurname() != null) && !person.getSurname().isEmpty()) {
Element surnameElem = personElem.newChildElement("surname");
final Element surnameElem = personElem.newChildElement("surname");
surnameElem.setText(person.getSurname());
}
if ((person.getGivenName() != null) && !person.getGivenName().isEmpty()) {
Element givenNameElem = personElem.newChildElement("givenname");
final Element givenNameElem = personElem.newChildElement("givenname");
givenNameElem.setText(person.getGivenName());
}
if ((person.getTitlePre() != null) && !person.getTitlePre().isEmpty()) {
Element titlePreElem = personElem.newChildElement("titlepre");
final Element titlePreElem = personElem.newChildElement("titlepre");
titlePreElem.setText(person.getTitlePre());
}
if ((person.getTitlePost() != null) && !person.getTitlePost().isEmpty()) {
Element titlePostElem = personElem.newChildElement("titlepost");
final Element titlePostElem = personElem.newChildElement("titlepost");
titlePostElem.setText(person.getTitlePost());
}
}
StringTokenizer keys = s_config.getContactEntryKeys();
Element contactKeysElem = element.newChildElement("contactEntryKeys");
final StringTokenizer keys = s_config.getContactEntryKeys();
final Element contactKeysElem = element.newChildElement("contactEntryKeys");
while (keys.hasMoreElements()) {
contactKeysElem.newChildElement("entryKey").setText(keys.nextToken());
}
if (getAddress() != null) {
final Element addressElem = element.newChildElement("address");
final GenericAddress address = getAddress();
if ((address.getAddress() != null) && !address.getAddress().isEmpty()) {
final Element addressAdrElem = addressElem.newChildElement("address");
addressAdrElem.setText(address.getAddress());
}
if ((address.getPostalCode() != null) && !address.getPostalCode().isEmpty()) {
final Element postalCodeElem = addressElem.newChildElement("postalCode");
postalCodeElem.setText(address.getPostalCode());
}
if ((address.getCity() != null) && !address.getCity().isEmpty()) {
final Element cityElem = addressElem.newChildElement("city");
cityElem.setText(address.getCity());
}
if ((address.getState() != null) && !address.getState().isEmpty()) {
final Element stateElem = addressElem.newChildElement("state");
stateElem.setText(address.getState());
}
if ((address.getIsoCountryCode() != null) && !address.getIsoCountryCode().isEmpty()) {
final Element isoCodeElem = addressElem.newChildElement("isoCountryCode");
isoCodeElem.setText(address.getIsoCountryCode());
final Element countryElem = addressElem.newChildElement("country");
countryElem.setText(GenericAddress.getCountryNameFromIsoCode(address.
getIsoCountryCode()));
}
final Element titleElem = element.newChildElement("title");
titleElem.setText(address.getTitle());
}
}
@Override
public void addGlobalStateParams(Page p) {
public void addGlobalStateParams(final Page page) {
//Nothing
}
@ -346,7 +340,7 @@ public class GenericContact extends ContentPage implements
@Override
public List<ExtraXMLGenerator> getExtraXMLGenerators() {
List<ExtraXMLGenerator> generators = super.getExtraXMLGenerators();
final List<ExtraXMLGenerator> generators = super.getExtraXMLGenerators();
generators.add(this);
return generators;
}

View File

@ -24,6 +24,8 @@ public class GenericContactBundle
public static final String BASE_DATA_OBJECT_TYPE =
"com.arsdigita.cms.contenttypes.GenericContactBundle";
public static final String PERSON = "person";
public static final String ADDRESS = "address";
public static final String ADDRESS_ORDER = "linkOrder";
public GenericContactBundle(final ContentItem primary) {
super(BASE_DATA_OBJECT_TYPE);
@ -93,6 +95,15 @@ public class GenericContactBundle
}
return true;
} else if(ADDRESS.equals(attribute)) {
final DataCollection addresses = (DataCollection) contactBundle.get(ADDRESS);
while(addresses.next()) {
createAddressAssoc(addresses);
}
return true;
} else {
return super.copyProperty(source, property, copier);
}
@ -122,6 +133,17 @@ public class GenericContactBundle
}
}
private void createAddressAssoc(final DataCollection addresses) {
final GenericAddressBundle draftAddress = (GenericAddressBundle) DomainObjectFactory.newInstance(addresses.getDataObject());
final GenericAddressBundle liveAddress = (GenericAddressBundle) draftAddress.getLiveVersion();
if (liveAddress != null) {
final DataObject link = add(ADDRESS, liveAddress);
link.save();
}
}
private void createOrgaUnitAssoc(final DataCollection orgaunits) {
final GenericOrganizationalUnitBundle draftOrga =
(GenericOrganizationalUnitBundle) DomainObjectFactory.
@ -168,7 +190,7 @@ public class GenericContactBundle
}
}
public void setPerson(GenericPerson person, String contactType) {
public void setPerson(final GenericPerson person, final String contactType) {
if (getPerson() != null) {
unsetPerson();
}
@ -190,4 +212,47 @@ public class GenericContactBundle
remove(PERSON, oldPerson.getGenericPersonBundle());
}
}
public GenericAddress getAddress() {
DataCollection collection;
collection = (DataCollection) get(ADDRESS);
if (collection.size() == 0) {
return null;
} else {
DataObject dobj;
collection.next();
dobj = collection.getDataObject();
//Close collection to prevent an open ResultSet
collection.close();
final GenericAddressBundle bundle = (GenericAddressBundle) DomainObjectFactory.newInstance(dobj);
return (GenericAddress) bundle.getPrimaryInstance();
}
}
public void setAddress(final GenericAddress address) {
if (getAddress() != null) {
unsetAddress();
}
if (address != null) {
Assert.exists(address, GenericAddress.class);
final DataObject link = add(ADDRESS, address.getGenericAddressBundle());
link.set(ADDRESS_ORDER, new BigDecimal("1"));
link.save();
}
}
public void unsetAddress() {
final GenericAddress oldAddress = getAddress();
if (oldAddress != null) {
remove(ADDRESS, oldAddress.getGenericAddressBundle());
}
}
}

View File

@ -0,0 +1,56 @@
package com.arsdigita.cms.contenttypes.ui;
import com.arsdigita.bebop.FormData;
import com.arsdigita.bebop.FormProcessException;
import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.event.FormSectionEvent;
import com.arsdigita.cms.ContentPage;
import com.arsdigita.cms.ContentSection;
import com.arsdigita.cms.Folder;
import com.arsdigita.cms.ItemSelectionModel;
import com.arsdigita.cms.contenttypes.GenericAddressBundle;
import static com.arsdigita.cms.ui.authoring.BasicPageForm.*;
import com.arsdigita.cms.ui.authoring.CreationSelector;
import com.arsdigita.cms.ui.authoring.PageCreate;
import java.util.Date;
/**
* Special create step for GenericAddress, creating a {@link GenericAddressBundle} instead of a
* Content Bundle
*
* @author Jens Pelzetter <jens@jp-digital.de>
* @version $Id$
*/
public class GenericAddressCreate extends PageCreate {
public GenericAddressCreate(final ItemSelectionModel itemModel,
final CreationSelector parent) {
super(itemModel, parent);
}
@Override
public void process(final FormSectionEvent fse) throws FormProcessException {
final FormData data = fse.getFormData();
final PageState state = fse.getPageState();
final ContentSection section = m_parent.getContentSection(state);
final Folder folder = m_parent.getFolder(state);
final ContentPage item = createContentPage(state);
item.setLanguage((String) data.get(LANGUAGE));
item.setName((String) data.get(NAME));
item.setTitle((String) data.get(TITLE));
if (!ContentSection.getConfig().getHideLaunchDate()) {
item.setLaunchDate((Date) data.get(LAUNCH_DATE));
}
final GenericAddressBundle bundle = new GenericAddressBundle(item);
bundle.setParent(folder);
bundle.setContentSection(section);
bundle.save();
m_workflowSection.applyWorkflow(state, item);
m_parent.editItem(state, item);
}
}

View File

@ -33,152 +33,237 @@ public class GenericContactAddressPropertiesStep extends SimpleEditStep {
public static final String EDIT_ADDRESS_SHEET_NAME = "editAddress";
public static final String CHANGE_ADDRESS_SHEET_NAME = "changeAddress";
public static final String DELETE_ADDRESS_SHEET_NAME = "deleteAddress";
private ItemSelectionModel itemModel;
//private ItemSelectionModel itemModel;
private final WorkflowLockedComponentAccess addAddress;
/** Creates a new instance of GenericContactAddressPropertiesStep */
public GenericContactAddressPropertiesStep(ItemSelectionModel itemModel,
AuthoringKitWizard parent) {
/**
* Creates a new instance of GenericContactAddressPropertiesStep
*/
public GenericContactAddressPropertiesStep(final ItemSelectionModel itemModel,
final AuthoringKitWizard parent) {
this(itemModel, parent, "");
}
public GenericContactAddressPropertiesStep(ItemSelectionModel itemModel,
AuthoringKitWizard parent,
String prefix) {
public GenericContactAddressPropertiesStep(final ItemSelectionModel itemModel,
final AuthoringKitWizard parent,
final String prefix) {
super(itemModel, parent, prefix);
this.itemModel = itemModel;
BasicPageForm attachAddressSheet =
new GenericContactAttachAddressPropertyForm(itemModel,
final BasicPageForm addAddressSheet = new GenericContactAttachAddressPropertyForm(itemModel,
this);
BasicPageForm reattachAddressSheet =
new GenericContactAttachAddressPropertyForm(itemModel,
this);
BasicPageForm editAddressSheet =
new GenericContactEditAddressPropertyForm(itemModel, this);
BasicPageForm deleteAddressSheet = new GenericContactDeleteAddressForm(
itemModel, this);
addAddress = new WorkflowLockedComponentAccess(addAddressSheet, itemModel);
add(ADD_ADDRESS_SHEET_NAME,
ContenttypesGlobalizationUtil.globalize(
"cms.contenttypes.ui.contact.attach_address"),
new AttachAddressWorkflowLockedComponentAccess(attachAddressSheet,
itemModel),
attachAddressSheet.getSaveCancelSection().getCancelButton());
add(CHANGE_ADDRESS_SHEET_NAME,
ContenttypesGlobalizationUtil.globalize(
"cms.contenttypes.ui.contact.reattach_address"),
new EditAddressWorkflowLockedComponentAccess(reattachAddressSheet,
itemModel),
reattachAddressSheet.getSaveCancelSection().getCancelButton());
/*add(EDIT_ADDRESS_SHEET_NAME, (String) ContenttypesGlobalizationUtil.
globalize("cms.contenttypes.ui.contact.edit_address").localize(),
new EditAddressWorkflowLockedComponentAccess(editAddressSheet,
itemModel),
editAddressSheet.getSaveCancelSection().getCancelButton());*/
add(DELETE_ADDRESS_SHEET_NAME,
ContenttypesGlobalizationUtil.globalize(
"cms.contenttypes.ui.contact.delete_address"),
new EditAddressWorkflowLockedComponentAccess(deleteAddressSheet,
itemModel),
deleteAddressSheet.getSaveCancelSection().getCancelButton());
ContenttypesGlobalizationUtil.globalize("cms.contenttypes.ui.contact.attach_address"),
addAddress,
addAddressSheet.getSaveCancelSection().getCancelButton());
/* Set the displayComponent for this step */
setDisplayComponent(getAddressPropertySheet(itemModel));
// this.itemModel = itemModel;
//
// BasicPageForm attachAddressSheet = new GenericContactAttachAddressPropertyForm(itemModel,
// this);
// BasicPageForm reattachAddressSheet = new GenericContactAttachAddressPropertyForm(itemModel,
// this);
// BasicPageForm editAddressSheet = new GenericContactEditAddressPropertyForm(itemModel, this);
// BasicPageForm deleteAddressSheet = new GenericContactDeleteAddressForm(
// itemModel, this);
//
// add(ADD_ADDRESS_SHEET_NAME,
// ContenttypesGlobalizationUtil.globalize(
// "cms.contenttypes.ui.contact.attach_address"),
// new AttachAddressWorkflowLockedComponentAccess(attachAddressSheet,
// itemModel),
// attachAddressSheet.getSaveCancelSection().getCancelButton());
// add(CHANGE_ADDRESS_SHEET_NAME,
// ContenttypesGlobalizationUtil.globalize(
// "cms.contenttypes.ui.contact.reattach_address"),
// new EditAddressWorkflowLockedComponentAccess(reattachAddressSheet,
// itemModel),
// reattachAddressSheet.getSaveCancelSection().getCancelButton());
//
// add(DELETE_ADDRESS_SHEET_NAME,
// ContenttypesGlobalizationUtil.globalize(
// "cms.contenttypes.ui.contact.delete_address"),
// new EditAddressWorkflowLockedComponentAccess(deleteAddressSheet,
// itemModel),
// deleteAddressSheet.getSaveCancelSection().getCancelButton());
//
// /* Set the displayComponent for this step */
// setDisplayComponent(getAddressPropertySheet(itemModel));
}
public static Component getAddressPropertySheet(final ItemSelectionModel itemModel) {
final GenericContactAddressSheet sheet = new GenericContactAddressSheet(itemModel);
return sheet;
}
/**
*
* @param itemModel
*
* @return
*/
public static Component getAddressPropertySheet(ItemSelectionModel itemModel) {
DomainObjectPropertySheet sheet = new DomainObjectPropertySheet(
itemModel);
sheet.add(ContenttypesGlobalizationUtil.globalize(
"cms.contenttypes.ui.address.address"),
"address." + GenericAddress.ADDRESS);
if (!GenericContact.getConfig().getHideAddressPostalCode()) {
sheet.add(ContenttypesGlobalizationUtil.globalize(
"cms.contenttypes.ui.address.postal_code"),
"address." + GenericAddress.POSTAL_CODE);
}
sheet.add(ContenttypesGlobalizationUtil.globalize(
"cms.contenttypes.ui.address.city"),
"address." + GenericAddress.CITY);
if (!GenericContact.getConfig().getHideAddressState()) {
sheet.add(ContenttypesGlobalizationUtil.globalize(
"cms.contenttypes.ui.address.state"),
"address." + GenericAddress.STATE);
}
if (!GenericContact.getConfig().getHideAddressCountry()) {
sheet.add(ContenttypesGlobalizationUtil.globalize(
"cms.contenttypes.ui.address.iso_country_code"),
"address." + GenericAddress.ISO_COUNTRY_CODE,
new DomainObjectPropertySheet.AttributeFormatter() {
@Override
public String format(DomainObject item,
String attribute,
PageState state) {
GenericAddress Address =
((GenericContact) item).getAddress();
if (Address != null && Address.getIsoCountryCode() != null) {
return GenericAddress.getCountryNameFromIsoCode(Address.
getIsoCountryCode());
} else {
return (String) GlobalizationUtil.globalize(
"cms.ui.unknown").localize();
}
}
});
}
return sheet;
}
public static Component getEmptyBaseAddressPropertySheet(
ItemSelectionModel itemModel) {
return new Label(
(ContenttypesGlobalizationUtil.globalize(
"cms.contenttypes.ui.contact.emptyAddress")));
}
private class EditAddressWorkflowLockedComponentAccess
extends WorkflowLockedComponentAccess {
public EditAddressWorkflowLockedComponentAccess(Component c,
ItemSelectionModel i) {
super(c, i);
}
@Override
public boolean isVisible(PageState state) {
GenericContact contact = (GenericContact) itemModel.
getSelectedObject(state);
return contact.hasAddress();
}
}
private class AttachAddressWorkflowLockedComponentAccess extends WorkflowLockedComponentAccess {
public AttachAddressWorkflowLockedComponentAccess(Component c,
ItemSelectionModel i) {
super(c, i);
}
@Override
public boolean isVisible(PageState state) {
GenericContact contact = (GenericContact) itemModel.
getSelectedObject(state);
return !contact.hasAddress();
}
}
// public static Component getAddressPropertySheet(ItemSelectionModel itemModel) {
//
// DomainObjectPropertySheet sheet = new DomainObjectPropertySheet(
// itemModel);
//
// sheet.add(ContenttypesGlobalizationUtil.globalize(
// "cms.contenttypes.ui.address.address"),
// "address." + GenericAddress.ADDRESS,
// new DomainObjectPropertySheet.AttributeFormatter() {
//
// @Override
// public String format(final DomainObject obj,
// final String attribute,
// final PageState state) {
// final GenericAddress address = ((GenericContact) obj).getAddress();
// if ((address == null) || (address.getAddress() == null)) {
// return (String) GlobalizationUtil.globalize(
// "cms.ui.unknown").localize();
// } else {
// return address.getAddress();
//
// }
// }
//
// });
// if (!GenericContact.getConfig().getHideAddressPostalCode()) {
// sheet.add(ContenttypesGlobalizationUtil.globalize(
// "cms.contenttypes.ui.address.postal_code"),
// "address." + GenericAddress.POSTAL_CODE,
// new DomainObjectPropertySheet.AttributeFormatter() {
//
// @Override
// public String format(final DomainObject obj,
// final String attribute,
// final PageState state) {
// final GenericAddress address = ((GenericContact) obj).getAddress();
// if ((address == null) || (address.getPostalCode() == null)) {
// return (String) GlobalizationUtil.globalize(
// "cms.ui.unknown").localize();
// } else {
// return address.getPostalCode();
//
// }
//
// }
//
// });
// }
// sheet.add(ContenttypesGlobalizationUtil.globalize(
// "cms.contenttypes.ui.address.city"),
// "address." + GenericAddress.CITY,
// new DomainObjectPropertySheet.AttributeFormatter() {
//
// @Override
// public String format(final DomainObject obj,
// final String attribute,
// final PageState state) {
// final GenericAddress address = ((GenericContact) obj).getAddress();
// if ((address == null) || (address.getCity() == null)) {
// return (String) GlobalizationUtil.globalize(
// "cms.ui.unknown").localize();
// } else {
// return address.getCity();
//
// }
//
// }
//
// });
// if (!GenericContact.getConfig().getHideAddressState()) {
// sheet.add(ContenttypesGlobalizationUtil.globalize(
// "cms.contenttypes.ui.address.state"),
// "address." + GenericAddress.STATE,
// new DomainObjectPropertySheet.AttributeFormatter() {
//
// @Override
// public String format(final DomainObject obj,
// final String attribute,
// final PageState state) {
// final GenericAddress address = ((GenericContact) obj).getAddress();
// if ((address == null) || (address.getState() == null)) {
// return (String) GlobalizationUtil.globalize(
// "cms.ui.unknown").localize();
// } else {
// return address.getState();
//
// }
//
// }
//
// });
// }
//
// if (!GenericContact.getConfig().getHideAddressCountry()) {
// sheet.add(ContenttypesGlobalizationUtil.globalize(
// "cms.contenttypes.ui.address.iso_country_code"),
// "address." + GenericAddress.ISO_COUNTRY_CODE,
// new DomainObjectPropertySheet.AttributeFormatter() {
//
// @Override
// public String format(DomainObject item,
// String attribute,
// PageState state) {
// GenericAddress address = ((GenericContact) item).getAddress();
// if (address != null && address.getIsoCountryCode() != null) {
// return GenericAddress.getCountryNameFromIsoCode(address.
// getIsoCountryCode());
// } else {
// return (String) GlobalizationUtil.globalize(
// "cms.ui.unknown").localize();
// }
// }
//
// });
// }
//
// return sheet;
//
// }
//
// public static Component getEmptyBaseAddressPropertySheet(
// ItemSelectionModel itemModel) {
// return new Label(
// (ContenttypesGlobalizationUtil.globalize(
// "cms.contenttypes.ui.contact.emptyAddress")));
// }
//
// private class EditAddressWorkflowLockedComponentAccess
// extends WorkflowLockedComponentAccess {
//
// public EditAddressWorkflowLockedComponentAccess(Component c,
// ItemSelectionModel i) {
// super(c, i);
// }
//
// @Override
// public boolean isVisible(PageState state) {
// GenericContact contact = (GenericContact) itemModel.
// getSelectedObject(state);
//
// return contact.hasAddress();
// }
//
// }
//
// private class AttachAddressWorkflowLockedComponentAccess extends WorkflowLockedComponentAccess {
//
// public AttachAddressWorkflowLockedComponentAccess(Component c,
// ItemSelectionModel i) {
// super(c, i);
// }
//
// @Override
// public boolean isVisible(PageState state) {
// GenericContact contact = (GenericContact) itemModel.
// getSelectedObject(state);
//
// return !contact.hasAddress();
// }
//
// }
}

View File

@ -0,0 +1,239 @@
package com.arsdigita.cms.contenttypes.ui;
import com.arsdigita.bebop.Component;
import com.arsdigita.bebop.ControlLink;
import com.arsdigita.bebop.Label;
import com.arsdigita.bebop.Link;
import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.Table;
import com.arsdigita.bebop.event.TableActionEvent;
import com.arsdigita.bebop.event.TableActionListener;
import com.arsdigita.bebop.table.TableCellRenderer;
import com.arsdigita.bebop.table.TableColumn;
import com.arsdigita.bebop.table.TableColumnModel;
import com.arsdigita.bebop.table.TableModel;
import com.arsdigita.bebop.table.TableModelBuilder;
import com.arsdigita.cms.CMS;
import com.arsdigita.cms.ContentSection;
import com.arsdigita.cms.ItemSelectionModel;
import com.arsdigita.cms.contenttypes.GenericAddress;
import com.arsdigita.cms.contenttypes.GenericContact;
import com.arsdigita.cms.contenttypes.util.ContenttypesGlobalizationUtil;
import com.arsdigita.cms.dispatcher.ItemResolver;
import com.arsdigita.domain.DataObjectNotFoundException;
import com.arsdigita.globalization.GlobalizedMessage;
import com.arsdigita.util.LockableImpl;
import java.math.BigDecimal;
/**
*
* @author Jens Pelzetter <jens@jp-digital.de>
* @version $Id$
*/
public class GenericContactAddressSheet extends Table implements TableActionListener {
private static final String TABLE_COL_EDIT = "table_col_edit";
private static final String TABLE_COL_DEL = "table_col_del";
private final ItemSelectionModel itemModel;
public GenericContactAddressSheet(final ItemSelectionModel itemModel) {
super();
this.itemModel = itemModel;
setEmptyView(new Label(ContenttypesGlobalizationUtil.globalize(
"cms.contenttypes.ui.contact.emptyAddress")));
final TableColumnModel colModel = getColumnModel();
colModel.add(new TableColumn(
0,
new Label(""),
TABLE_COL_EDIT));
colModel.add(new TableColumn(
1,
new Label(""),
TABLE_COL_DEL));
setModelBuilder(new GenericContactAddressTableModelBuilder(itemModel));
colModel.get(0).setCellRenderer(new EditCellRenderer());
colModel.get(1).setCellRenderer(new DeleteCellRenderer());
addTableActionListener(this);
}
private class GenericContactAddressTableModelBuilder
extends LockableImpl
implements TableModelBuilder {
private ItemSelectionModel itemModel;
public GenericContactAddressTableModelBuilder(final ItemSelectionModel itemModel) {
this.itemModel = itemModel;
}
@Override
public TableModel makeModel(final Table table, final PageState state) {
table.getRowSelectionModel().clearSelection(state);
final GenericContact contact = (GenericContact) itemModel.getSelectedObject(state);
return new GenericContactAddressTableModel(table, state, contact);
}
}
private class GenericContactAddressTableModel implements TableModel {
private final Table table;
private final GenericAddress address;
private boolean done;
public GenericContactAddressTableModel(final Table table,
final PageState state,
final GenericContact contact) {
this.table = table;
address = contact.getAddress();
if (address == null) {
done = false;
} else {
done = true;
}
}
@Override
public int getColumnCount() {
return table.getColumnModel().size();
}
@Override
public boolean nextRow() {
boolean ret;
if (done) {
ret = true;
done = false;
} else {
ret = false;
}
return ret;
}
@Override
public Object getElementAt(final int columnIndex) {
switch (columnIndex) {
case 0:
return address.getTitle();
case 1:
return ContenttypesGlobalizationUtil.globalize(
"cms.contenttypes.ui.contact.delete_address.button_label");
default:
return null;
}
}
@Override
public Object getKeyAt(final int columnIndex) {
return address.getID();
}
}
private class EditCellRenderer extends LockableImpl implements TableCellRenderer {
public EditCellRenderer() {
//Nothing
}
@Override
public Component getComponent(final Table table,
final PageState state,
final Object value,
final boolean isSelected,
final Object key,
final int row,
final int column) {
com.arsdigita.cms.SecurityManager securityManager = CMS.getSecurityManager(state);
GenericContact contact = (GenericContact) itemModel.getSelectedObject(state);
boolean canEdit = securityManager.canAccess(
state.getRequest(),
com.arsdigita.cms.SecurityManager.EDIT_ITEM,
contact);
if (canEdit) {
final GenericAddress address;
try {
address = new GenericAddress((BigDecimal) key);
} catch (DataObjectNotFoundException ex) {
return new Label(value.toString());
}
final ContentSection section = address.getContentSection();
final ItemResolver resolver = section.getItemResolver();
return new Link(value.toString(),
resolver.generateItemURL(state,
address,
section,
address.getVersion()));
} else {
return new Label(value.toString());
}
}
}
private class DeleteCellRenderer extends LockableImpl implements TableCellRenderer {
@Override
public Component getComponent(Table table,
PageState state,
Object value,
boolean isSelected,
Object key,
int row,
int col) {
com.arsdigita.cms.SecurityManager securityManager = CMS.getSecurityManager(state);
GenericContact contact = (GenericContact) itemModel.getSelectedObject(
state);
boolean canEdit = securityManager.canAccess(
state.getRequest(),
com.arsdigita.cms.SecurityManager.DELETE_ITEM,
contact);
if (canEdit) {
final ControlLink link = new ControlLink(new Label((GlobalizedMessage) value));
link.setConfirmation(ContenttypesGlobalizationUtil.globalize(
"cms.contenttypes.ui.contact.person.confirm_remove"));
return link;
} else {
Label label = new Label("");
return label;
}
}
}
@Override
public void cellSelected(final TableActionEvent event) {
final PageState state = event.getPageState();
final GenericContact contact = (GenericContact) itemModel.getSelectedObject(state);
final TableColumn column = getColumnModel().get(event.getColumn().intValue());
if (TABLE_COL_DEL.equals(column.getHeaderKey().toString())) {
contact.unsetAddress();
}
}
@Override
public void headSelected(final TableActionEvent event) {
//Nothing
}
}

View File

@ -36,6 +36,7 @@ public class GenericContactDeleteAddressForm extends BasicPageForm implements Fo
GenericContactDeleteAddressForm(ItemSelectionModel itemModel, GenericContactAddressPropertiesStep step) {
super(ID, itemModel);
addSaveCancelSection();
addProcessListener(this);
}
public void init(FormSectionEvent fse) {

View File

@ -247,13 +247,13 @@ public class GenericContactEntriesTable extends Table implements TableActionList
SecurityManager.DELETE_ITEM,
contact);
if (canDelete) {
ControlLink link = new ControlLink(value.toString());
ControlLink link = new ControlLink((Label)value);
link.setConfirmation(
ContenttypesGlobalizationUtil.globalize(
"cms.contenttypes.ui.contact.confirm_delete"));
return link;
} else {
return new Label(value.toString());
return (Label) value;
}
}

View File

@ -39,7 +39,6 @@ import com.arsdigita.cms.contenttypes.GenericContact;
import com.arsdigita.cms.contenttypes.GenericPerson;
import com.arsdigita.cms.contenttypes.util.ContenttypesGlobalizationUtil;
import com.arsdigita.cms.dispatcher.ItemResolver;
//import com.arsdigita.cms.dispatcher.Utilities;
import com.arsdigita.domain.DataObjectNotFoundException;
import com.arsdigita.util.LockableImpl;
import java.math.BigDecimal;
@ -47,20 +46,20 @@ import java.math.BigDecimal;
/**
*
* @author Jens Pelzetter
* @version $Id$
*/
public class GenericContactPersonSheet
extends Table
implements TableActionListener {
public class GenericContactPersonSheet extends Table implements TableActionListener {
private final String TABLE_COL_EDIT = "table_col_edit";
private final String TABLE_COL_DEL = "table_col_del";
private ItemSelectionModel m_itemModel;
private final ItemSelectionModel m_itemModel;
/**
* Constructor.
*
* @param itemModel
*/
public GenericContactPersonSheet(ItemSelectionModel itemModel) {
public GenericContactPersonSheet(final ItemSelectionModel itemModel) {
super();
m_itemModel = itemModel;
@ -117,13 +116,13 @@ public class GenericContactPersonSheet
private class GenericContactPersonSheetModel implements TableModel {
private Table m_table;
private GenericPerson m_person;
private final Table m_table;
private final GenericPerson m_person;
private boolean m_done;
public GenericContactPersonSheetModel(Table table,
PageState state,
GenericContact contact) {
public GenericContactPersonSheetModel(final Table table,
final PageState state,
final GenericContact contact) {
m_table = table;
m_person = contact.getPerson();
if (m_person == null) {
@ -133,10 +132,12 @@ public class GenericContactPersonSheet
}
}
@Override
public int getColumnCount() {
return m_table.getColumnModel().size();
}
@Override
public boolean nextRow() {
boolean ret;
@ -150,7 +151,8 @@ public class GenericContactPersonSheet
return ret;
}
public Object getElementAt(int columnIndex) {
@Override
public Object getElementAt(final int columnIndex) {
switch (columnIndex) {
case 0:
return m_person.getFullName();
@ -162,15 +164,14 @@ public class GenericContactPersonSheet
}
}
public Object getKeyAt(int columnIndex) {
@Override
public Object getKeyAt(final int columnIndex) {
return m_person.getID();
}
}
private class EditCellRenderer
extends LockableImpl
implements TableCellRenderer {
private class EditCellRenderer extends LockableImpl implements TableCellRenderer {
@Override
public Component getComponent(Table table,
@ -181,8 +182,7 @@ public class GenericContactPersonSheet
int row,
int column) {
com.arsdigita.cms.SecurityManager securityManager =
CMS.getSecurityManager(state);
com.arsdigita.cms.SecurityManager securityManager = CMS.getSecurityManager(state);
GenericContact contact = (GenericContact) m_itemModel.getSelectedObject(state);
boolean canEdit = securityManager.canAccess(

View File

@ -36,6 +36,7 @@ import com.arsdigita.xml.Element;
*/
public class GenericPersonExtraXmlGenerator implements ExtraXMLGenerator {
@Override
public void generateXML(final ContentItem item,
final Element element,
final PageState state) {
@ -73,6 +74,7 @@ public class GenericPersonExtraXmlGenerator implements ExtraXMLGenerator {
//System.err.printf("Generated XML for a contact in %d ms\n", (System.nanoTime() - start) / 1000000);
}
@Override
public void addGlobalStateParams(final Page page) {
//Nothing
}

View File

@ -0,0 +1,324 @@
package com.arsdigita.cms.contenttypes.upgrades;
import com.arsdigita.runtime.RuntimeConfig;
import com.arsdigita.util.cmd.Program;
import com.arsdigita.util.jdbc.Connections;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.cli.CommandLine;
/**
*
* @author Jens Pelzetter <jens@jp-digital.de>
* @version $Id$
*/
public class Upgrade6610to6611ContactAddressAssoc extends Program {
public Upgrade6610to6611ContactAddressAssoc() {
super("Upgrade-6.6.10-to-6.6.11_AddressAssoc", "1.0.0", "", true, true);
}
@Override
public void doRun(final CommandLine cmdLine) {
upgradeBundles();
upgradeAssoc();
}
public static void main(final String[] args) {
new Upgrade6610to6611ContactAddressAssoc().run(args);
}
private void upgradeBundles() {
System.out.println("Starting upgrade part 1 of 2");
System.out.println("Trying to get JDBC connection...");
final Connection conn = Connections.acquire(RuntimeConfig.getConfig().
getJDBCURL());
try {
conn.setAutoCommit(false);
} catch (SQLException ex) {
System.err.println("Failed to configure JDBC connection.");
printStackTrace(ex);
close(conn);
return;
}
try {
System.err.println("Creating new table 'cms_address_bundles...'");
final Statement stmt = conn.createStatement();
stmt.addBatch("CREATE TABLE cms_address_bundles (bundle_id integer NOT NULL)");
stmt.addBatch("ALTER TABLE ONLY cms_address_bundles "
+ "ADD CONSTRAINT cms_addr_bundl_bund_id_p_f_pvc "
+ "PRIMARY KEY (bundle_id)");
stmt.addBatch("ALTER TABLE ONLY cms_address_bundles "
+ "ADD CONSTRAINT cms_addr_bundl_bund_id_f_n3leu "
+ "FOREIGN KEY (bundle_id) REFERENCES cms_bundles(bundle_id)");
stmt.executeBatch();
} catch (SQLException ex) {
System.err.println("Failed to create table 'cms_address_bundles'");
printStackTrace(ex);
rollback(conn);
close(conn);
return;
}
try {
System.out.println("Filling new table 'cms_address_bundle' with data...");
final Statement queryAddressesStmt = conn.createStatement();
final Statement stmt = conn.createStatement();
final ResultSet addressesRs = queryAddressesStmt.executeQuery(
"SELECT parent_id "
+ " FROM cms_items "
+ " JOIN cms_addresses "
+ " ON cms_items.item_id = cms_addresses.address_id");
while (addressesRs.next()) {
stmt.addBatch(String.
format("INSERT INTO cms_address_bundles(bundle_id) VALUES (%d)",
addressesRs.getInt(1)));
stmt.addBatch(String.format(
"UPDATE acs_objects "
+ "SET default_domain_class = 'com.arsdigita.cms.contenttypes.GenericAddressBundle',"
+ "object_type = 'com.arsdigita.cms.contenttypes.GenericAddressBundle'"
+ "WHERE object_id = %d",
addressesRs.getInt(1)));
}
stmt.executeBatch();
} catch (SQLException ex) {
System.err.println("Failed to fill tables.");
printStackTrace(ex);
rollback(conn);
close(conn);
return;
}
try {
conn.commit();
} catch (SQLException ex) {
System.err.println("Failed to commiting modifications.");
printStackTrace(ex);
rollback(conn);
return;
}
close(conn);
}
private void upgradeAssoc() {
System.out.println("Starting upgrade part 2 of 2");
System.out.println("Trying to get JDBC connection...");
final Connection conn = Connections.acquire(RuntimeConfig.getConfig().
getJDBCURL());
try {
conn.setAutoCommit(false);
} catch (SQLException ex) {
System.err.println("Failed to configure JDBC connection.");
printStackTrace(ex);
close(conn);
return;
}
System.out.println("Retrieving old data...");
final List<ContactBundleAddressBundlePair> newAssocs
= new ArrayList<ContactBundleAddressBundlePair>();
try {
final Statement stmt = conn.createStatement();
final List<ContactAddressPair> oldAssocs = new ArrayList<ContactAddressPair>();
final ResultSet oldAssocResult = stmt.executeQuery(
"SELECT contact_id, address_id "
+ "FROM cms_contacts");
while (oldAssocResult.next()) {
ContactAddressPair pair = new ContactAddressPair();
pair.setContactId(oldAssocResult.getBigDecimal("contact_id"));
pair.setAddressId(oldAssocResult.getBigDecimal("address_id"));
oldAssocs.add(pair);
}
stmt.addBatch("CREATE TABLE cms_contact_address_map ("
+ "contact_id integer NOT NULL,"
+ "address_id integer NOT NULL,"
+ "link_order integer)");
stmt.addBatch("ALTER TABLE ONLY cms_contact_address_map "
+ "ADD CONSTRAINT cms_con_add_map_add_id_p_r1p86 "
+ "PRIMARY KEY (contact_id, address_id)");
stmt.addBatch("ALTER TABLE ONLY cms_contact_address_map "
+ "ADD CONSTRAINT cms_con_add_map_con_id_f_u7txu "
+ "FOREIGN KEY (contact_id) "
+ "REFERENCES cms_contact_bundles(bundle_id)");
stmt.addBatch("ALTER TABLE ONLY cms_contact_address_map "
+ "ADD CONSTRAINT cms_con_add_map_add_id_f_92kx1 "
+ "FOREIGN KEY (address_id) "
+ "REFERENCES cms_address_bundles(bundle_id)");
for (ContactAddressPair pair : oldAssocs) {
if (pair.getAddressId() != null) {
ContactBundleAddressBundlePair assocPair = new ContactBundleAddressBundlePair();
System.out.printf(
"Executing SELECT parent_id FROM cms_items WHERE item_id = %s\n",
pair.getContactId().toString());
ResultSet contactBundleIdResult = stmt.executeQuery(
String.format("SELECT parent_id FROM cms_items WHERE item_id = %s",
pair.getContactId().toString()));
contactBundleIdResult.next();
assocPair.setContactBundleId(contactBundleIdResult.getBigDecimal("parent_id"));
System.out.printf(
"Executing SELECT parent_id FROM cms_items WHERE item_id = %s\n",
pair.getAddressId().toString());
ResultSet addressBundleIdResult = stmt.executeQuery(
String.format("SELECT parent_id FROM cms_items WHERE item_id = %s",
pair.getAddressId().toString()));
addressBundleIdResult.next();
assocPair.setAddressBundleId(addressBundleIdResult.
getBigDecimal("parent_id"));
System.out.println("Aadding to new assoc list...");
newAssocs.add(assocPair);
}
}
System.out.println("Inserting data into new assoc table...");
for (ContactBundleAddressBundlePair pair : newAssocs) {
stmt.addBatch(String.format(
"INSERT INTO cms_contact_address_map "
+ "(contact_id, address_id, link_order)"
+ "VALUES (%s, %s, 1)",
pair.getContactBundleId().toString(),
pair.getAddressBundleId().toString()));
}
stmt.addBatch("ALTER TABLE cms_contacts DROP COLUMN address_id");
stmt.executeBatch();
} catch (SQLException ex) {
System.err.
println("Failed to create table 'cms_address_bundles' and fill it with data.");
printStackTrace(ex);
rollback(conn);
close(conn);
return;
} catch (Exception ex) {
System.err.
println("Failed to create table 'cms_address_bundles' and fill it with data.");
printStackTrace(ex);
rollback(conn);
close(conn);
return;
}
try {
conn.commit();
} catch (SQLException ex) {
System.err.println("Failed to commiting modifications.");
printStackTrace(ex);
rollback(conn);
return;
}
close(conn);
}
private void rollback(final Connection conn) {
try {
System.err.println("WARNING: Rollback.");
conn.rollback();
} catch (SQLException ex1) {
System.err.println("Rollback failed.");
ex1.printStackTrace(System.err);
}
}
private void close(final Connection conn) {
try {
conn.close();
} catch (SQLException ex) {
System.err.println("Failed to close JDBC connection.");
printStackTrace(ex);
}
}
private void printStackTrace(final SQLException ex) {
ex.printStackTrace(System.err);
if (ex.getNextException() != null) {
printStackTrace(ex.getNextException());
}
}
private void printStackTrace(final Exception ex) {
ex.printStackTrace(System.err);
}
private class ContactAddressPair {
private BigDecimal contactId;
private BigDecimal addressId;
public ContactAddressPair() {
//Nothing
}
public BigDecimal getContactId() {
return contactId;
}
public void setContactId(final BigDecimal contactId) {
this.contactId = contactId;
}
public BigDecimal getAddressId() {
return addressId;
}
public void setAddressId(final BigDecimal addressId) {
this.addressId = addressId;
}
}
private class ContactBundleAddressBundlePair {
private BigDecimal contactBundleId;
private BigDecimal addressBundleId;
public ContactBundleAddressBundlePair() {
//Nothing
}
public BigDecimal getContactBundleId() {
return contactBundleId;
}
public void setContactBundleId(final BigDecimal contactBundleId) {
this.contactBundleId = contactBundleId;
}
public BigDecimal getAddressBundleId() {
return addressBundleId;
}
public void setAddressBundleId(final BigDecimal addressBundleId) {
this.addressBundleId = addressBundleId;
}
}
}