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<Organization>() {
058    
059                                    @Override
060                                    public void performAction(Organization organization) {
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 " +
079                                                                            "organization " +
080                                                                                    organization.getOrganizationId(),
081                                                                    e);
082                                                    }
083                                            }
084                                    }
085    
086                            });
087    
088                    actionableDynamicQuery.performActions();
089            }
090    
091            protected void updateOrganizationAssets() throws Exception {
092                    List<Organization> organizations =
093                            OrganizationLocalServiceUtil.getNoAssetOrganizations();
094    
095                    if (_log.isDebugEnabled()) {
096                            _log.debug(
097                                    "Processing " + organizations.size() + " organizations with " +
098                                            "no asset");
099                    }
100    
101                    for (Organization organization : organizations) {
102                            try {
103                                    OrganizationLocalServiceUtil.updateAsset(
104                                            organization.getUserId(), organization, null, null);
105                            }
106                            catch (Exception e) {
107                                    if (_log.isWarnEnabled()) {
108                                            _log.warn(
109                                                    "Unable to update asset for organization " +
110                                                            organization.getOrganizationId() + ": " +
111                                                                    e.getMessage());
112                                    }
113                            }
114                    }
115    
116                    if (_log.isDebugEnabled()) {
117                            _log.debug("Assets verified for organizations");
118                    }
119            }
120    
121            private static final Log _log = LogFactoryUtil.getLog(
122                    VerifyOrganization.class);
123    
124    }