001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.verify;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.Organization;
022    import com.liferay.portal.service.OrganizationLocalServiceUtil;
023    import com.liferay.portal.service.persistence.OrganizationActionableDynamicQuery;
024    import com.liferay.portal.util.PortalInstances;
025    import com.liferay.portlet.asset.model.AssetEntry;
026    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
027    
028    import java.util.List;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     * @author Daniel Kocsis
033     */
034    public class VerifyOrganization extends VerifyProcess {
035    
036            @Override
037            protected void doVerify() throws Exception {
038                    rebuildTree();
039    
040                    updateOrganizationAssets();
041    
042                    updateOrganizationAssetEntries();
043            }
044    
045            protected void rebuildTree() throws Exception {
046                    long[] companyIds = PortalInstances.getCompanyIdsBySQL();
047    
048                    for (long companyId : companyIds) {
049                            OrganizationLocalServiceUtil.rebuildTree(companyId);
050                    }
051            }
052    
053            protected void updateOrganizationAssetEntries() throws Exception {
054                    ActionableDynamicQuery actionableDynamicQuery =
055                            new OrganizationActionableDynamicQuery() {
056    
057                            @Override
058                            protected void performAction(Object object) {
059                                    Organization organization = (Organization)object;
060    
061                                    try {
062                                            AssetEntry assetEntry =
063                                                    AssetEntryLocalServiceUtil.getEntry(
064                                                            Organization.class.getName(),
065                                                            organization.getOrganizationId());
066    
067                                            if (Validator.isNotNull(assetEntry.getClassUuid())) {
068                                                    return;
069                                            }
070    
071                                            assetEntry.setClassUuid(organization.getUuid());
072    
073                                            AssetEntryLocalServiceUtil.updateAssetEntry(assetEntry);
074                                    }
075                                    catch (Exception e) {
076                                            if (_log.isWarnEnabled()) {
077                                                    _log.warn(
078                                                            "Unable to update asset entry for organization " +
079                                                                    organization.getOrganizationId(),
080                                                            e);
081                                            }
082                                    }
083                            }
084    
085                    };
086    
087                    actionableDynamicQuery.performActions();
088            }
089    
090            protected void updateOrganizationAssets() throws Exception {
091                    List<Organization> organizations =
092                            OrganizationLocalServiceUtil.getNoAssetOrganizations();
093    
094                    if (_log.isDebugEnabled()) {
095                            _log.debug(
096                                    "Processing " + organizations.size() + " organizations with " +
097                                            "no asset");
098                    }
099    
100                    for (Organization organization : organizations) {
101                            try {
102                                    OrganizationLocalServiceUtil.updateAsset(
103                                            organization.getUserId(), organization, null, null);
104                            }
105                            catch (Exception e) {
106                                    if (_log.isWarnEnabled()) {
107                                            _log.warn(
108                                                    "Unable to update asset for organization " +
109                                                            organization.getOrganizationId() + ": " +
110                                                                    e.getMessage());
111                                    }
112                            }
113                    }
114    
115                    if (_log.isDebugEnabled()) {
116                            _log.debug("Assets verified for organizations");
117                    }
118            }
119    
120            private static Log _log = LogFactoryUtil.getLog(VerifyOrganization.class);
121    
122    }