001    /**
002     * Copyright (c) 2000-present 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.util.PortalInstances;
024    import com.liferay.portlet.asset.model.AssetEntry;
025    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
026    
027    import java.util.List;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Daniel Kocsis
032     */
033    public class VerifyOrganization extends VerifyProcess {
034    
035            @Override
036            protected void doVerify() throws Exception {
037                    rebuildTree();
038    
039                    updateOrganizationAssets();
040    
041                    updateOrganizationAssetEntries();
042            }
043    
044            protected void rebuildTree() throws Exception {
045                    long[] companyIds = PortalInstances.getCompanyIdsBySQL();
046    
047                    for (long companyId : companyIds) {
048                            OrganizationLocalServiceUtil.rebuildTree(companyId);
049                    }
050            }
051    
052            protected void updateOrganizationAssetEntries() throws Exception {
053                    ActionableDynamicQuery actionableDynamicQuery =
054                            OrganizationLocalServiceUtil.getActionableDynamicQuery();
055    
056                    actionableDynamicQuery.setPerformActionMethod(
057                            new ActionableDynamicQuery.PerformActionMethod() {
058    
059                                    @Override
060                                    public void performAction(Object object) {
061                                            Organization organization = (Organization)object;
062    
063                                            try {
064                                                    AssetEntry assetEntry =
065                                                            AssetEntryLocalServiceUtil.getEntry(
066                                                                    Organization.class.getName(),
067                                                                    organization.getOrganizationId());
068    
069                                                    if (Validator.isNotNull(assetEntry.getClassUuid())) {
070                                                            return;
071                                                    }
072    
073                                                    assetEntry.setClassUuid(organization.getUuid());
074    
075                                                    AssetEntryLocalServiceUtil.updateAssetEntry(assetEntry);
076                                            }
077                                            catch (Exception e) {
078                                                    if (_log.isWarnEnabled()) {
079                                                            _log.warn(
080                                                                    "Unable to update asset entry for " +
081                                                                            "organization " +
082                                                                                    organization.getOrganizationId(),
083                                                                    e);
084                                                    }
085                                            }
086                                    }
087    
088                            });
089    
090                    actionableDynamicQuery.performActions();
091            }
092    
093            protected void updateOrganizationAssets() throws Exception {
094                    List<Organization> organizations =
095                            OrganizationLocalServiceUtil.getNoAssetOrganizations();
096    
097                    if (_log.isDebugEnabled()) {
098                            _log.debug(
099                                    "Processing " + organizations.size() + " organizations with " +
100                                            "no asset");
101                    }
102    
103                    for (Organization organization : organizations) {
104                            try {
105                                    OrganizationLocalServiceUtil.updateAsset(
106                                            organization.getUserId(), organization, null, null);
107                            }
108                            catch (Exception e) {
109                                    if (_log.isWarnEnabled()) {
110                                            _log.warn(
111                                                    "Unable to update asset for organization " +
112                                                            organization.getOrganizationId() + ": " +
113                                                                    e.getMessage());
114                                    }
115                            }
116                    }
117    
118                    if (_log.isDebugEnabled()) {
119                            _log.debug("Assets verified for organizations");
120                    }
121            }
122    
123            private static final Log _log = LogFactoryUtil.getLog(
124                    VerifyOrganization.class);
125    
126    }