[opencms-dev] IMPORT USERS FROM EXTERNAL DATABASE TO OPENCMS

Vahe Momjian m_vahe at yahoo.com
Thu Apr 8 12:06:45 CEST 2010


Hello

I have had a situation like this las year, I needed to import users to Newsletter subscription from external XML file.

Look into this source , I could help you

import org.openuri.fdadEmailSubscribers.impl.RootDocumentImpl;
import org.openuri.fdadEmailSubscribers.SubscriberDocument;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.opencms.file.CmsUser;
import org.opencms.file.CmsObject;
import org.opencms.main.CmsException;
import org.opencms.main.OpenCms;
import org.opencms.jsp.CmsJspActionElement;
import org.opencms.util.CmsStringUtil;

import java.io.File;
import java.io.IOException;
import java.util.Locale;
import java.util.Collections;

import com.alkacon.opencms.newsletter.CmsNewsletterManager;

import javax.servlet.jsp.JspWriter;

/**
 * Created by IntelliJ IDEA.
 * User: Vahe Momjyan
 * Date: 03.09.2009
 * Time: 16:55:14
 */
public class CmsNewsletterUserImport {

    /**
     * The admin CmsObject that is used for user/group operations.
     */
    private CmsObject m_adminCms;

    /**
     * The name of the newsletter module.
     */
    public static final String MODULE_NAME = CmsNewsletterManager.class.getPackage().getName();

    /**
     * The default password for all newsletter users, can/should be overwritten in the module parameter.
     */
    private static final String PASSWORD_USER = "Uw82-Qn!";

     /**
     * The full name of newletter subscription mailing list.
     */
    private static final String NESLETTER_OFQN = "nl_Newsletter/Mailing List";


    public CmsNewsletterUserImport(CmsJspActionElement jsp) {
        this.m_adminCms = jsp.getCmsObject();

    }


    protected CmsUser createNewsletterUser(String email, String groupName, boolean activate, Locale locale) {

        CmsUser user = null;
        // create additional infos containing the active flag set to passed parameter

        try {
            String ouFqn = getAdminCms().readGroup(groupName).getOuFqn();
            try {
                // first try to read the user
                user = getAdminCms().readUser(ouFqn + email);
            } catch (CmsException e) {
                // user does not exist
            }
            if (user == null) {
                // create the user with additional infos
                user = getAdminCms().createUser(
                        ouFqn + email,
                        getPassword(),
                        "Alkacon OpenCms Newsletter",
                        Collections.EMPTY_MAP);
                // set the users email address
                user.setEmail(email);
                if (!activate) {
                    // set the additional info as marker that the new user is currently not active at all
                    user.setAdditionalInfo(CmsNewsletterManager.USER_ADDITIONALINFO_ACTIVE, Boolean.FALSE);
                }
            } else {
                Object o = user.getAdditionalInfo(CmsNewsletterManager.USER_ADDITIONALINFO_ACTIVE + groupName);
                if (o != null) {
                    // user tried to subscribe to this mailing list group before, return null to show error message
                    return null;
                }
            }
            user.setAdditionalInfo(CmsNewsletterManager.USER_ADDITIONALINFO_ACTIVE + groupName, Boolean.valueOf(activate));
            user.setAdditionalInfo("Language", locale.getLanguage());
            if (activate && user.getAdditionalInfo().get(CmsNewsletterManager.USER_ADDITIONALINFO_ACTIVE) != null) {
                // remove flag that this user is not active at all
                user.deleteAdditionalInfo(CmsNewsletterManager.USER_ADDITIONALINFO_ACTIVE);
            }
            // write the user
            getAdminCms().writeUser(user);
            // add the user to the given mailing list group
            getAdminCms().addUserToGroup(user.getName(), groupName);
        } catch (CmsException e) {
            // error creating user or modifying user
        }
        return user;
    }

    /**
     * Returns the admin CmsObject that is used for user/group operations.<p>
     *
     * @return the admin CmsObject that is used for user/group operations
     */
    private CmsObject getAdminCms() {

        return m_adminCms;
    }

    /**
     * Returns the password to use for all newsletter users.<p>
     *
     * @return the password to use for all newsletter users
     */
    public static String getPassword() {

        return OpenCms.getModuleManager().getModule(CmsNewsletterManager.MODULE_NAME).getParameter(CmsNewsletterManager.MODULE_PARAM_PASSWORD_USER, PASSWORD_USER);
    }

    public void ImportUsersFromXML(String xmlfilename, JspWriter out) throws XmlException, IOException {
        // Input XML
        File xmlFile = new File(xmlfilename);

        // Bind the instance to the generated XMLBeans type.
        RootDocumentImpl document = null;

        document = (RootDocumentImpl) XmlObject.Factory.parse(xmlFile);

        int counter = 0;
        SubscriberDocument.Subscriber[] allNewSubscribers = document.getRoot().getSubscriberArray();

        //get default locale
        Locale defLocale = OpenCms.getWorkplaceManager().getDefaultLocale();
        for (SubscriberDocument.Subscriber each : allNewSubscribers) {

            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(each.getEmail())) {
                createNewsletterUser(each.getEmail(), NESLETTER_OFQN, true, defLocale);
                counter++;
            }
            else
            {
                out.write("User not imported<BR><b>" + each.getEmail() + "</b><br>");
            }
        }
        out.write("Total users imported: " + counter);
    }
}



________________________________
From: Ruben Martín <ruben.martin.ramos at gmail.com>
To: opencms-dev at opencms.org
Sent: Thu, April 8, 2010 1:22:00 PM
Subject: [opencms-dev] IMPORT USERS FROM EXTERNAL DATABASE TO OPENCMS

Hi guys ,

Is There any way of import users from an external database and mapping to the fields of the OAMP register module fields ? We could use LDAP but it is mandatory to import every data of the users into the opencms.

Is there any way ?

Thanks in advance


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20100408/3e119bf7/attachment.htm>


More information about the opencms-dev mailing list